-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsha1.js
More file actions
21 lines (19 loc) · 4.85 KB
/
sha1.js
File metadata and controls
21 lines (19 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
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-2013
Distributed under the BSD License
See http://caligatio.github.com/jsSHA/ for more information
Several functions taken from Paul Johnston
*/
'use strict';(function(B){function n(a,f,b){var e=0,c=[0],d="",g=null,d=b||"UTF8";if("UTF8"!==d&&"UTF16"!==d)throw"encoding must be UTF8 or UTF16";if("HEX"===f){if(0!==a.length%2)throw"srcString of HEX type must be in byte increments";g=v(a);e=g.binLen;c=g.value}else if("ASCII"===f||"TEXT"===f)g=w(a,d),e=g.binLen,c=g.value;else if("B64"===f)g=x(a),e=g.binLen,c=g.value;else throw"inputFormat must be HEX, TEXT, ASCII, or B64";this.getHash=function(d,a,b,f){var g=null,h=c.slice(),l=e,p;3===arguments.length?
"number"!==typeof b&&(f=b,b=1):2===arguments.length&&(b=1);if(b!==parseInt(b,10)||1>b)throw"numRounds must a integer >= 1";switch(a){case "HEX":g=y;break;case "B64":g=z;break;default:throw"format must be HEX or B64";}if("SHA-1"===d)for(p=0;p<b;p++)h=s(h,l),l=160;else throw"Chosen SHA variant is not supported";return g(h,A(f))};this.getHMAC=function(b,a,f,g,q){var h,l,p,r,n=[],t=[];h=null;switch(g){case "HEX":g=y;break;case "B64":g=z;break;default:throw"outputFormat must be HEX or B64";}if("SHA-1"===
f)l=64,r=160;else throw"Chosen SHA variant is not supported";if("HEX"===a)h=v(b),p=h.binLen,h=h.value;else if("ASCII"===a||"TEXT"===a)h=w(b,d),p=h.binLen,h=h.value;else if("B64"===a)h=x(b),p=h.binLen,h=h.value;else throw"inputFormat must be HEX, TEXT, ASCII, or B64";b=8*l;a=l/4-1;if(l<p/8){if("SHA-1"===f)h=s(h,p);else throw"Unexpected error in HMAC implementation";h[a]&=4294967040}else l>p/8&&(h[a]&=4294967040);for(l=0;l<=a;l+=1)n[l]=h[l]^909522486,t[l]=h[l]^1549556828;if("SHA-1"===f)f=s(t.concat(s(n.concat(c),
b+e)),b+r);else throw"Unexpected error in HMAC implementation";return g(f,A(q))}}function w(a,f){var b=[],e,c=[],d=0,g;if("UTF8"===f)for(g=0;g<a.length;g+=1)for(e=a.charCodeAt(g),c=[],2048<e?(c[0]=224|(e&61440)>>>12,c[1]=128|(e&4032)>>>6,c[2]=128|e&63):128<e?(c[0]=192|(e&1984)>>>6,c[1]=128|e&63):c[0]=e,e=0;e<c.length;e+=1)b[d>>>2]|=c[e]<<24-d%4*8,d+=1;else if("UTF16"===f)for(g=0;g<a.length;g+=1)b[d>>>2]|=a.charCodeAt(g)<<16-d%4*8,d+=2;return{value:b,binLen:8*d}}function v(a){var f=[],b=a.length,e,
c;if(0!==b%2)throw"String of HEX type must be in byte increments";for(e=0;e<b;e+=2){c=parseInt(a.substr(e,2),16);if(isNaN(c))throw"String of HEX type contains invalid characters";f[e>>>3]|=c<<24-e%8*4}return{value:f,binLen:4*b}}function x(a){var f=[],b=0,e,c,d,g,m;if(-1===a.search(/^[a-zA-Z0-9=+\/]+$/))throw"Invalid character in base-64 string";e=a.indexOf("=");a=a.replace(/\=/g,"");if(-1!==e&&e<a.length)throw"Invalid '=' found in base-64 string";for(c=0;c<a.length;c+=4){m=a.substr(c,4);for(d=g=0;d<
m.length;d+=1)e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(m[d]),g|=e<<18-6*d;for(d=0;d<m.length-1;d+=1)f[b>>2]|=(g>>>16-8*d&255)<<24-b%4*8,b+=1}return{value:f,binLen:8*b}}function y(a,f){var b="",e=4*a.length,c,d;for(c=0;c<e;c+=1)d=a[c>>>2]>>>8*(3-c%4),b+="0123456789abcdef".charAt(d>>>4&15)+"0123456789abcdef".charAt(d&15);return f.outputUpper?b.toUpperCase():b}function z(a,f){var b="",e=4*a.length,c,d,g;for(c=0;c<e;c+=3)for(g=(a[c>>>2]>>>8*(3-c%4)&255)<<16|(a[c+1>>>
2]>>>8*(3-(c+1)%4)&255)<<8|a[c+2>>>2]>>>8*(3-(c+2)%4)&255,d=0;4>d;d+=1)b=8*c+6*d<=32*a.length?b+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(g>>>6*(3-d)&63):b+f.b64Pad;return b}function A(a){var f={outputUpper:!1,b64Pad:"="};try{a.hasOwnProperty("outputUpper")&&(f.outputUpper=a.outputUpper),a.hasOwnProperty("b64Pad")&&(f.b64Pad=a.b64Pad)}catch(b){}if("boolean"!==typeof f.outputUpper)throw"Invalid outputUpper formatting option";if("string"!==typeof f.b64Pad)throw"Invalid b64Pad formatting option";
return f}function q(a,f){return a<<f|a>>>32-f}function r(a,f){var b=(a&65535)+(f&65535);return((a>>>16)+(f>>>16)+(b>>>16)&65535)<<16|b&65535}function t(a,f,b,e,c){var d=(a&65535)+(f&65535)+(b&65535)+(e&65535)+(c&65535);return((a>>>16)+(f>>>16)+(b>>>16)+(e>>>16)+(c>>>16)+(d>>>16)&65535)<<16|d&65535}function s(a,f){var b=[],e,c,d,g,m,n,u,k,s,h=[1732584193,4023233417,2562383102,271733878,3285377520];a[f>>>5]|=128<<24-f%32;a[(f+65>>>9<<4)+15]=f;s=a.length;for(u=0;u<s;u+=16){e=h[0];c=h[1];d=h[2];g=h[3];
m=h[4];for(k=0;80>k;k+=1)b[k]=16>k?a[k+u]:q(b[k-3]^b[k-8]^b[k-14]^b[k-16],1),n=20>k?t(q(e,5),c&d^~c&g,m,1518500249,b[k]):40>k?t(q(e,5),c^d^g,m,1859775393,b[k]):60>k?t(q(e,5),c&d^c&g^d&g,m,2400959708,b[k]):t(q(e,5),c^d^g,m,3395469782,b[k]),m=g,g=d,d=q(c,30),c=e,e=n;h[0]=r(e,h[0]);h[1]=r(c,h[1]);h[2]=r(d,h[2]);h[3]=r(g,h[3]);h[4]=r(m,h[4])}return h}"function"===typeof define&&define.amd?define(function(){return n}):"undefined"!==typeof exports?"undefined"!==typeof module&&module.exports?module.exports=
exports=n:exports=n:B.jsSHA=n})(this);