UNPKG

4.06 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 WordArray = C_lib.WordArray;
21 var Hasher = C_lib.Hasher;
22 var C_algo = C.algo;
23
24 // Reusable object
25 var W = [];
26
27 /**
28 * SHA-1 hash algorithm.
29 */
30 var SHA1 = C_algo.SHA1 = Hasher.extend({
31 _doReset: function () {
32 this._hash = new WordArray.init([
33 0x67452301, 0xefcdab89,
34 0x98badcfe, 0x10325476,
35 0xc3d2e1f0
36 ]);
37 },
38
39 _doProcessBlock: function (M, offset) {
40 // Shortcut
41 var H = this._hash.words;
42
43 // Working variables
44 var a = H[0];
45 var b = H[1];
46 var c = H[2];
47 var d = H[3];
48 var e = H[4];
49
50 // Computation
51 for (var i = 0; i < 80; i++) {
52 if (i < 16) {
53 W[i] = M[offset + i] | 0;
54 } else {
55 var n = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
56 W[i] = (n << 1) | (n >>> 31);
57 }
58
59 var t = ((a << 5) | (a >>> 27)) + e + W[i];
60 if (i < 20) {
61 t += ((b & c) | (~b & d)) + 0x5a827999;
62 } else if (i < 40) {
63 t += (b ^ c ^ d) + 0x6ed9eba1;
64 } else if (i < 60) {
65 t += ((b & c) | (b & d) | (c & d)) - 0x70e44324;
66 } else /* if (i < 80) */ {
67 t += (b ^ c ^ d) - 0x359d3e2a;
68 }
69
70 e = d;
71 d = c;
72 c = (b << 30) | (b >>> 2);
73 b = a;
74 a = t;
75 }
76
77 // Intermediate hash value
78 H[0] = (H[0] + a) | 0;
79 H[1] = (H[1] + b) | 0;
80 H[2] = (H[2] + c) | 0;
81 H[3] = (H[3] + d) | 0;
82 H[4] = (H[4] + e) | 0;
83 },
84
85 _doFinalize: function () {
86 // Shortcuts
87 var data = this._data;
88 var dataWords = data.words;
89
90 var nBitsTotal = this._nDataBytes * 8;
91 var nBitsLeft = data.sigBytes * 8;
92
93 // Add padding
94 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
95 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
96 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
97 data.sigBytes = dataWords.length * 4;
98
99 // Hash final blocks
100 this._process();
101
102 // Return final computed hash
103 return this._hash;
104 },
105
106 clone: function () {
107 var clone = Hasher.clone.call(this);
108 clone._hash = this._hash.clone();
109
110 return clone;
111 }
112 });
113
114 /**
115 * Shortcut function to the hasher's object interface.
116 *
117 * @param {WordArray|string} message The message to hash.
118 *
119 * @return {WordArray} The hash.
120 *
121 * @static
122 *
123 * @example
124 *
125 * var hash = CryptoJS.SHA1('message');
126 * var hash = CryptoJS.SHA1(wordArray);
127 */
128 C.SHA1 = Hasher._createHelper(SHA1);
129
130 /**
131 * Shortcut function to the HMAC's object interface.
132 *
133 * @param {WordArray|string} message The message to hash.
134 * @param {WordArray|string} key The secret key.
135 *
136 * @return {WordArray} The HMAC.
137 *
138 * @static
139 *
140 * @example
141 *
142 * var hmac = CryptoJS.HmacSHA1(message, key);
143 */
144 C.HmacSHA1 = Hasher._createHmacHelper(SHA1);
145 }());
146
147
148 return CryptoJS.SHA1;
149
150}));
\No newline at end of file