@cryptography/sha256
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -18,3 +18,3 @@ "use strict"; | ||
// K table for SHA-256 | ||
var _k = [ | ||
var _k = new Uint32Array([ | ||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, | ||
@@ -36,3 +36,3 @@ 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, | ||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, | ||
]; | ||
]); | ||
// padding | ||
@@ -62,3 +62,3 @@ var _padding = String.fromCharCode(128); | ||
// Array to use to store words. | ||
var words = new Array(64); | ||
var words = new Uint32Array(64); | ||
// SHA-256 state contains eight 32-bit integers | ||
@@ -73,22 +73,7 @@ var h1 = 0x6A09E667; | ||
var h8 = 0x5BE0CD19; | ||
for (var p = 0; p < data.length - (data.length % 64); p += 64) { | ||
// the w array will be populated with sixteen 32-bit big-endian words | ||
// and then extended into 64 32-bit words according to SHA-256 | ||
for (i = 0; i < 16; i += 1) { | ||
words[i] = strToInt32(data, p + i * 4); | ||
} | ||
for (; i < 64; i += 1) { | ||
// XOR word 2 words ago rot right 17, rot right 19, shft right 10 | ||
t1 = words[i - 2]; | ||
t1 = ((t1 >>> 17) | (t1 << 15)) | ||
^ ((t1 >>> 19) | (t1 << 13)) | ||
^ (t1 >>> 10); | ||
// XOR word 15 words ago rot right 7, rot right 18, shft right 3 | ||
t2 = words[i - 15]; | ||
t2 = ((t2 >>> 7) | (t2 << 25)) | ||
^ ((t2 >>> 18) | (t2 << 14)) | ||
^ (t2 >>> 3); | ||
// sum(t1, word 7 ago, t2, word 16 ago) modulo 2^32 | ||
words[i] = (t1 + words[i - 7] + t2 + words[i - 16]) | 0; | ||
} | ||
var len = data.length; | ||
var p = 0; | ||
var ni = 64; | ||
// While decrementing loop is much faster than for | ||
while (len >= 64) { | ||
// initialize hash value for this chunk | ||
@@ -104,4 +89,27 @@ // [a, b, c, d, e, f, g, h] = nextState; | ||
h = h8; | ||
// round function | ||
for (i = 0; i < 64; i += 1) { | ||
ni = 64; | ||
i = 0; | ||
// the w array will be populated with sixteen 32-bit big-endian words | ||
// and then extended into 64 32-bit words according to SHA-256 | ||
while (ni--) { | ||
i = 63 - ni; | ||
if (i < 16) { | ||
words[i] = strToInt32(data, p); | ||
p += 4; | ||
} | ||
else { | ||
// XOR word 2 words ago rot right 17, rot right 19, shft right 10 | ||
t1 = words[i - 2]; | ||
t1 = ((t1 >>> 17) | (t1 << 15)) | ||
^ ((t1 >>> 19) | (t1 << 13)) | ||
^ (t1 >>> 10); | ||
// XOR word 15 words ago rot right 7, rot right 18, shft right 3 | ||
t2 = words[i - 15]; | ||
t2 = ((t2 >>> 7) | (t2 << 25)) | ||
^ ((t2 >>> 18) | (t2 << 14)) | ||
^ (t2 >>> 3); | ||
// sum(t1, word 7 ago, t2, word 16 ago) modulo 2^32 | ||
words[i] = (t1 + words[i - 7] + t2 + words[i - 16]); | ||
} | ||
// Round Function | ||
// Sum1(e) | ||
@@ -131,3 +139,3 @@ s1 = ((e >>> 6) | (e << 26)) | ||
} | ||
// update hash state | ||
// update hash state | ||
h1 = (h1 + a) | 0; | ||
@@ -141,2 +149,3 @@ h2 = (h2 + b) | 0; | ||
h8 = (h8 + h) | 0; | ||
len -= 64; | ||
} | ||
@@ -154,7 +163,8 @@ return { | ||
// true 64-bit message length as two 32-bit ints | ||
var len64 = [(len / 0x100000000) >>> 0, len >>> 0]; | ||
var len64hi = (len / 0x100000000) >>> 0; | ||
var len64lo = len >>> 0; | ||
var pad = message | ||
+ _padding.substr(0, 64 - ((len64[1] + 8) & 0x3F)) | ||
+ int32ToStr((len64[0] << 3) | (len64[0] >>> 28)) | ||
+ int32ToStr(len64[1] << 3); | ||
+ _padding.substr(0, 64 - ((len64lo + 8) & 0x3F)) | ||
+ int32ToStr((len64hi << 3) | (len64hi >>> 28)) | ||
+ int32ToStr(len64lo << 3); | ||
var state = update(pad); | ||
@@ -161,0 +171,0 @@ return int32ToStr(state.h1) |
@@ -1,1 +0,1 @@ | ||
"use strict";function strToInt32(r,t){return r.charCodeAt(t)<<24^r.charCodeAt(t+1)<<16^r.charCodeAt(t+2)<<8^r.charCodeAt(t+3)}function int32ToStr(r){return String.fromCharCode(r>>24&255)+String.fromCharCode(r>>16&255)+String.fromCharCode(r>>8&255)+String.fromCharCode(255&r)}Object.defineProperty(exports,"__esModule",{value:!0});for(var _k=[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],_padding=String.fromCharCode(128),i=0;i<64;i+=1)_padding+=String.fromCharCode(0);function update(r){for(var t,o,n,e,h,i,a,d,f,S,C,u,T=0,g=new Array(64),s=1779033703,c=3144134277,p=1013904242,_=2773480762,l=1359893119,m=2600822924,A=528734635,v=1541459225,b=0;b<r.length-r.length%64;b+=64){for(T=0;T<16;T+=1)g[T]=strToInt32(r,b+4*T);for(;T<64;T+=1)t=((t=g[T-2])>>>17|t<<15)^(t>>>19|t<<13)^t>>>10,o=((o=g[T-15])>>>7|o<<25)^(o>>>18|o<<14)^o>>>3,g[T]=t+g[T-7]+o+g[T-16]|0;for(h=s,i=c,a=p,d=_,f=l,S=m,C=A,u=v,T=0;T<64;T+=1)n=(h>>>2|h<<30)^(h>>>13|h<<19)^(h>>>22|h<<10),e=h&i|a&(h^i),t=u+((f>>>6|f<<26)^(f>>>11|f<<21)^(f>>>25|f<<7))+(C^f&(S^C))+_k[T]+g[T],u=C,C=S,S=f,f=d+t|0,d=a,a=i,i=h,h=t+(o=n+e)|0;s=s+h|0,c=c+i|0,p=p+a|0,_=_+d|0,l=l+f|0,m=m+S|0,A=A+C|0,v=v+u|0}return{h1:s,h2:c,h3:p,h4:_,h5:l,h6:m,h7:A,h8:v}}function sha256(r){var t=r.length,o=[t/4294967296>>>0,t>>>0],n=update(r+_padding.substr(0,64-(o[1]+8&63))+int32ToStr(o[0]<<3|o[0]>>>28)+int32ToStr(o[1]<<3));return int32ToStr(n.h1)+int32ToStr(n.h2)+int32ToStr(n.h3)+int32ToStr(n.h4)+int32ToStr(n.h5)+int32ToStr(n.h6)+int32ToStr(n.h7)+int32ToStr(n.h8)}exports.default=sha256; | ||
"use strict";function strToInt32(r,t){return r.charCodeAt(t)<<24^r.charCodeAt(t+1)<<16^r.charCodeAt(t+2)<<8^r.charCodeAt(t+3)}function int32ToStr(r){return String.fromCharCode(r>>24&255)+String.fromCharCode(r>>16&255)+String.fromCharCode(r>>8&255)+String.fromCharCode(255&r)}Object.defineProperty(exports,"__esModule",{value:!0});for(var _k=new Uint32Array([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]),_padding=String.fromCharCode(128),i=0;i<64;i+=1)_padding+=String.fromCharCode(0);function update(r){for(var t,n,o,e,i,h,a,d,S,C,f,u,T=0,g=new Uint32Array(64),s=1779033703,c=3144134277,p=1013904242,_=2773480762,m=1359893119,A=2600822924,l=528734635,v=1541459225,y=r.length,b=0,k=64;64<=y;){for(i=s,h=c,a=p,d=_,S=m,C=A,f=l,u=v,k=64,T=0;k--;)(T=63-k)<16?(g[T]=strToInt32(r,b),b+=4):(t=((t=g[T-2])>>>17|t<<15)^(t>>>19|t<<13)^t>>>10,n=((n=g[T-15])>>>7|n<<25)^(n>>>18|n<<14)^n>>>3,g[T]=t+g[T-7]+n+g[T-16]),o=(i>>>2|i<<30)^(i>>>13|i<<19)^(i>>>22|i<<10),e=i&h|a&(i^h),t=u+((S>>>6|S<<26)^(S>>>11|S<<21)^(S>>>25|S<<7))+(f^S&(C^f))+_k[T]+g[T],u=f,f=C,C=S,S=d+t|0,d=a,a=h,h=i,i=t+(n=o+e)|0;s=s+i|0,c=c+h|0,p=p+a|0,_=_+d|0,m=m+S|0,A=A+C|0,l=l+f|0,v=v+u|0,y-=64}return{h1:s,h2:c,h3:p,h4:_,h5:m,h6:A,h7:l,h8:v}}function sha256(r){var t=r.length,n=t/4294967296>>>0,o=t>>>0,e=update(r+_padding.substr(0,64-(8+o&63))+int32ToStr(n<<3|n>>>28)+int32ToStr(o<<3));return int32ToStr(e.h1)+int32ToStr(e.h2)+int32ToStr(e.h3)+int32ToStr(e.h4)+int32ToStr(e.h5)+int32ToStr(e.h6)+int32ToStr(e.h7)+int32ToStr(e.h8)}exports.default=sha256; |
{ | ||
"name": "@cryptography/sha256", | ||
"description": "https://github.com/js-cryptography/sha256", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"author": "Konstantin Darutkin", | ||
@@ -6,0 +6,0 @@ "scripts": { |
# @cryptography/sha256 | ||
Fast SHA-256 implementation for JavaScript | ||
![](https://img.shields.io/bundlephobia/minzip/@cryptography/sha256) ![](https://img.shields.io/codecov/c/github/js-cryptography/sha256?token=617017dc35344eb6b4637420457746c8) ![](https://img.shields.io/travis/js-cryptography/sha256) | ||
Synchronous SHA-256 implementation for JavaScript | ||
* Blazing fast | ||
* Lightweight without any dependencies | ||
* Typed | ||
## Setup | ||
``` | ||
npm install @cryptography/sha256 | ||
``` | ||
``` | ||
yarn add @cryptography/sha256 | ||
``` | ||
## Usage | ||
@@ -5,0 +19,0 @@ ```js |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27717
176
23