UNPKG

3.99 kBJavaScriptView Raw
1;(function (root, factory, undef) {
2 if (typeof exports === "object") {
3 // CommonJS
4 module.exports = exports = factory(require("./core"), require("./sha1"), require("./hmac"));
5 }
6 else if (typeof define === "function" && define.amd) {
7 // AMD
8 define(["./core", "./sha1", "./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 MD5 = C_algo.MD5;
24
25 /**
26 * This key derivation function is meant to conform with EVP_BytesToKey.
27 * www.openssl.org/docs/crypto/EVP_BytesToKey.html
28 */
29 var EvpKDF = C_algo.EvpKDF = 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 hash algorithm to use. Default: MD5
35 * @property {number} iterations The number of iterations to perform. Default: 1
36 */
37 cfg: Base.extend({
38 keySize: 128/32,
39 hasher: MD5,
40 iterations: 1
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.EvpKDF.create();
51 * var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8 });
52 * var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8, iterations: 1000 });
53 */
54 init: function (cfg) {
55 this.cfg = this.cfg.extend(cfg);
56 },
57
58 /**
59 * Derives a key from a password.
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 var block;
72
73 // Shortcut
74 var cfg = this.cfg;
75
76 // Init hasher
77 var hasher = cfg.hasher.create();
78
79 // Initial values
80 var derivedKey = WordArray.create();
81
82 // Shortcuts
83 var derivedKeyWords = derivedKey.words;
84 var keySize = cfg.keySize;
85 var iterations = cfg.iterations;
86
87 // Generate key
88 while (derivedKeyWords.length < keySize) {
89 if (block) {
90 hasher.update(block);
91 }
92 block = hasher.update(password).finalize(salt);
93 hasher.reset();
94
95 // Iterations
96 for (var i = 1; i < iterations; i++) {
97 block = hasher.finalize(block);
98 hasher.reset();
99 }
100
101 derivedKey.concat(block);
102 }
103 derivedKey.sigBytes = keySize * 4;
104
105 return derivedKey;
106 }
107 });
108
109 /**
110 * Derives a key from a password.
111 *
112 * @param {WordArray|string} password The password.
113 * @param {WordArray|string} salt A salt.
114 * @param {Object} cfg (Optional) The configuration options to use for this computation.
115 *
116 * @return {WordArray} The derived key.
117 *
118 * @static
119 *
120 * @example
121 *
122 * var key = CryptoJS.EvpKDF(password, salt);
123 * var key = CryptoJS.EvpKDF(password, salt, { keySize: 8 });
124 * var key = CryptoJS.EvpKDF(password, salt, { keySize: 8, iterations: 1000 });
125 */
126 C.EvpKDF = function (password, salt, cfg) {
127 return EvpKDF.create(cfg).compute(password, salt);
128 };
129 }());
130
131
132 return CryptoJS.EvpKDF;
133
134}));
\No newline at end of file