Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ultraviolet
GitHub Repository: ultraviolet/bitaddress.org
Path: blob/master/src/cryptojs.pbkdf2.js
248 views
1
/*!
2
* Crypto-JS v2.5.4 PBKDF2.js
3
* http://code.google.com/p/crypto-js/
4
* Copyright (c) 2009-2013, Jeff Mott. All rights reserved.
5
* http://code.google.com/p/crypto-js/wiki/License
6
*/
7
(function () {
8
9
// Shortcuts
10
var C = Crypto,
11
util = C.util,
12
charenc = C.charenc,
13
UTF8 = charenc.UTF8,
14
Binary = charenc.Binary;
15
16
C.PBKDF2 = function (password, salt, keylen, options) {
17
18
// Convert to byte arrays
19
if (password.constructor == String) password = UTF8.stringToBytes(password);
20
if (salt.constructor == String) salt = UTF8.stringToBytes(salt);
21
/* else, assume byte arrays already */
22
23
// Defaults
24
var hasher = options && options.hasher || C.SHA1,
25
iterations = options && options.iterations || 1;
26
27
// Pseudo-random function
28
function PRF(password, salt) {
29
return C.HMAC(hasher, salt, password, { asBytes: true });
30
}
31
32
// Generate key
33
var derivedKeyBytes = [],
34
blockindex = 1;
35
while (derivedKeyBytes.length < keylen) {
36
var block = PRF(password, salt.concat(util.wordsToBytes([blockindex])));
37
for (var u = block, i = 1; i < iterations; i++) {
38
u = PRF(password, u);
39
for (var j = 0; j < block.length; j++) block[j] ^= u[j];
40
}
41
derivedKeyBytes = derivedKeyBytes.concat(block);
42
blockindex++;
43
}
44
45
// Truncate excess bytes
46
derivedKeyBytes.length = keylen;
47
48
return options && options.asBytes ? derivedKeyBytes :
49
options && options.asString ? Binary.bytesToString(derivedKeyBytes) :
50
util.bytesToHex(derivedKeyBytes);
51
52
};
53
54
})();
55