@nogsantos/hash
Version:
Sha1 and Md5 hash encoder for javascript
12 lines • 8.6 kB
JavaScript
!function(t,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.Hash=r():t.Hash=r()}(window,function(){return function(t){var r={};function i(e){if(r[e])return r[e].exports;var h=r[e]={i:e,l:!1,exports:{}};return t[e].call(h.exports,h,h.exports,i),h.l=!0,h.exports}return i.m=t,i.c=r,i.d=function(t,r,e){i.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:e})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,r){if(1&r&&(t=i(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var e=Object.create(null);if(i.r(e),Object.defineProperty(e,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var h in t)i.d(e,h,function(r){return t[r]}.bind(null,h));return e},i.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(r,"a",r),r},i.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},i.p="",i(i.s=0)}([function(t,r,i){"use strict";i.r(r);class e{_rotateLeft(t,r){return t<<r|t>>>32-r}_addUnsigned(t,r){var i,e,h,n,s;return h=2147483648&t,n=2147483648&r,s=(1073741823&t)+(1073741823&r),(i=1073741824&t)&(e=1073741824&r)?2147483648^s^h^n:i|e?1073741824&s?3221225472^s^h^n:1073741824^s^h^n:s^h^n}_f(t,r,i){return t&r|~t&i}_g(t,r,i){return t&i|r&~i}_h(t,r,i){return t^r^i}_i(t,r,i){return r^(t|~i)}_ff(t,r,i,e,h,n,s){return t=this._addUnsigned(t,this._addUnsigned(this._addUnsigned(this._f(r,i,e),h),s)),this._addUnsigned(this._rotateLeft(t,n),r)}_gg(t,r,i,e,h,n,s){return t=this._addUnsigned(t,this._addUnsigned(this._addUnsigned(this._g(r,i,e),h),s)),this._addUnsigned(this._rotateLeft(t,n),r)}_hh(t,r,i,e,h,n,s){return t=this._addUnsigned(t,this._addUnsigned(this._addUnsigned(this._h(r,i,e),h),s)),this._addUnsigned(this._rotateLeft(t,n),r)}_ii(t,r,i,e,h,n,s){return t=this._addUnsigned(t,this._addUnsigned(this._addUnsigned(this._i(r,i,e),h),s)),this._addUnsigned(this._rotateLeft(t,n),r)}_convertToWordArray(t){for(var r,i=t.length,e=i+8,h=16*((e-e%64)/64+1),n=Array(h-1),s=0,a=0;a<i;)s=a%4*8,n[r=(a-a%4)/4]=n[r]|t.charCodeAt(a)<<s,a++;return s=a%4*8,n[r=(a-a%4)/4]=n[r]|128<<s,n[h-2]=i<<3,n[h-1]=i>>>29,n}_wordToHex(t){var r,i="",e="";for(r=0;r<=3;r++)i+=(e="0"+(t>>>8*r&255).toString(16)).substr(e.length-2,2);return i}_utf8Encode(t){t=t.replace(/\r\n/g,"\n");for(var r="",i=0;i<t.length;i++){var e=t.charCodeAt(i);e<128?r+=String.fromCharCode(e):e>127&e<2048?(r+=String.fromCharCode(e>>6|192),r+=String.fromCharCode(63&e|128)):(r+=String.fromCharCode(e>>12|224),r+=String.fromCharCode(e>>6&63|128),r+=String.fromCharCode(63&e|128))}return r}encode(t){if(!t)return null;let r,i,e,h,n,s,a,o,_,d=Array();for(t=this._utf8Encode(t),d=this._convertToWordArray(t),s=1732584193,a=4023233417,o=2562383102,_=271733878,r=0;r<d.length;r+=16)i=s,e=a,h=o,n=_,s=this._ff(s,a,o,_,d[r+0],7,3614090360),_=this._ff(_,s,a,o,d[r+1],12,3905402710),o=this._ff(o,_,s,a,d[r+2],17,606105819),a=this._ff(a,o,_,s,d[r+3],22,3250441966),s=this._ff(s,a,o,_,d[r+4],7,4118548399),_=this._ff(_,s,a,o,d[r+5],12,1200080426),o=this._ff(o,_,s,a,d[r+6],17,2821735955),a=this._ff(a,o,_,s,d[r+7],22,4249261313),s=this._ff(s,a,o,_,d[r+8],7,1770035416),_=this._ff(_,s,a,o,d[r+9],12,2336552879),o=this._ff(o,_,s,a,d[r+10],17,4294925233),a=this._ff(a,o,_,s,d[r+11],22,2304563134),s=this._ff(s,a,o,_,d[r+12],7,1804603682),_=this._ff(_,s,a,o,d[r+13],12,4254626195),o=this._ff(o,_,s,a,d[r+14],17,2792965006),a=this._ff(a,o,_,s,d[r+15],22,1236535329),s=this._gg(s,a,o,_,d[r+1],5,4129170786),_=this._gg(_,s,a,o,d[r+6],9,3225465664),o=this._gg(o,_,s,a,d[r+11],14,643717713),a=this._gg(a,o,_,s,d[r+0],20,3921069994),s=this._gg(s,a,o,_,d[r+5],5,3593408605),_=this._gg(_,s,a,o,d[r+10],9,38016083),o=this._gg(o,_,s,a,d[r+15],14,3634488961),a=this._gg(a,o,_,s,d[r+4],20,3889429448),s=this._gg(s,a,o,_,d[r+9],5,568446438),_=this._gg(_,s,a,o,d[r+14],9,3275163606),o=this._gg(o,_,s,a,d[r+3],14,4107603335),a=this._gg(a,o,_,s,d[r+8],20,1163531501),s=this._gg(s,a,o,_,d[r+13],5,2850285829),_=this._gg(_,s,a,o,d[r+2],9,4243563512),o=this._gg(o,_,s,a,d[r+7],14,1735328473),a=this._gg(a,o,_,s,d[r+12],20,2368359562),s=this._hh(s,a,o,_,d[r+5],4,4294588738),_=this._hh(_,s,a,o,d[r+8],11,2272392833),o=this._hh(o,_,s,a,d[r+11],16,1839030562),a=this._hh(a,o,_,s,d[r+14],23,4259657740),s=this._hh(s,a,o,_,d[r+1],4,2763975236),_=this._hh(_,s,a,o,d[r+4],11,1272893353),o=this._hh(o,_,s,a,d[r+7],16,4139469664),a=this._hh(a,o,_,s,d[r+10],23,3200236656),s=this._hh(s,a,o,_,d[r+13],4,681279174),_=this._hh(_,s,a,o,d[r+0],11,3936430074),o=this._hh(o,_,s,a,d[r+3],16,3572445317),a=this._hh(a,o,_,s,d[r+6],23,76029189),s=this._hh(s,a,o,_,d[r+9],4,3654602809),_=this._hh(_,s,a,o,d[r+12],11,3873151461),o=this._hh(o,_,s,a,d[r+15],16,530742520),a=this._hh(a,o,_,s,d[r+2],23,3299628645),s=this._ii(s,a,o,_,d[r+0],6,4096336452),_=this._ii(_,s,a,o,d[r+7],10,1126891415),o=this._ii(o,_,s,a,d[r+14],15,2878612391),a=this._ii(a,o,_,s,d[r+5],21,4237533241),s=this._ii(s,a,o,_,d[r+12],6,1700485571),_=this._ii(_,s,a,o,d[r+3],10,2399980690),o=this._ii(o,_,s,a,d[r+10],15,4293915773),a=this._ii(a,o,_,s,d[r+1],21,2240044497),s=this._ii(s,a,o,_,d[r+8],6,1873313359),_=this._ii(_,s,a,o,d[r+15],10,4264355552),o=this._ii(o,_,s,a,d[r+6],15,2734768916),a=this._ii(a,o,_,s,d[r+13],21,1309151649),s=this._ii(s,a,o,_,d[r+4],6,4149444226),_=this._ii(_,s,a,o,d[r+11],10,3174756917),o=this._ii(o,_,s,a,d[r+2],15,718787259),a=this._ii(a,o,_,s,d[r+9],21,3951481745),s=this._addUnsigned(s,i),a=this._addUnsigned(a,e),o=this._addUnsigned(o,h),_=this._addUnsigned(_,n);return(this._wordToHex(s)+this._wordToHex(a)+this._wordToHex(o)+this._wordToHex(_)).toLowerCase()}}
/**!
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
* in FIPS 180-1
* Version 2.2 Copyright Paul Johnston 2000 - 2009.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for details.
*
* Contributor: Fabricio Nogueira - 2018
*/class h{_rawToHex(t){for(var r="",i=0;i<t.length;i++){var e=t.charCodeAt(i);r+="0123456789abcdef".charAt(e>>>4&15)+"0123456789abcdef".charAt(15&e)}return r}_sha1Raw(t){return this._binaryToRaw(this._sha1Binary(this.rawToBinary(t),8*t.length))}_binaryToRaw(t){for(var r="",i=0,e=32*t.length;i<e;i+=8)r+=String.fromCharCode(t[i>>5]>>>24-i%32&255);return r}_sha1Binary(t,r){t[r>>5]|=128<<24-r%32,t[15+(r+64>>9<<4)]=r;for(var i=new Array(80),e=1732584193,h=-271733879,n=-1732584194,s=271733878,a=-1009589776,o=0,_=t.length;o<_;o+=16){for(var d=e,f=h,g=n,u=s,c=a,l=0;l<80;l++){i[l]=l<16?t[o+l]:this._rotateLeft(i[l-3]^i[l-8]^i[l-14]^i[l-16],1);var y=this._add(this._add(this._rotateLeft(e,5),this._ft(l,h,n,s)),this._add(this._add(a,i[l]),this._kt(l)));a=s,s=n,n=this._rotateLeft(h,30),h=e,e=y}e=this._add(e,d),h=this._add(h,f),n=this._add(n,g),s=this._add(s,u),a=this._add(a,c)}return[e,h,n,s,a]}_add(t,r){var i=(65535&t)+(65535&r);return(t>>16)+(r>>16)+(i>>16)<<16|65535&i}_rotateLeft(t,r){return t<<r|t>>>32-r}_ft(t,r,i,e){return t<20?r&i|~r&e:t<40?r^i^e:t<60?r&i|r&e|i&e:r^i^e}_kt(t){return t<20?1518500249:t<40?1859775393:t<60?-1894007588:-899497514}rawToBinary(t){for(var r=new Array(t.length>>2),i=0,e=r.length;i<e;i++)r[i]=0;for(i=0,e=8*t.length;i<e;i+=8)r[i>>5]|=(255&t.charCodeAt(i/8))<<24-i%32;return r}stringToRaw(t){for(var r,i,e="",h=-1,n=t.length;++h<n;)r=t.charCodeAt(h),i=h+1<n?t.charCodeAt(h+1):0,55296<=r&&r<=56319&&56320<=i&&i<=57343&&(r=65536+((1023&r)<<10)+(1023&i),++h),r<=127?e+=String.fromCharCode(r):r<=2047?e+=String.fromCharCode(192|r>>>6&31,128|63&r):r<=65535?e+=String.fromCharCode(224|r>>>12&15,128|r>>>6&63,128|63&r):r<=2097151&&(e+=String.fromCharCode(240|r>>>18&7,128|r>>>12&63,128|r>>>6&63,128|63&r));return e}hmacRaw(t,r){var i=this.rawToBinary(t);i.length>16&&(i=this._sha1Binary(i,8*t.length));for(var e=new Array(16),h=new Array(16),n=0;n<16;n++)e[n]=909522486^i[n],h[n]=1549556828^i[n];var s=this._sha1Binary(e.concat(this.rawToBinary(r)),512+8*r.length);return this._binaryToRaw(this._sha1Binary(h.concat(s),672))}encode(t){return t?this._rawToHex(this._sha1Raw(this.stringToRaw(t))):null}hexToString(t){if(!t)return null;for(var r="",i=0,e=t.length;i<e;i+=2)r+=String.fromCharCode(parseInt(t.substr(i,2),16));return r}sha1ToHex(t){return t?this._rawToHex(this._sha1Raw(this.hexToString(t))):null}}i.d(r,"Md5",function(){return e}),i.d(r,"Sha1",function(){return h})}])});
//# sourceMappingURL=hash.umd.js.map