3
code.google.com/p/crypto-js
4
(c) 2009-2012 by Jeff Mott. All rights reserved.
5
code.google.com/p/crypto-js/wiki/License
11
var WordArray = C_lib.WordArray;
12
var Hasher = C_lib.Hasher;
15
// Initialization and round constants tables
22
var sqrtN = Math.sqrt(n);
23
for (var factor = 2; factor <= sqrtN; factor++) {
32
function getFractionalBits(n) {
33
return ((n - (n | 0)) * 0x100000000) | 0;
41
H[nPrime] = getFractionalBits(Math.pow(n, 1 / 2));
43
K[nPrime] = getFractionalBits(Math.pow(n, 1 / 3));
56
* SHA-256 hash algorithm.
58
var SHA256 = C_algo.SHA256 = Hasher.extend({
59
_doReset: function () {
60
this._hash = WordArray.create(H.slice(0));
63
_doProcessBlock: function (M, offset) {
65
var H = this._hash.words;
78
for (var i = 0; i < 64; i++) {
80
W[i] = M[offset + i] | 0;
82
var gamma0x = W[i - 15];
83
var gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^
84
((gamma0x << 14) | (gamma0x >>> 18)) ^
87
var gamma1x = W[i - 2];
88
var gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^
89
((gamma1x << 13) | (gamma1x >>> 19)) ^
92
W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16];
95
var ch = (e & f) ^ (~e & g);
96
var maj = (a & b) ^ (a & c) ^ (b & c);
98
var sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22));
99
var sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7) | (e >>> 25));
101
var t1 = h + sigma1 + ch + K[i] + W[i];
102
var t2 = sigma0 + maj;
114
// Intermediate hash value
115
H[0] = (H[0] + a) | 0;
116
H[1] = (H[1] + b) | 0;
117
H[2] = (H[2] + c) | 0;
118
H[3] = (H[3] + d) | 0;
119
H[4] = (H[4] + e) | 0;
120
H[5] = (H[5] + f) | 0;
121
H[6] = (H[6] + g) | 0;
122
H[7] = (H[7] + h) | 0;
125
_doFinalize: function () {
127
var data = this._data;
128
var dataWords = data.words;
130
var nBitsTotal = this._nDataBytes * 8;
131
var nBitsLeft = data.sigBytes * 8;
134
dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
135
dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
136
data.sigBytes = dataWords.length * 4;
144
* Shortcut function to the hasher's object interface.
146
* @param {WordArray|string} message The message to hash.
148
* @return {WordArray} The hash.
154
* var hash = CryptoJS.SHA256('message');
155
* var hash = CryptoJS.SHA256(wordArray);
157
C.SHA256 = Hasher._createHelper(SHA256);
160
* Shortcut function to the HMAC's object interface.
162
* @param {WordArray|string} message The message to hash.
163
* @param {WordArray|string} key The secret key.
165
* @return {WordArray} The HMAC.
171
* var hmac = CryptoJS.HmacSHA256(message, key);
173
C.HmacSHA256 = Hasher._createHmacHelper(SHA256);