github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/app/lib/crypto1/pbkdf2/pbkdf2.js (about) 1 /*! 2 * Crypto-JS v1.1.0 3 * http://code.google.com/p/crypto-js/ 4 * Copyright (c) 2009, Jeff Mott. All rights reserved. 5 * http://code.google.com/p/crypto-js/wiki/License 6 */ 7 (function(){ 8 9 // Shortcut 10 var util = Crypto.util; 11 12 Crypto.PBKDF2 = function (password, salt, keylen, options) { 13 14 // Defaults 15 var hasher = options && options.hasher || Crypto.SHA1, 16 iterations = options && options.iterations || 1; 17 18 // Pseudo-random function 19 function PRF(password, salt) { 20 return Crypto.HMAC(hasher, salt, password, { asBytes: true }); 21 } 22 23 // Generate key 24 var derivedKeyBytes = [], 25 blockindex = 1; 26 while (derivedKeyBytes.length < keylen) { 27 28 var block = PRF(password, salt + util.bytesToString( 29 util.wordsToBytes([blockindex]))); 30 31 for (var u = block, i = 1; i < iterations; i++) { 32 u = PRF(password, util.bytesToString(u)); 33 for (var j = 0; j < block.length; j++) block[j] ^= u[j]; 34 } 35 36 derivedKeyBytes = derivedKeyBytes.concat(block); 37 blockindex++; 38 39 } 40 41 // Truncate excess bytes 42 derivedKeyBytes.length = keylen; 43 44 return options && options.asBytes ? derivedKeyBytes : 45 options && options.asString ? util.bytesToString(derivedKeyBytes) : 46 util.bytesToHex(derivedKeyBytes); 47 48 }; 49 50 })();