* Remove ascii parameter.

* Improve performance.
* Add test cases.
This commit is contained in:
emn178
2015-02-03 13:59:16 +08:00
parent f0ba196020
commit c016df9256
9 changed files with 95 additions and 136 deletions
+61 -90
View File
@@ -1,5 +1,5 @@
/*
* js-sha256 v0.1.4
* js-sha256 v0.2.0
* https://github.com/emn178/js-sha256
*
* Copyright 2014-2015, emn178@gmail.com
@@ -13,14 +13,10 @@
var NODE_JS = typeof(module) != 'undefined';
if(NODE_JS) {
root = global;
if(root.JS_SHA256_TEST) {
root.navigator = { userAgent: 'Firefox'};
}
}
var FIREFOX = (root.JS_SHA256_TEST || !NODE_JS) && navigator.userAgent.indexOf('Firefox') != -1;
var HEX_CHARS = '0123456789abcdef'.split('');
var EXTRA = [-2147483648, 8388608, 32768, 128];
var SHIFT = [24, 16, 8, 0];
var K =[0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
@@ -30,21 +26,21 @@
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2];
var sha256 = function(message, asciiOnly) {
return sha2(message, true, asciiOnly);
var blocks = [];
var sha256 = function(message) {
return sha2(message, true);
};
var sha224 = function(message, asciiOnly) {
return sha2(message, false, asciiOnly);
var sha224 = function(message) {
return sha2(message, false);
};
var sha2 = function(message, is256, asciiOnly) {
var chunks, h0, h1, h2, h3, h4, h5, h6, h7;
if(!asciiOnly && /[^\x00-\x7F]/.test(message)) {
chunks = getChunksFromUtf8(message);
} else {
chunks = getChunksFromAscii(message);
}
var sha2 = function(message, is256) {
var h0, h1, h2, h3, h4, h5, h6, h7, code, end = false,
i, j, index = 0, start = 0, bytes = 0, length = message.length,
s0, s1, tmp1, tmp2, tmp3, maj, t1, t2, ch, ab, da, cd, bc;
if(is256) {
h0 = 0x6a09e667;
h1 = 0xbb67ae85;
@@ -64,22 +60,56 @@
h6 = 0x64f98fa7;
h7 = 0xbefa4fa4;
}
blocks[64] = 0;
do {
blocks[0] = blocks[64];
blocks[16] = blocks[1] = blocks[2] = blocks[3] =
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
for (i = start;index < length && i < 64; ++index) {
code = message.charCodeAt(index);
if (code < 0x80) {
blocks[i >> 2] |= code << SHIFT[i++ & 3];
} else if (code < 0x800) {
blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else if (code < 0xd800 || code >= 0xe000) {
blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
}
}
blocks[64] = blocks[16];
bytes += i - start;
start = i - 64;
if(index == length) {
blocks[i >> 2] |= EXTRA[i & 3];
++index;
}
if(index > length && i < 56) {
blocks[15] = bytes << 3;
end = true;
}
for(var i = 0, length = chunks.length;i < length;++i) {
var w = chunks[i], s0, s1, j, tmp1, tmp2, tmp3, maj, t1, t2, ch, ab, da, cd, bc,
a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
var a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
for(j = 16;j < 64;++j) {
// rightrotate
tmp1 = tmp2 = w[j - 15];
tmp1 = tmp2 = blocks[j - 15];
tmp1 = (tmp1 >>> 7) | (tmp1 << 25);
tmp2 = (tmp2 >>> 18) | (tmp2 << 14);
s0 = tmp1 ^ tmp2 ^ (w[j - 15] >>> 3);
tmp1 = tmp2 = w[j - 2];
s0 = tmp1 ^ tmp2 ^ (blocks[j - 15] >>> 3);
tmp1 = tmp2 = blocks[j - 2];
tmp1 = (tmp1 >>> 17) | (tmp1 << 15);
tmp2 = (tmp2 >>> 19) | (tmp2 << 13);
s1 = tmp1 ^ tmp2 ^ (w[j - 2] >>> 10);
w[j] = w[j - 16] + s0 + w[j - 7] + s1 << 0;
s1 = tmp1 ^ tmp2 ^ (blocks[j - 2] >>> 10);
blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0;
}
bc = b & c;
@@ -95,7 +125,7 @@
ab = a & b;
maj = ab ^ (a & c) ^ bc;
ch = (e & f) ^ (~e & g);
t1 = h + s1 + ch + K[j] + w[j] << 0;
t1 = h + s1 + ch + K[j] + blocks[j] << 0;
t2 = s0 + maj << 0;
h = d + t1 << 0;
d = t1 + t2 << 0;
@@ -110,7 +140,7 @@
da = d & a;
maj = da ^ (d & b) ^ ab;
ch = (h & e) ^ (~h & f);
t1 = g + s1 + ch + K[j + 1] + w[j + 1] << 0;
t1 = g + s1 + ch + K[j + 1] + blocks[j + 1] << 0;
t2 = s0 + maj << 0;
g = c + t1 << 0;
c = t1 + t2 << 0;
@@ -125,7 +155,7 @@
cd = c & d;
maj = cd ^ (c & a) ^ da;
ch = (g & h) ^ (~g & e);
t1 = f + s1 + ch + K[j + 2] + w[j + 2] << 0;
t1 = f + s1 + ch + K[j + 2] + blocks[j + 2] << 0;
t2 = s0 + maj << 0;
f = b + t1 << 0;
b = t1 + t2 << 0;
@@ -140,7 +170,7 @@
bc = b & c;
maj = bc ^ (b & d) ^ cd;
ch = (f & g) ^ (~f & h);
t1 = e + s1 + ch + K[j + 3] + w[j + 3] << 0;
t1 = e + s1 + ch + K[j + 3] + blocks[j + 3] << 0;
t2 = s0 + maj << 0;
e = a + t1 << 0;
a = t1 + t2 << 0;
@@ -154,7 +184,7 @@
h5 = h5 + f << 0;
h6 = h6 + g << 0;
h7 = h7 + h << 0;
}
} while(!end);
var hex = HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
@@ -193,65 +223,6 @@
return hex;
};
var getBytesFromUtf8 = function(str) {
var bytes = [], index = 0;
for (var i = 0;i < str.length; i++) {
var c = str.charCodeAt(i);
if (c < 0x80) {
bytes[index++] = c;
} else if (c < 0x800) {
bytes[index++] = 0xc0 | (c >> 6);
bytes[index++] = 0x80 | (c & 0x3f);
} else if (c < 0xd800 || c >= 0xe000) {
bytes[index++] = 0xe0 | (c >> 12);
bytes[index++] = 0x80 | ((c >> 6) & 0x3f);
bytes[index++] = 0x80 | (c & 0x3f);
} else {
c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff));
bytes[index++] = 0xf0 | (c >> 18);
bytes[index++] = 0x80 | ((c >> 12) & 0x3f);
bytes[index++] = 0x80 | ((c >> 6) & 0x3f);
bytes[index++] = 0x80 | (c & 0x3f);
}
}
return bytes;
};
var getChunksFromAscii = function(message) {
// a block is 32 bits(4 bytes), a chunk is 512 bits(64 bytes)
var length = message.length;
var chunkCount = ((length + 8) >> 6) + 1;
var chunks = [], blocks, i;
for(i = 0;i < chunkCount;++i) {
chunks[i] = blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
}
for(i = 0;i < length;++i) {
blocks = chunks[i >> 6];
blocks[(i & 63) >> 2] |= message.charCodeAt(i) << SHIFT[i & 3];
}
blocks[(i & 63) >> 2] |= 0x80 << SHIFT[i & 3];
blocks[15] = length << 3; // length * 8
return chunks;
};
var getChunksFromUtf8 = function(message) {
// a block is 32 bits(4 bytes), a chunk is 512 bits(64 bytes)
var bytes = getBytesFromUtf8(message);
var length = bytes.length;
var chunkCount = ((length + 8) >> 6) + 1;
var chunks = [], blocks, i;
for(i = 0;i < chunkCount;++i) {
chunks[i] = blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
}
for(i = 0;i < length;++i) {
blocks = chunks[i >> 6];
blocks[(i & 63) >> 2] |= bytes[i] << SHIFT[i & 3];
}
blocks[(i & 63) >> 2] |= 0x80 << SHIFT[i & 3];
blocks[15] = length << 3; // length * 8
return chunks;
};
if(!root.JS_SHA256_TEST && NODE_JS) {
sha256.sha256 = sha256;
sha256.sha224 = sha224;