DocumentServer/OnlineEditorsExample/OnlineEditorsExampleJS/sha256.js
2015-04-29 19:10:50 +03:00

26 lines
6.9 KiB
JavaScript

/*
A JavaScript implementation of the SHA family of hashes, as
defined in FIPS PUB 180-2 as well as the corresponding HMAC implementation
as defined in FIPS PUB 198a
Copyright Brian Turek 2008-2014
Distributed under the BSD License
See http://caligatio.github.com/jsSHA/ for more information
Several functions taken from Paul Johnston
*/
'use strict';(function(D){function r(a,d,b){var c=0,f=[0],g="",h=null,g=b||"UTF8";if("UTF8"!==g&&"UTF16"!==g)throw"encoding must be UTF8 or UTF16";if("HEX"===d){if(0!==a.length%2)throw"srcString of HEX type must be in byte increments";h=u(a);c=h.binLen;f=h.value}else if("TEXT"===d)h=v(a,g),c=h.binLen,f=h.value;else if("B64"===d)h=w(a),c=h.binLen,f=h.value;else if("BYTES"===d)h=x(a),c=h.binLen,f=h.value;else throw"inputFormat must be HEX, TEXT, B64, or BYTES";this.getHash=function(a,d,b,g){var h=null,
e=f.slice(),m=c,k;3===arguments.length?"number"!==typeof b&&(g=b,b=1):2===arguments.length&&(b=1);if(b!==parseInt(b,10)||1>b)throw"numRounds must a integer >= 1";switch(d){case "HEX":h=y;break;case "B64":h=z;break;case "BYTES":h=A;break;default:throw"format must be HEX, B64, or BYTES";}if("SHA-224"===a)for(k=0;k<b;k+=1)e=q(e,m,a),m=224;else if("SHA-256"===a)for(k=0;k<b;k+=1)e=q(e,m,a),m=256;else throw"Chosen SHA variant is not supported";return h(e,B(g))};this.getHMAC=function(a,b,d,h,l){var e,m,
k,n,C=[],s=[];e=null;switch(h){case "HEX":h=y;break;case "B64":h=z;break;case "BYTES":h=A;break;default:throw"outputFormat must be HEX, B64, or BYTES";}if("SHA-224"===d)m=64,n=224;else if("SHA-256"===d)m=64,n=256;else throw"Chosen SHA variant is not supported";if("HEX"===b)e=u(a),k=e.binLen,e=e.value;else if("TEXT"===b)e=v(a,g),k=e.binLen,e=e.value;else if("B64"===b)e=w(a),k=e.binLen,e=e.value;else if("BYTES"===b)e=x(a),k=e.binLen,e=e.value;else throw"inputFormat must be HEX, TEXT, B64, or BYTES";
a=8*m;b=m/4-1;m<k/8?(e=q(e,k,d),e[b]&=4294967040):m>k/8&&(e[b]&=4294967040);for(m=0;m<=b;m+=1)C[m]=e[m]^909522486,s[m]=e[m]^1549556828;d=q(s.concat(q(C.concat(f),a+c,d)),a+n,d);return h(d,B(l))}}function v(a,d){var b=[],c,f=[],g=0,h;if("UTF8"===d)for(h=0;h<a.length;h+=1)for(c=a.charCodeAt(h),f=[],128>c?f.push(c):2048>c?(f.push(192|c>>>6),f.push(128|c&63)):55296>c||57344<=c?f.push(224|c>>>12,128|c>>>6&63,128|c&63):(h+=1,c=65536+((c&1023)<<10|a.charCodeAt(h)&1023),f.push(240|c>>>18,128|c>>>12&63,128|
c>>>6&63,128|c&63)),c=0;c<f.length;c+=1)(g>>>2)+1>b.length&&b.push(0),b[g>>>2]|=f[c]<<24-g%4*8,g+=1;else if("UTF16"===d)for(h=0;h<a.length;h+=1)(g>>>2)+1>b.length&&b.push(0),b[g>>>2]|=a.charCodeAt(h)<<16-g%4*8,g+=2;return{value:b,binLen:8*g}}function u(a){var d=[],b=a.length,c,f;if(0!==b%2)throw"String of HEX type must be in byte increments";for(c=0;c<b;c+=2){f=parseInt(a.substr(c,2),16);if(isNaN(f))throw"String of HEX type contains invalid characters";d[c>>>3]|=f<<24-c%8*4}return{value:d,binLen:4*
b}}function x(a){var d=[],b,c;for(c=0;c<a.length;c+=1)b=a.charCodeAt(c),(c>>>2)+1>d.length&&d.push(0),d[c>>>2]|=b<<24-c%4*8;return{value:d,binLen:8*a.length}}function w(a){var d=[],b=0,c,f,g,h,l;if(-1===a.search(/^[a-zA-Z0-9=+\/]+$/))throw"Invalid character in base-64 string";c=a.indexOf("=");a=a.replace(/\=/g,"");if(-1!==c&&c<a.length)throw"Invalid '=' found in base-64 string";for(f=0;f<a.length;f+=4){l=a.substr(f,4);for(g=h=0;g<l.length;g+=1)c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(l[g]),
h|=c<<18-6*g;for(g=0;g<l.length-1;g+=1)d[b>>2]|=(h>>>16-8*g&255)<<24-b%4*8,b+=1}return{value:d,binLen:8*b}}function y(a,d){var b="",c=4*a.length,f,g;for(f=0;f<c;f+=1)g=a[f>>>2]>>>8*(3-f%4),b+="0123456789abcdef".charAt(g>>>4&15)+"0123456789abcdef".charAt(g&15);return d.outputUpper?b.toUpperCase():b}function z(a,d){var b="",c=4*a.length,f,g,h;for(f=0;f<c;f+=3)for(h=(a[f>>>2]>>>8*(3-f%4)&255)<<16|(a[f+1>>>2]>>>8*(3-(f+1)%4)&255)<<8|a[f+2>>>2]>>>8*(3-(f+2)%4)&255,g=0;4>g;g+=1)b=8*f+6*g<=32*a.length?b+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(h>>>6*(3-g)&63):b+d.b64Pad;return b}function A(a){var d="",b=4*a.length,c,f;for(c=0;c<b;c+=1)f=a[c>>>2]>>>8*(3-c%4)&255,d+=String.fromCharCode(f);return d}function B(a){var d={outputUpper:!1,b64Pad:"="};try{a.hasOwnProperty("outputUpper")&&(d.outputUpper=a.outputUpper),a.hasOwnProperty("b64Pad")&&(d.b64Pad=a.b64Pad)}catch(b){}if("boolean"!==typeof d.outputUpper)throw"Invalid outputUpper formatting option";if("string"!==typeof d.b64Pad)throw"Invalid b64Pad formatting option";
return d}function l(a,d){return a>>>d|a<<32-d}function I(a,d,b){return a&d^~a&b}function J(a,d,b){return a&d^a&b^d&b}function K(a){return l(a,2)^l(a,13)^l(a,22)}function L(a){return l(a,6)^l(a,11)^l(a,25)}function M(a){return l(a,7)^l(a,18)^a>>>3}function N(a){return l(a,17)^l(a,19)^a>>>10}function O(a,d){var b=(a&65535)+(d&65535);return((a>>>16)+(d>>>16)+(b>>>16)&65535)<<16|b&65535}function P(a,d,b,c){var f=(a&65535)+(d&65535)+(b&65535)+(c&65535);return((a>>>16)+(d>>>16)+(b>>>16)+(c>>>16)+(f>>>16)&
65535)<<16|f&65535}function Q(a,d,b,c,f){var g=(a&65535)+(d&65535)+(b&65535)+(c&65535)+(f&65535);return((a>>>16)+(d>>>16)+(b>>>16)+(c>>>16)+(f>>>16)+(g>>>16)&65535)<<16|g&65535}function q(a,d,b){var c,f,g,h,l,q,r,E,u,e,m,k,n,C,s,p,v,w,x,y,z,A,B,F,G,t=[],H,D=[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];e=[3238371032,914150663,812702999,4144912697,4290775857,1750603025,
1694076839,3204075428];c=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225];if("SHA-224"===b||"SHA-256"===b)m=64,C=16,s=1,G=Number,p=O,v=P,w=Q,x=M,y=N,z=K,A=L,F=J,B=I,e="SHA-224"===b?e:c;else throw"Unexpected error in SHA-2 implementation";a[d>>>5]|=128<<24-d%32;a[(d+65>>>9<<4)+15]=d;H=a.length;for(k=0;k<H;k+=C){d=e[0];c=e[1];f=e[2];g=e[3];h=e[4];l=e[5];q=e[6];r=e[7];for(n=0;n<m;n+=1)t[n]=16>n?new G(a[n*s+k],a[n*s+k+1]):v(y(t[n-2]),t[n-7],x(t[n-15]),t[n-16]),
E=w(r,A(h),B(h,l,q),D[n],t[n]),u=p(z(d),F(d,c,f)),r=q,q=l,l=h,h=p(g,E),g=f,f=c,c=d,d=p(E,u);e[0]=p(d,e[0]);e[1]=p(c,e[1]);e[2]=p(f,e[2]);e[3]=p(g,e[3]);e[4]=p(h,e[4]);e[5]=p(l,e[5]);e[6]=p(q,e[6]);e[7]=p(r,e[7])}if("SHA-224"===b)a=[e[0],e[1],e[2],e[3],e[4],e[5],e[6]];else if("SHA-256"===b)a=e;else throw"Unexpected error in SHA-2 implementation";return a}"function"===typeof define&&define.amd?define(function(){return r}):"undefined"!==typeof exports?"undefined"!==typeof module&&module.exports?module.exports=
exports=r:exports=r:D.jsSHA=r})(this);