UNPKG

3.98 kBJavaScriptView Raw
1;(function (root, factory) {
2 if (typeof exports === "object") {
3 // CommonJS
4 module.exports = exports = factory(require("./core"));
5 }
6 else if (typeof define === "function" && define.amd) {
7 // AMD
8 define(["./core"], 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 C_enc = C.enc;
22 var Utf8 = C_enc.Utf8;
23 var C_algo = C.algo;
24
25 /**
26 * HMAC algorithm.
27 */
28 var HMAC = C_algo.HMAC = Base.extend({
29 /**
30 * Initializes a newly created HMAC.
31 *
32 * @param {Hasher} hasher The hash algorithm to use.
33 * @param {WordArray|string} key The secret key.
34 *
35 * @example
36 *
37 * var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
38 */
39 init: function (hasher, key) {
40 // Init hasher
41 hasher = this._hasher = new hasher.init();
42
43 // Convert string to WordArray, else assume WordArray already
44 if (typeof key == 'string') {
45 key = Utf8.parse(key);
46 }
47
48 // Shortcuts
49 var hasherBlockSize = hasher.blockSize;
50 var hasherBlockSizeBytes = hasherBlockSize * 4;
51
52 // Allow arbitrary length keys
53 if (key.sigBytes > hasherBlockSizeBytes) {
54 key = hasher.finalize(key);
55 }
56
57 // Clamp excess bits
58 key.clamp();
59
60 // Clone key for inner and outer pads
61 var oKey = this._oKey = key.clone();
62 var iKey = this._iKey = key.clone();
63
64 // Shortcuts
65 var oKeyWords = oKey.words;
66 var iKeyWords = iKey.words;
67
68 // XOR keys with pad constants
69 for (var i = 0; i < hasherBlockSize; i++) {
70 oKeyWords[i] ^= 0x5c5c5c5c;
71 iKeyWords[i] ^= 0x36363636;
72 }
73 oKey.sigBytes = iKey.sigBytes = hasherBlockSizeBytes;
74
75 // Set initial values
76 this.reset();
77 },
78
79 /**
80 * Resets this HMAC to its initial state.
81 *
82 * @example
83 *
84 * hmacHasher.reset();
85 */
86 reset: function () {
87 // Shortcut
88 var hasher = this._hasher;
89
90 // Reset
91 hasher.reset();
92 hasher.update(this._iKey);
93 },
94
95 /**
96 * Updates this HMAC with a message.
97 *
98 * @param {WordArray|string} messageUpdate The message to append.
99 *
100 * @return {HMAC} This HMAC instance.
101 *
102 * @example
103 *
104 * hmacHasher.update('message');
105 * hmacHasher.update(wordArray);
106 */
107 update: function (messageUpdate) {
108 this._hasher.update(messageUpdate);
109
110 // Chainable
111 return this;
112 },
113
114 /**
115 * Finalizes the HMAC computation.
116 * Note that the finalize operation is effectively a destructive, read-once operation.
117 *
118 * @param {WordArray|string} messageUpdate (Optional) A final message update.
119 *
120 * @return {WordArray} The HMAC.
121 *
122 * @example
123 *
124 * var hmac = hmacHasher.finalize();
125 * var hmac = hmacHasher.finalize('message');
126 * var hmac = hmacHasher.finalize(wordArray);
127 */
128 finalize: function (messageUpdate) {
129 // Shortcut
130 var hasher = this._hasher;
131
132 // Compute HMAC
133 var innerHash = hasher.finalize(messageUpdate);
134 hasher.reset();
135 var hmac = hasher.finalize(this._oKey.clone().concat(innerHash));
136
137 return hmac;
138 }
139 });
140 }());
141
142
143}));
\No newline at end of file