1 | ;(function (root, factory, undef) {
|
2 | if (typeof exports === "object") {
|
3 | // CommonJS
|
4 | module.exports = exports = factory(require("./core"), require("./sha256"), require("./hmac"));
|
5 | }
|
6 | else if (typeof define === "function" && define.amd) {
|
7 | // AMD
|
8 | define(["./core", "./sha256", "./hmac"], factory);
|
9 | }
|
10 | else {
|
11 | // Global (browser)
|
12 | factory(root.CryptoJS);
|
13 | }
|
14 | }(this, function (CryptoJS) {
|
15 |
|
16 | (function () {
|
17 | // Shortcuts
|
18 | var C = CryptoJS;
|
19 | var C_lib = C.lib;
|
20 | var Base = C_lib.Base;
|
21 | var WordArray = C_lib.WordArray;
|
22 | var C_algo = C.algo;
|
23 | var SHA256 = C_algo.SHA256;
|
24 | var HMAC = C_algo.HMAC;
|
25 |
|
26 | /**
|
27 | * Password-Based Key Derivation Function 2 algorithm.
|
28 | */
|
29 | var PBKDF2 = C_algo.PBKDF2 = Base.extend({
|
30 | /**
|
31 | * Configuration options.
|
32 | *
|
33 | * @property {number} keySize The key size in words to generate. Default: 4 (128 bits)
|
34 | * @property {Hasher} hasher The hasher to use. Default: SHA256
|
35 | * @property {number} iterations The number of iterations to perform. Default: 250000
|
36 | */
|
37 | cfg: Base.extend({
|
38 | keySize: 128/32,
|
39 | hasher: SHA256,
|
40 | iterations: 250000
|
41 | }),
|
42 |
|
43 | /**
|
44 | * Initializes a newly created key derivation function.
|
45 | *
|
46 | * @param {Object} cfg (Optional) The configuration options to use for the derivation.
|
47 | *
|
48 | * @example
|
49 | *
|
50 | * var kdf = CryptoJS.algo.PBKDF2.create();
|
51 | * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8 });
|
52 | * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8, iterations: 1000 });
|
53 | */
|
54 | init: function (cfg) {
|
55 | this.cfg = this.cfg.extend(cfg);
|
56 | },
|
57 |
|
58 | /**
|
59 | * Computes the Password-Based Key Derivation Function 2.
|
60 | *
|
61 | * @param {WordArray|string} password The password.
|
62 | * @param {WordArray|string} salt A salt.
|
63 | *
|
64 | * @return {WordArray} The derived key.
|
65 | *
|
66 | * @example
|
67 | *
|
68 | * var key = kdf.compute(password, salt);
|
69 | */
|
70 | compute: function (password, salt) {
|
71 | // Shortcut
|
72 | var cfg = this.cfg;
|
73 |
|
74 | // Init HMAC
|
75 | var hmac = HMAC.create(cfg.hasher, password);
|
76 |
|
77 | // Initial values
|
78 | var derivedKey = WordArray.create();
|
79 | var blockIndex = WordArray.create([0x00000001]);
|
80 |
|
81 | // Shortcuts
|
82 | var derivedKeyWords = derivedKey.words;
|
83 | var blockIndexWords = blockIndex.words;
|
84 | var keySize = cfg.keySize;
|
85 | var iterations = cfg.iterations;
|
86 |
|
87 | // Generate key
|
88 | while (derivedKeyWords.length < keySize) {
|
89 | var block = hmac.update(salt).finalize(blockIndex);
|
90 | hmac.reset();
|
91 |
|
92 | // Shortcuts
|
93 | var blockWords = block.words;
|
94 | var blockWordsLength = blockWords.length;
|
95 |
|
96 | // Iterations
|
97 | var intermediate = block;
|
98 | for (var i = 1; i < iterations; i++) {
|
99 | intermediate = hmac.finalize(intermediate);
|
100 | hmac.reset();
|
101 |
|
102 | // Shortcut
|
103 | var intermediateWords = intermediate.words;
|
104 |
|
105 | // XOR intermediate with block
|
106 | for (var j = 0; j < blockWordsLength; j++) {
|
107 | blockWords[j] ^= intermediateWords[j];
|
108 | }
|
109 | }
|
110 |
|
111 | derivedKey.concat(block);
|
112 | blockIndexWords[0]++;
|
113 | }
|
114 | derivedKey.sigBytes = keySize * 4;
|
115 |
|
116 | return derivedKey;
|
117 | }
|
118 | });
|
119 |
|
120 | /**
|
121 | * Computes the Password-Based Key Derivation Function 2.
|
122 | *
|
123 | * @param {WordArray|string} password The password.
|
124 | * @param {WordArray|string} salt A salt.
|
125 | * @param {Object} cfg (Optional) The configuration options to use for this computation.
|
126 | *
|
127 | * @return {WordArray} The derived key.
|
128 | *
|
129 | * @static
|
130 | *
|
131 | * @example
|
132 | *
|
133 | * var key = CryptoJS.PBKDF2(password, salt);
|
134 | * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8 });
|
135 | * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8, iterations: 1000 });
|
136 | */
|
137 | C.PBKDF2 = function (password, salt, cfg) {
|
138 | return PBKDF2.create(cfg).compute(password, salt);
|
139 | };
|
140 | }());
|
141 |
|
142 |
|
143 | return CryptoJS.PBKDF2;
|
144 |
|
145 | })); |
\ | No newline at end of file |