mirror of
https://github.com/emn178/js-sha256.git
synced 2026-08-13 04:51:56 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a564472309 | ||
|
|
64149617be |
@@ -0,0 +1 @@
|
||||
node_modules/
|
||||
+7
-3
@@ -1,4 +1,8 @@
|
||||
# v0.1.3 / 2015-01-06
|
||||
# v0.1.3 / 2015-01-09
|
||||
|
||||
* Improve performance.
|
||||
|
||||
# v0.1.2 / 2015-01-06
|
||||
|
||||
* Add bower package.
|
||||
* Fixed JSHint warnings.
|
||||
@@ -7,8 +11,8 @@
|
||||
|
||||
# v0.1.1 / 2014-07-27
|
||||
|
||||
Fixed accents bug
|
||||
* Fixed accents bug
|
||||
|
||||
# v0.1.0 / 2014-01-05
|
||||
|
||||
Initial release
|
||||
* Initial release
|
||||
|
||||
@@ -94,6 +94,10 @@ Output
|
||||
72726d8818f693066ceb69afa364218b692e62ea92b385782363780f47529c21
|
||||
dfbab71afdf54388af4d55f8bd3de8c9b15e0eb916bf9125f4a959d4
|
||||
|
||||
## Benchmark
|
||||
[ASCII](http://jsperf.com/sha256-ascii/)
|
||||
[UTF8](http://jsperf.com/sha256-utf8/)
|
||||
|
||||
## Extensions
|
||||
### jQuery
|
||||
If you prefer jQuery style, you can add following code to add a jQuery extension.
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "js-sha256",
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.3",
|
||||
"main": ["build/sha256.min.js"],
|
||||
"ignore": [
|
||||
"samples",
|
||||
|
||||
Vendored
+7
-5
@@ -1,5 +1,7 @@
|
||||
(function(x,H){var y="0123456789abcdef".split(""),I=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,
|
||||
3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],k=function(b,g){return F(b,!0,g)},G=function(b,g){return F(b,!1,g)},F=function(b,g,c){g===H&&(g=!0);var e,d,a,r,t,u,v;if(!c&&/[^\x00-\x7F]/.test(b)){c=[];for(d=e=0;d<b.length;d++)a=b.charCodeAt(d),128>a?c[e++]=a:(2048>a?c[e++]=192|a>>6:(55296>a||57344<=a?c[e++]=224|
|
||||
a>>12:(a=65536+((a&1023)<<10|b.charCodeAt(++d)&1023),c[e++]=240|a>>18,c[e++]=128|a>>12&63),c[e++]=128|a>>6&63),c[e++]=128|a&63);b=c.length;e=(b+8>>6)+1<<4;d=[];for(a=0;a<e;++a)d[a]=0;for(a=0;a<b;++a)d[a>>2]|=c[a]<<(3-(a&3)<<3);d[a>>2]|=128<<(3-(a&3)<<3);d[e-1]=b<<3}else{c=b.length;e=(c+8>>6)+1<<4;d=[];for(a=0;a<e;++a)d[a]=0;for(a=0;a<c;++a)d[a>>2]|=b.charCodeAt(a)<<(3-(a&3)<<3);d[a>>2]|=128<<(3-(a&3)<<3);d[e-1]=c<<3}c=d;g?(e=1779033703,d=3144134277,a=1013904242,r=2773480762,t=1359893119,u=2600822924,
|
||||
v=528734635,b=1541459225):(e=3238371032,d=914150663,a=812702999,r=4144912697,t=4290775857,u=1750603025,v=1694076839,b=3204075428);for(var k=0,x=c.length;k<x;k+=16){var h=[],w,m,f;for(f=0;16>f;++f)h[f]=c[k+f];for(f=16;64>f;++f)w=l(h[f-15],7)^l(h[f-15],18)^h[f-15]>>>3,m=l(h[f-2],17)^l(h[f-2],19)^h[f-2]>>>10,h[f]=h[f-16]+w+h[f-7]+m;var n=e,z=d,A=a,D=r,p=t,B=u,C=v,E=b,y;for(f=0;64>f;++f)w=l(n,2)^l(n,13)^l(n,22),m=n&z^n&A^z&A,w+=m,m=l(p,6)^l(p,11)^l(p,25),y=p&B^~p&C,m=E+m+y+I[f]+h[f]&4294967295,E=C,C=
|
||||
B,B=p,p=D+m,D=A,A=z,z=n,n=m+w;e+=n;d+=z;a+=A;r+=D;t+=p;u+=B;v+=C;b+=E}c=q(e)+q(d)+q(a)+q(r)+q(t)+q(u)+q(v);g&&(c+=q(b));return c},l=function(b,g){return b>>>g|b<<32-g},q=function(b){for(var g="",c=0;4>c;c++)var e=3-c<<3,g=g+(y[b>>e+4&15]+y[b>>e&15]);return g};"undefined"!=typeof module?(k.sha256=k,k.sha224=G,module.exports=k):x&&(x.sha256=k,x.sha224=G)})(this);
|
||||
(function(E,J){var G="0123456789abcdef".split(""),F=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,
|
||||
3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],z=function(e,x){return H(e,!0,x)},I=function(e,x){return H(e,!1,x)},H=function(e,x,f){x===J&&(x=!0);var k,g,l,a,B,C,D;if(!f&&/[^\x00-\x7F]/.test(e)){g=[];for(l=f=0;l<e.length;l++)a=e.charCodeAt(l),128>a?g[f++]=a:(2048>a?g[f++]=192|a>>6:(55296>a||57344<=a?g[f++]=224|
|
||||
a>>12:(a=65536+((a&1023)<<10|e.charCodeAt(++l)&1023),g[f++]=240|a>>18,g[f++]=128|a>>12&63),g[f++]=128|a>>6&63),g[f++]=128|a&63);e=g.length;f=(e+8>>6)+1;l=[];for(a=0;a<f;++a)l[a]=k=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(a=0;a<e;++a)k=l[a>>6],k[(a&63)>>2]|=g[a]<<(3-(a&3)<<3);k[(a&63)>>2]|=128<<(3-(a&3)<<3);k[15]=e<<3}else{k=e.length;f=(k+8>>6)+1;l=[];for(a=0;a<f;++a)l[a]=g=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(a=0;a<k;++a)g=l[a>>6],g[(a&63)>>2]|=e.charCodeAt(a)<<(3-(a&3)<<3);g[(a&63)>>2]|=128<<(3-(a&
|
||||
3)<<3);g[15]=k<<3}k=l;x?(g=1779033703,f=3144134277,l=1013904242,a=2773480762,B=1359893119,C=2600822924,D=528734635,e=1541459225):(g=3238371032,f=914150663,l=812702999,a=4144912697,B=4290775857,C=1750603025,D=1694076839,e=3204075428);for(var z=0,E=k.length;z<E;++z){var y=k[z],m,b,h,c,d,n=g,p=f,q=l,r=a,t=B,u=C,v=D,w=e;for(h=16;64>h;++h)c=b=y[h-15],c=c>>>7|c<<25,b=b>>>18|b<<14,m=c^b^y[h-15]>>>3,c=b=y[h-2],c=c>>>17|c<<15,b=b>>>19|b<<13,b=c^b^y[h-2]>>>10,y[h]=y[h-16]+m+y[h-7]+b<<0;for(h=0;64>h;h+=4)c=
|
||||
n>>>2|n<<30,b=n>>>13|n<<19,d=n>>>22|n<<10,m=c^b^d,c=t>>>6|t<<26,b=t>>>11|t<<21,d=t>>>25|t<<7,b=c^b^d,c=n&p^n&q^p&q,d=t&u^~t&v,b=w+b+d+F[h+0]+y[h+0]<<0,m=m+c<<0,w=r+b<<0,r=b+m<<0,c=r>>>2|r<<30,b=r>>>13|r<<19,d=r>>>22|r<<10,m=c^b^d,c=w>>>6|w<<26,b=w>>>11|w<<21,d=w>>>25|w<<7,b=c^b^d,c=r&n^r&p^n&p,d=w&t^~w&u,b=v+b+d+F[h+1]+y[h+1]<<0,m=m+c<<0,v=q+b<<0,q=b+m<<0,c=q>>>2|q<<30,b=q>>>13|q<<19,d=q>>>22|q<<10,m=c^b^d,c=v>>>6|v<<26,b=v>>>11|v<<21,d=v>>>25|v<<7,b=c^b^d,c=q&r^q&n^r&n,d=v&w^~v&t,b=u+b+d+F[h+2]+
|
||||
y[h+2]<<0,m=m+c<<0,u=p+b<<0,p=b+m<<0,c=p>>>2|p<<30,b=p>>>13|p<<19,d=p>>>22|p<<10,m=c^b^d,c=u>>>6|u<<26,b=u>>>11|u<<21,d=u>>>25|u<<7,b=c^b^d,c=p&q^p&r^q&r,d=u&v^~u&w,b=t+b+d+F[h+3]+y[h+3]<<0,m=m+c<<0,t=n+b<<0,n=b+m<<0;g=g+n<<0;f=f+p<<0;l=l+q<<0;a=a+r<<0;B=B+t<<0;C=C+u<<0;D=D+v<<0;e=e+w<<0}k=A(g)+A(f)+A(l)+A(a)+A(B)+A(C)+A(D);x&&(k+=A(e));return k},A=function(e){for(var x="",f=0;4>f;f++)var k=3-f<<3,x=x+(G[e>>k+4&15]+G[e>>k&15]);return x};"undefined"!=typeof module?(z.sha256=z,z.sha224=I,module.exports=
|
||||
z):E&&(E.sha256=z,E.sha224=I)})(this);
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "js-sha256",
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.3",
|
||||
"description": "A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.",
|
||||
"main": "src/sha256.js",
|
||||
"devDependencies": {
|
||||
|
||||
+101
-67
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* js-sha256 v0.1.2
|
||||
* js-sha256 v0.1.3
|
||||
* https://github.com/emn178/js-sha256
|
||||
*
|
||||
* Copyright 2014-2015, emn178@gmail.com
|
||||
@@ -34,11 +34,11 @@
|
||||
is256 = true;
|
||||
}
|
||||
|
||||
var blocks, h0, h1, h2, h3, h4, h5, h6, h7;
|
||||
var chunks, h0, h1, h2, h3, h4, h5, h6, h7;
|
||||
if(!asciiOnly && /[^\x00-\x7F]/.test(message)) {
|
||||
blocks = getBlocksFromUtf8(message);
|
||||
chunks = getChunksFromUtf8(message);
|
||||
} else {
|
||||
blocks = getBlocksFromAscii(message);
|
||||
chunks = getChunksFromAscii(message);
|
||||
}
|
||||
if(is256) {
|
||||
h0 = 0x6a09e667;
|
||||
@@ -60,53 +60,90 @@
|
||||
h7 = 0xbefa4fa4;
|
||||
}
|
||||
|
||||
for(var i = 0, length = blocks.length;i < length;i += 16) {
|
||||
var w = [], s0, s1, j;
|
||||
for(j = 0;j < 16;++j) {
|
||||
w[j] = blocks[i + j];
|
||||
}
|
||||
for(var i = 0, length = chunks.length;i < length;++i) {
|
||||
var w = chunks[i], s0, s1, j, tmp1, tmp2, tmp3, maj, t1, t2, ch,
|
||||
a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
|
||||
|
||||
for(j = 16;j < 64;++j) {
|
||||
s0 = rightrotate(w[j - 15], 7) ^ rightrotate(w[j - 15], 18) ^ (w[j - 15] >>> 3);
|
||||
s1 = rightrotate(w[j - 2], 17) ^ rightrotate(w[j - 2], 19) ^ (w[j - 2] >>> 10);
|
||||
w[j] = w[j - 16] + s0 + w[j - 7] + s1;
|
||||
// rightrotate
|
||||
tmp1 = tmp2 = w[j - 15];
|
||||
tmp1 = (tmp1 >>> 7) | (tmp1 << 25);
|
||||
tmp2 = (tmp2 >>> 18) | (tmp2 << 14);
|
||||
s0 = tmp1 ^ tmp2 ^ (w[j - 15] >>> 3);
|
||||
tmp1 = tmp2 = w[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;
|
||||
}
|
||||
|
||||
var a = h0;
|
||||
var b = h1;
|
||||
var c = h2;
|
||||
var d = h3;
|
||||
var e = h4;
|
||||
var f = h5;
|
||||
var g = h6;
|
||||
var h = h7;
|
||||
var maj, t1, t2, ch;
|
||||
|
||||
for(j = 0;j < 64;++j) {
|
||||
s0 = rightrotate(a, 2) ^ rightrotate(a, 13) ^ rightrotate(a, 22);
|
||||
for(j = 0;j < 64;j += 4) {
|
||||
tmp1 = (a >>> 2) | (a << 30);
|
||||
tmp2 = (a >>> 13) | (a << 19);
|
||||
tmp3 = (a >>> 22) | (a << 10);
|
||||
s0 = tmp1 ^ tmp2 ^ tmp3;
|
||||
tmp1 = (e >>> 6) | (e << 26);
|
||||
tmp2 = (e >>> 11) | (e << 21);
|
||||
tmp3 = (e >>> 25) | (e << 7);
|
||||
s1 = tmp1 ^ tmp2 ^ tmp3;
|
||||
maj = (a & b) ^ (a & c) ^ (b & c);
|
||||
t2 = s0 + maj;
|
||||
s1 = rightrotate(e, 6) ^ rightrotate(e, 11) ^ rightrotate(e, 25);
|
||||
ch = (e & f) ^ ((~ e) & g);
|
||||
t1 = (h + s1 + ch + K[j] + w[j]) & 0xffffffff;
|
||||
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + t1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1 + t2;
|
||||
ch = (e & f) ^ (~e & g);
|
||||
t1 = (h + s1 + ch + K[j + 0] + w[j + 0]) << 0;
|
||||
t2 = (s0 + maj) << 0;
|
||||
h = (d + t1) << 0;
|
||||
d = (t1 + t2) << 0;
|
||||
tmp1 = (d >>> 2) | (d << 30);
|
||||
tmp2 = (d >>> 13) | (d << 19);
|
||||
tmp3 = (d >>> 22) | (d << 10);
|
||||
s0 = tmp1 ^ tmp2 ^ tmp3;
|
||||
tmp1 = (h >>> 6) | (h << 26);
|
||||
tmp2 = (h >>> 11) | (h << 21);
|
||||
tmp3 = (h >>> 25) | (h << 7);
|
||||
s1 = tmp1 ^ tmp2 ^ tmp3;
|
||||
maj = (d & a) ^ (d & b) ^ (a & b);
|
||||
ch = (h & e) ^ (~h & f);
|
||||
t1 = (g + s1 + ch + K[j + 1] + w[j + 1]) << 0;
|
||||
t2 = (s0 + maj) << 0;
|
||||
g = (c + t1) << 0;
|
||||
c = (t1 + t2) << 0;
|
||||
tmp1 = (c >>> 2) | (c << 30);
|
||||
tmp2 = (c >>> 13) | (c << 19);
|
||||
tmp3 = (c >>> 22) | (c << 10);
|
||||
s0 = tmp1 ^ tmp2 ^ tmp3;
|
||||
tmp1 = (g >>> 6) | (g << 26);
|
||||
tmp2 = (g >>> 11) | (g << 21);
|
||||
tmp3 = (g >>> 25) | (g << 7);
|
||||
s1 = tmp1 ^ tmp2 ^ tmp3;
|
||||
maj = (c & d) ^ (c & a) ^ (d & a);
|
||||
ch = (g & h) ^ (~g & e);
|
||||
t1 = (f + s1 + ch + K[j + 2] + w[j + 2]) << 0;
|
||||
t2 = (s0 + maj) << 0;
|
||||
f = (b + t1) << 0;
|
||||
b = (t1 + t2) << 0;
|
||||
tmp1 = (b >>> 2) | (b << 30);
|
||||
tmp2 = (b >>> 13) | (b << 19);
|
||||
tmp3 = (b >>> 22) | (b << 10);
|
||||
s0 = tmp1 ^ tmp2 ^ tmp3;
|
||||
tmp1 = (f >>> 6) | (f << 26);
|
||||
tmp2 = (f >>> 11) | (f << 21);
|
||||
tmp3 = (f >>> 25) | (f << 7);
|
||||
s1 = tmp1 ^ tmp2 ^ tmp3;
|
||||
maj = (b & c) ^ (b & d) ^ (c & d);
|
||||
ch = (f & g) ^ (~f & h);
|
||||
t1 = (e + s1 + ch + K[j + 3] + w[j + 3]) << 0;
|
||||
t2 = (s0 + maj) << 0;
|
||||
e = (a + t1) << 0;
|
||||
a = (t1 + t2) << 0;
|
||||
}
|
||||
|
||||
h0 += a;
|
||||
h1 += b;
|
||||
h2 += c;
|
||||
h3 += d;
|
||||
h4 += e;
|
||||
h5 += f;
|
||||
h6 += g;
|
||||
h7 += h;
|
||||
h0 = (h0 + a) << 0;
|
||||
h1 = (h1 + b) << 0;
|
||||
h2 = (h2 + c) << 0;
|
||||
h3 = (h3 + d) << 0;
|
||||
h4 = (h4 + e) << 0;
|
||||
h5 = (h5 + f) << 0;
|
||||
h6 = (h6 + g) << 0;
|
||||
h7 = (h7 + h) << 0;
|
||||
}
|
||||
|
||||
var hex = toHexString(h0) + toHexString(h1)+ toHexString(h2) + toHexString(h3) + toHexString(h4) + toHexString(h5) + toHexString(h6);
|
||||
@@ -116,10 +153,6 @@
|
||||
return hex;
|
||||
};
|
||||
|
||||
var rightrotate = function(x, c) {
|
||||
return (x >>> c) | (x << (32 - c));
|
||||
};
|
||||
|
||||
var toHexString = function(num) {
|
||||
var hex = '';
|
||||
for(var i = 0; i < 4; i++) {
|
||||
@@ -153,38 +186,39 @@
|
||||
return bytes;
|
||||
};
|
||||
|
||||
var getBlocksFromAscii = function(message) {
|
||||
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 blockCount = chunkCount << 4; // chunkCount * 16
|
||||
var blocks = [], i;
|
||||
for(i = 0;i < blockCount;++i) {
|
||||
blocks[i] = 0;
|
||||
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[i >> 2] |= message.charCodeAt(i) << (3 - (i & 3) << 3);
|
||||
blocks = chunks[i >> 6];
|
||||
blocks[(i & 63) >> 2] |= message.charCodeAt(i) << (3 - (i & 3) << 3);
|
||||
}
|
||||
blocks[i >> 2] |= 0x80 << (3 - (i & 3) << 3);
|
||||
blocks[blockCount - 1] = length << 3; // length * 8
|
||||
return blocks;
|
||||
blocks[(i & 63) >> 2] |= 0x80 << (3 - (i & 3) << 3);
|
||||
blocks[15] = length << 3; // length * 8
|
||||
return chunks;
|
||||
};
|
||||
|
||||
var getBlocksFromUtf8 = function(message) {
|
||||
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 blockCount = chunkCount << 4; // chunkCount * 16
|
||||
var blocks = [], i;
|
||||
for(i = 0;i < blockCount;++i) {
|
||||
blocks[i] = 0;
|
||||
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[i >> 2] |= bytes[i] << (3 - (i & 3) << 3);
|
||||
blocks = chunks[i >> 6];
|
||||
blocks[(i & 63) >> 2] |= bytes[i] << (3 - (i & 3) << 3);
|
||||
}
|
||||
blocks[i >> 2] |= 0x80 << (3 - (i & 3) << 3);
|
||||
blocks[blockCount - 1] = length << 3; // length * 8
|
||||
return blocks;
|
||||
blocks[(i & 63) >> 2] |= 0x80 << (3 - (i & 3) << 3);
|
||||
blocks[15] = length << 3; // length * 8
|
||||
return chunks;
|
||||
};
|
||||
|
||||
if(typeof(module) != 'undefined') {
|
||||
|
||||
Reference in New Issue
Block a user