js-sha1
Advanced tools
Comparing version 0.6.0 to 0.7.0
/* | ||
* [js-sha1]{@link https://github.com/emn178/js-sha1} | ||
* | ||
* @version 0.6.0 | ||
* @version 0.7.0 | ||
* @author Chen, Yi-Cyuan [emn178@gmail.com] | ||
* @copyright Chen, Yi-Cyuan 2014-2017 | ||
* @copyright Chen, Yi-Cyuan 2014-2024 | ||
* @license MIT | ||
*/ | ||
!function(){"use strict";function t(t){t?(f[0]=f[16]=f[1]=f[2]=f[3]=f[4]=f[5]=f[6]=f[7]=f[8]=f[9]=f[10]=f[11]=f[12]=f[13]=f[14]=f[15]=0,this.blocks=f):this.blocks=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],this.h0=1732584193,this.h1=4023233417,this.h2=2562383102,this.h3=271733878,this.h4=3285377520,this.block=this.start=this.bytes=this.hBytes=0,this.finalized=this.hashed=!1,this.first=!0}var h="object"==typeof window?window:{},s=!h.JS_SHA1_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node;s&&(h=global);var i=!h.JS_SHA1_NO_COMMON_JS&&"object"==typeof module&&module.exports,e="function"==typeof define&&define.amd,r="0123456789abcdef".split(""),o=[-2147483648,8388608,32768,128],n=[24,16,8,0],a=["hex","array","digest","arrayBuffer"],f=[],u=function(h){return function(s){return new t(!0).update(s)[h]()}},c=function(){var h=u("hex");s&&(h=p(h)),h.create=function(){return new t},h.update=function(t){return h.create().update(t)};for(var i=0;i<a.length;++i){var e=a[i];h[e]=u(e)}return h},p=function(t){var h=eval("require('crypto')"),s=eval("require('buffer').Buffer"),i=function(i){if("string"==typeof i)return h.createHash("sha1").update(i,"utf8").digest("hex");if(i.constructor===ArrayBuffer)i=new Uint8Array(i);else if(void 0===i.length)return t(i);return h.createHash("sha1").update(new s(i)).digest("hex")};return i};t.prototype.update=function(t){if(!this.finalized){var s="string"!=typeof t;s&&t.constructor===h.ArrayBuffer&&(t=new Uint8Array(t));for(var i,e,r=0,o=t.length||0,a=this.blocks;r<o;){if(this.hashed&&(this.hashed=!1,a[0]=this.block,a[16]=a[1]=a[2]=a[3]=a[4]=a[5]=a[6]=a[7]=a[8]=a[9]=a[10]=a[11]=a[12]=a[13]=a[14]=a[15]=0),s)for(e=this.start;r<o&&e<64;++r)a[e>>2]|=t[r]<<n[3&e++];else for(e=this.start;r<o&&e<64;++r)(i=t.charCodeAt(r))<128?a[e>>2]|=i<<n[3&e++]:i<2048?(a[e>>2]|=(192|i>>6)<<n[3&e++],a[e>>2]|=(128|63&i)<<n[3&e++]):i<55296||i>=57344?(a[e>>2]|=(224|i>>12)<<n[3&e++],a[e>>2]|=(128|i>>6&63)<<n[3&e++],a[e>>2]|=(128|63&i)<<n[3&e++]):(i=65536+((1023&i)<<10|1023&t.charCodeAt(++r)),a[e>>2]|=(240|i>>18)<<n[3&e++],a[e>>2]|=(128|i>>12&63)<<n[3&e++],a[e>>2]|=(128|i>>6&63)<<n[3&e++],a[e>>2]|=(128|63&i)<<n[3&e++]);this.lastByteIndex=e,this.bytes+=e-this.start,e>=64?(this.block=a[16],this.start=e-64,this.hash(),this.hashed=!0):this.start=e}return this.bytes>4294967295&&(this.hBytes+=this.bytes/4294967296<<0,this.bytes=this.bytes%4294967296),this}},t.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,h=this.lastByteIndex;t[16]=this.block,t[h>>2]|=o[3&h],this.block=t[16],h>=56&&(this.hashed||this.hash(),t[0]=this.block,t[16]=t[1]=t[2]=t[3]=t[4]=t[5]=t[6]=t[7]=t[8]=t[9]=t[10]=t[11]=t[12]=t[13]=t[14]=t[15]=0),t[14]=this.hBytes<<3|this.bytes>>>29,t[15]=this.bytes<<3,this.hash()}},t.prototype.hash=function(){var t,h,s=this.h0,i=this.h1,e=this.h2,r=this.h3,o=this.h4,n=this.blocks;for(t=16;t<80;++t)h=n[t-3]^n[t-8]^n[t-14]^n[t-16],n[t]=h<<1|h>>>31;for(t=0;t<20;t+=5)s=(h=(i=(h=(e=(h=(r=(h=(o=(h=s<<5|s>>>27)+(i&e|~i&r)+o+1518500249+n[t]<<0)<<5|o>>>27)+(s&(i=i<<30|i>>>2)|~s&e)+r+1518500249+n[t+1]<<0)<<5|r>>>27)+(o&(s=s<<30|s>>>2)|~o&i)+e+1518500249+n[t+2]<<0)<<5|e>>>27)+(r&(o=o<<30|o>>>2)|~r&s)+i+1518500249+n[t+3]<<0)<<5|i>>>27)+(e&(r=r<<30|r>>>2)|~e&o)+s+1518500249+n[t+4]<<0,e=e<<30|e>>>2;for(;t<40;t+=5)s=(h=(i=(h=(e=(h=(r=(h=(o=(h=s<<5|s>>>27)+(i^e^r)+o+1859775393+n[t]<<0)<<5|o>>>27)+(s^(i=i<<30|i>>>2)^e)+r+1859775393+n[t+1]<<0)<<5|r>>>27)+(o^(s=s<<30|s>>>2)^i)+e+1859775393+n[t+2]<<0)<<5|e>>>27)+(r^(o=o<<30|o>>>2)^s)+i+1859775393+n[t+3]<<0)<<5|i>>>27)+(e^(r=r<<30|r>>>2)^o)+s+1859775393+n[t+4]<<0,e=e<<30|e>>>2;for(;t<60;t+=5)s=(h=(i=(h=(e=(h=(r=(h=(o=(h=s<<5|s>>>27)+(i&e|i&r|e&r)+o-1894007588+n[t]<<0)<<5|o>>>27)+(s&(i=i<<30|i>>>2)|s&e|i&e)+r-1894007588+n[t+1]<<0)<<5|r>>>27)+(o&(s=s<<30|s>>>2)|o&i|s&i)+e-1894007588+n[t+2]<<0)<<5|e>>>27)+(r&(o=o<<30|o>>>2)|r&s|o&s)+i-1894007588+n[t+3]<<0)<<5|i>>>27)+(e&(r=r<<30|r>>>2)|e&o|r&o)+s-1894007588+n[t+4]<<0,e=e<<30|e>>>2;for(;t<80;t+=5)s=(h=(i=(h=(e=(h=(r=(h=(o=(h=s<<5|s>>>27)+(i^e^r)+o-899497514+n[t]<<0)<<5|o>>>27)+(s^(i=i<<30|i>>>2)^e)+r-899497514+n[t+1]<<0)<<5|r>>>27)+(o^(s=s<<30|s>>>2)^i)+e-899497514+n[t+2]<<0)<<5|e>>>27)+(r^(o=o<<30|o>>>2)^s)+i-899497514+n[t+3]<<0)<<5|i>>>27)+(e^(r=r<<30|r>>>2)^o)+s-899497514+n[t+4]<<0,e=e<<30|e>>>2;this.h0=this.h0+s<<0,this.h1=this.h1+i<<0,this.h2=this.h2+e<<0,this.h3=this.h3+r<<0,this.h4=this.h4+o<<0},t.prototype.hex=function(){this.finalize();var t=this.h0,h=this.h1,s=this.h2,i=this.h3,e=this.h4;return r[t>>28&15]+r[t>>24&15]+r[t>>20&15]+r[t>>16&15]+r[t>>12&15]+r[t>>8&15]+r[t>>4&15]+r[15&t]+r[h>>28&15]+r[h>>24&15]+r[h>>20&15]+r[h>>16&15]+r[h>>12&15]+r[h>>8&15]+r[h>>4&15]+r[15&h]+r[s>>28&15]+r[s>>24&15]+r[s>>20&15]+r[s>>16&15]+r[s>>12&15]+r[s>>8&15]+r[s>>4&15]+r[15&s]+r[i>>28&15]+r[i>>24&15]+r[i>>20&15]+r[i>>16&15]+r[i>>12&15]+r[i>>8&15]+r[i>>4&15]+r[15&i]+r[e>>28&15]+r[e>>24&15]+r[e>>20&15]+r[e>>16&15]+r[e>>12&15]+r[e>>8&15]+r[e>>4&15]+r[15&e]},t.prototype.toString=t.prototype.hex,t.prototype.digest=function(){this.finalize();var t=this.h0,h=this.h1,s=this.h2,i=this.h3,e=this.h4;return[t>>24&255,t>>16&255,t>>8&255,255&t,h>>24&255,h>>16&255,h>>8&255,255&h,s>>24&255,s>>16&255,s>>8&255,255&s,i>>24&255,i>>16&255,i>>8&255,255&i,e>>24&255,e>>16&255,e>>8&255,255&e]},t.prototype.array=t.prototype.digest,t.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(20),h=new DataView(t);return h.setUint32(0,this.h0),h.setUint32(4,this.h1),h.setUint32(8,this.h2),h.setUint32(12,this.h3),h.setUint32(16,this.h4),t};var y=c();i?module.exports=y:(h.sha1=y,e&&define(function(){return y}))}(); | ||
!function(){"use strict";function t(t){t?(l[0]=l[16]=l[1]=l[2]=l[3]=l[4]=l[5]=l[6]=l[7]=l[8]=l[9]=l[10]=l[11]=l[12]=l[13]=l[14]=l[15]=0,this.blocks=l):this.blocks=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],this.h0=1732584193,this.h1=4023233417,this.h2=2562383102,this.h3=271733878,this.h4=3285377520,this.block=this.start=this.bytes=this.hBytes=0,this.finalized=this.hashed=!1,this.first=!0}function r(r,e){var i,h=v(r);if(r=h[0],h[1]){var s,n=[],o=r.length,a=0;for(i=0;i<o;++i)(s=r.charCodeAt(i))<128?n[a++]=s:s<2048?(n[a++]=192|s>>>6,n[a++]=128|63&s):s<55296||s>=57344?(n[a++]=224|s>>>12,n[a++]=128|s>>>6&63,n[a++]=128|63&s):(s=65536+((1023&s)<<10|1023&r.charCodeAt(++i)),n[a++]=240|s>>>18,n[a++]=128|s>>>12&63,n[a++]=128|s>>>6&63,n[a++]=128|63&s);r=n}r.length>64&&(r=new t(!0).update(r).array());var f=[],u=[];for(i=0;i<64;++i){var c=r[i]||0;f[i]=92^c,u[i]=54^c}t.call(this,e),this.update(u),this.oKeyPad=f,this.inner=!0,this.sharedMemory=e}var e="input is invalid type",i="object"==typeof window,h=i?window:{};h.JS_SHA1_NO_WINDOW&&(i=!1);var s=!i&&"object"==typeof self,n=!h.JS_SHA1_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node;n?h=global:s&&(h=self);var o=!h.JS_SHA1_NO_COMMON_JS&&"object"==typeof module&&module.exports,a="function"==typeof define&&define.amd,f=!h.JS_SHA1_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,u="0123456789abcdef".split(""),c=[-2147483648,8388608,32768,128],y=[24,16,8,0],p=["hex","array","digest","arrayBuffer"],l=[],d=Array.isArray;!h.JS_SHA1_NO_NODE_JS&&d||(d=function(t){return"[object Array]"===Object.prototype.toString.call(t)});var b=ArrayBuffer.isView;!f||!h.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW&&b||(b=function(t){return"object"==typeof t&&t.buffer&&t.buffer.constructor===ArrayBuffer});var v=function(t){var r=typeof t;if("string"===r)return[t,!0];if("object"!==r||null===t)throw new Error(e);if(f&&t.constructor===ArrayBuffer)return[new Uint8Array(t),!1];if(!d(t)&&!b(t))throw new Error(e);return[t,!1]},_=function(r){return function(e){return new t(!0).update(e)[r]()}},A=function(t){var r,i=require("crypto"),s=require("buffer").Buffer;r=s.from&&!h.JS_SHA1_NO_BUFFER_FROM?s.from:function(t){return new s(t)};return function(h){if("string"==typeof h)return i.createHash("sha1").update(h,"utf8").digest("hex");if(null===h||void 0===h)throw new Error(e);return h.constructor===ArrayBuffer&&(h=new Uint8Array(h)),d(h)||b(h)||h.constructor===s?i.createHash("sha1").update(r(h)).digest("hex"):t(h)}},w=function(t){return function(e,i){return new r(e,!0).update(i)[t]()}};t.prototype.update=function(t){if(this.finalized)throw new Error("finalize already called");var r=v(t);t=r[0];for(var e,i,h=r[1],s=0,n=t.length||0,o=this.blocks;s<n;){if(this.hashed&&(this.hashed=!1,o[0]=this.block,this.block=o[16]=o[1]=o[2]=o[3]=o[4]=o[5]=o[6]=o[7]=o[8]=o[9]=o[10]=o[11]=o[12]=o[13]=o[14]=o[15]=0),h)for(i=this.start;s<n&&i<64;++s)(e=t.charCodeAt(s))<128?o[i>>>2]|=e<<y[3&i++]:e<2048?(o[i>>>2]|=(192|e>>>6)<<y[3&i++],o[i>>>2]|=(128|63&e)<<y[3&i++]):e<55296||e>=57344?(o[i>>>2]|=(224|e>>>12)<<y[3&i++],o[i>>>2]|=(128|e>>>6&63)<<y[3&i++],o[i>>>2]|=(128|63&e)<<y[3&i++]):(e=65536+((1023&e)<<10|1023&t.charCodeAt(++s)),o[i>>>2]|=(240|e>>>18)<<y[3&i++],o[i>>>2]|=(128|e>>>12&63)<<y[3&i++],o[i>>>2]|=(128|e>>>6&63)<<y[3&i++],o[i>>>2]|=(128|63&e)<<y[3&i++]);else for(i=this.start;s<n&&i<64;++s)o[i>>>2]|=t[s]<<y[3&i++];this.lastByteIndex=i,this.bytes+=i-this.start,i>=64?(this.block=o[16],this.start=i-64,this.hash(),this.hashed=!0):this.start=i}return this.bytes>4294967295&&(this.hBytes+=this.bytes/4294967296<<0,this.bytes=this.bytes%4294967296),this},t.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,r=this.lastByteIndex;t[16]=this.block,t[r>>>2]|=c[3&r],this.block=t[16],r>=56&&(this.hashed||this.hash(),t[0]=this.block,t[16]=t[1]=t[2]=t[3]=t[4]=t[5]=t[6]=t[7]=t[8]=t[9]=t[10]=t[11]=t[12]=t[13]=t[14]=t[15]=0),t[14]=this.hBytes<<3|this.bytes>>>29,t[15]=this.bytes<<3,this.hash()}},t.prototype.hash=function(){var t,r,e=this.h0,i=this.h1,h=this.h2,s=this.h3,n=this.h4,o=this.blocks;for(t=16;t<80;++t)r=o[t-3]^o[t-8]^o[t-14]^o[t-16],o[t]=r<<1|r>>>31;for(t=0;t<20;t+=5)e=(r=(i=(r=(h=(r=(s=(r=(n=(r=e<<5|e>>>27)+(i&h|~i&s)+n+1518500249+o[t]<<0)<<5|n>>>27)+(e&(i=i<<30|i>>>2)|~e&h)+s+1518500249+o[t+1]<<0)<<5|s>>>27)+(n&(e=e<<30|e>>>2)|~n&i)+h+1518500249+o[t+2]<<0)<<5|h>>>27)+(s&(n=n<<30|n>>>2)|~s&e)+i+1518500249+o[t+3]<<0)<<5|i>>>27)+(h&(s=s<<30|s>>>2)|~h&n)+e+1518500249+o[t+4]<<0,h=h<<30|h>>>2;for(;t<40;t+=5)e=(r=(i=(r=(h=(r=(s=(r=(n=(r=e<<5|e>>>27)+(i^h^s)+n+1859775393+o[t]<<0)<<5|n>>>27)+(e^(i=i<<30|i>>>2)^h)+s+1859775393+o[t+1]<<0)<<5|s>>>27)+(n^(e=e<<30|e>>>2)^i)+h+1859775393+o[t+2]<<0)<<5|h>>>27)+(s^(n=n<<30|n>>>2)^e)+i+1859775393+o[t+3]<<0)<<5|i>>>27)+(h^(s=s<<30|s>>>2)^n)+e+1859775393+o[t+4]<<0,h=h<<30|h>>>2;for(;t<60;t+=5)e=(r=(i=(r=(h=(r=(s=(r=(n=(r=e<<5|e>>>27)+(i&h|i&s|h&s)+n-1894007588+o[t]<<0)<<5|n>>>27)+(e&(i=i<<30|i>>>2)|e&h|i&h)+s-1894007588+o[t+1]<<0)<<5|s>>>27)+(n&(e=e<<30|e>>>2)|n&i|e&i)+h-1894007588+o[t+2]<<0)<<5|h>>>27)+(s&(n=n<<30|n>>>2)|s&e|n&e)+i-1894007588+o[t+3]<<0)<<5|i>>>27)+(h&(s=s<<30|s>>>2)|h&n|s&n)+e-1894007588+o[t+4]<<0,h=h<<30|h>>>2;for(;t<80;t+=5)e=(r=(i=(r=(h=(r=(s=(r=(n=(r=e<<5|e>>>27)+(i^h^s)+n-899497514+o[t]<<0)<<5|n>>>27)+(e^(i=i<<30|i>>>2)^h)+s-899497514+o[t+1]<<0)<<5|s>>>27)+(n^(e=e<<30|e>>>2)^i)+h-899497514+o[t+2]<<0)<<5|h>>>27)+(s^(n=n<<30|n>>>2)^e)+i-899497514+o[t+3]<<0)<<5|i>>>27)+(h^(s=s<<30|s>>>2)^n)+e-899497514+o[t+4]<<0,h=h<<30|h>>>2;this.h0=this.h0+e<<0,this.h1=this.h1+i<<0,this.h2=this.h2+h<<0,this.h3=this.h3+s<<0,this.h4=this.h4+n<<0},t.prototype.hex=function(){this.finalize();var t=this.h0,r=this.h1,e=this.h2,i=this.h3,h=this.h4;return u[t>>>28&15]+u[t>>>24&15]+u[t>>>20&15]+u[t>>>16&15]+u[t>>>12&15]+u[t>>>8&15]+u[t>>>4&15]+u[15&t]+u[r>>>28&15]+u[r>>>24&15]+u[r>>>20&15]+u[r>>>16&15]+u[r>>>12&15]+u[r>>>8&15]+u[r>>>4&15]+u[15&r]+u[e>>>28&15]+u[e>>>24&15]+u[e>>>20&15]+u[e>>>16&15]+u[e>>>12&15]+u[e>>>8&15]+u[e>>>4&15]+u[15&e]+u[i>>>28&15]+u[i>>>24&15]+u[i>>>20&15]+u[i>>>16&15]+u[i>>>12&15]+u[i>>>8&15]+u[i>>>4&15]+u[15&i]+u[h>>>28&15]+u[h>>>24&15]+u[h>>>20&15]+u[h>>>16&15]+u[h>>>12&15]+u[h>>>8&15]+u[h>>>4&15]+u[15&h]},t.prototype.toString=t.prototype.hex,t.prototype.digest=function(){this.finalize();var t=this.h0,r=this.h1,e=this.h2,i=this.h3,h=this.h4;return[t>>>24&255,t>>>16&255,t>>>8&255,255&t,r>>>24&255,r>>>16&255,r>>>8&255,255&r,e>>>24&255,e>>>16&255,e>>>8&255,255&e,i>>>24&255,i>>>16&255,i>>>8&255,255&i,h>>>24&255,h>>>16&255,h>>>8&255,255&h]},t.prototype.array=t.prototype.digest,t.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(20),r=new DataView(t);return r.setUint32(0,this.h0),r.setUint32(4,this.h1),r.setUint32(8,this.h2),r.setUint32(12,this.h3),r.setUint32(16,this.h4),t},(r.prototype=new t).finalize=function(){if(t.prototype.finalize.call(this),this.inner){this.inner=!1;var r=this.array();t.call(this,this.sharedMemory),this.update(this.oKeyPad),this.update(r),t.prototype.finalize.call(this)}};var S=function(){var r=_("hex");n&&(r=A(r)),r.create=function(){return new t},r.update=function(t){return r.create().update(t)};for(var e=0;e<p.length;++e){var i=p[e];r[i]=_(i)}return r}();S.sha1=S,S.sha1.hmac=function(){var t=w("hex");t.create=function(t){return new r(t)},t.update=function(r,e){return t.create(r).update(e)};for(var e=0;e<p.length;++e){var i=p[e];t[i]=w(i)}return t}(),o?module.exports=S:(h.sha1=S,a&&define(function(){return S}))}(); |
# Change Log | ||
## v0.7.0 / 2024-01-24 | ||
### Added | ||
- TypeScript interfaces. #6, #9 | ||
- HMAC feature. | ||
- support for web worker. #14 | ||
### Fixed | ||
- deprecated `new Buffer`, replace with `Buffer.from`. #10 | ||
- dependencies and security issues. | ||
- refactor: simplify formatMessage internal logic. | ||
- Generates incorrect hash in some cases. | ||
### Changed | ||
- remove `eval` and use `require` directly. #8 | ||
- throw error by Error oject. | ||
- throw error if update after finalize | ||
- use unsigned right shift. | ||
## v0.6.0 / 2017-12-21 | ||
@@ -4,0 +22,0 @@ ### Fixed |
@@ -1,2 +0,2 @@ | ||
Copyright 2014-2017 Chen, Yi-Cyuan | ||
Copyright 2014-2024 Chen, Yi-Cyuan | ||
@@ -3,0 +3,0 @@ Permission is hereby granted, free of charge, to any person obtaining |
{ | ||
"name": "js-sha1", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "A simple SHA1 hash function for JavaScript supports UTF-8 encoding.", | ||
@@ -8,5 +8,6 @@ "main": "src/sha1.js", | ||
"expect.js": "~0.3.1", | ||
"mocha": "~2.3.4", | ||
"nyc": "^11.3.0", | ||
"mocha": "~10.2.0", | ||
"nyc": "^15.1.0", | ||
"requirejs": "^2.1.22", | ||
"tiny-worker": "^2.3.0", | ||
"uglify-js": "^3.1.9" | ||
@@ -41,3 +42,7 @@ }, | ||
] | ||
}, | ||
"browser": { | ||
"crypto": false, | ||
"buffer": false | ||
} | ||
} |
@@ -9,2 +9,3 @@ # js-sha1 | ||
[SHA1 Online](http://emn178.github.io/online-tools/sha1.html) | ||
[SHA1 File Checksum Online](https://emn178.github.io/online-tools/sha1_checksum.html) | ||
@@ -24,2 +25,5 @@ ## Download | ||
## Notice | ||
NIST formally deprecated use of SHA-1 in 2011 and disallowed its use for digital signatures in 2013, and declared that it should be phased out by 2030. However, SHA-1 is still secure for HMAC. [wiki](https://en.wikipedia.org/wiki/SHA-1) | ||
## Usage | ||
@@ -32,7 +36,24 @@ You could use like this: | ||
hash.hex(); | ||
// HMAC | ||
sha1.hmac('key', 'Message to hash'); | ||
var hash = sha1.hmac.create('key'); | ||
hash.update('Message to hash'); | ||
hash.hex(); | ||
``` | ||
### Node.js | ||
If you use node.js, you should require the module first: | ||
```JavaScript | ||
sha1 = require('js-sha1'); | ||
var sha1 = require('js-sha1'); | ||
``` | ||
### TypeScript | ||
If you use TypeScript, you can import like this: | ||
```TypeScript | ||
import { sha1 } from 'js-sha1'; | ||
``` | ||
## RequireJS | ||
It supports AMD: | ||
@@ -64,2 +85,7 @@ ```JavaScript | ||
sha1.arrayBuffer(''); // ArrayBuffer | ||
// HMAC | ||
sha1.hmac.hex('key', 'Message to hash'); | ||
sha1.hmac.array('key', 'Message to hash'); | ||
// ... | ||
``` | ||
@@ -66,0 +92,0 @@ |
251
src/sha1.js
/* | ||
* [js-sha1]{@link https://github.com/emn178/js-sha1} | ||
* | ||
* @version 0.6.0 | ||
* @version 0.7.0 | ||
* @author Chen, Yi-Cyuan [emn178@gmail.com] | ||
* @copyright Chen, Yi-Cyuan 2014-2017 | ||
* @copyright Chen, Yi-Cyuan 2014-2024 | ||
* @license MIT | ||
@@ -13,9 +13,19 @@ */ | ||
var root = typeof window === 'object' ? window : {}; | ||
var INPUT_ERROR = 'input is invalid type'; | ||
var FINALIZE_ERROR = 'finalize already called'; | ||
var WINDOW = typeof window === 'object'; | ||
var root = WINDOW ? window : {}; | ||
if (root.JS_SHA1_NO_WINDOW) { | ||
WINDOW = false; | ||
} | ||
var WEB_WORKER = !WINDOW && typeof self === 'object'; | ||
var NODE_JS = !root.JS_SHA1_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; | ||
if (NODE_JS) { | ||
root = global; | ||
} else if (WEB_WORKER) { | ||
root = self; | ||
} | ||
var COMMON_JS = !root.JS_SHA1_NO_COMMON_JS && typeof module === 'object' && module.exports; | ||
var AMD = typeof define === 'function' && define.amd; | ||
var ARRAY_BUFFER = !root.JS_SHA1_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined'; | ||
var HEX_CHARS = '0123456789abcdef'.split(''); | ||
@@ -28,2 +38,34 @@ var EXTRA = [-2147483648, 8388608, 32768, 128]; | ||
var isArray = Array.isArray; | ||
if (root.JS_SHA1_NO_NODE_JS || !isArray) { | ||
isArray = function (obj) { | ||
return Object.prototype.toString.call(obj) === '[object Array]'; | ||
}; | ||
} | ||
var isView = ArrayBuffer.isView; | ||
if (ARRAY_BUFFER && (root.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW || !isView)) { | ||
isView = function (obj) { | ||
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer; | ||
}; | ||
} | ||
// [message: string, isString: bool] | ||
var formatMessage = function (message) { | ||
var type = typeof message; | ||
if (type === 'string') { | ||
return [message, true]; | ||
} | ||
if (type !== 'object' || message === null) { | ||
throw new Error(INPUT_ERROR); | ||
} | ||
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { | ||
return [new Uint8Array(message), false]; | ||
} | ||
if (!isArray(message) && !isView(message)) { | ||
throw new Error(INPUT_ERROR); | ||
} | ||
return [message, false]; | ||
} | ||
var createOutputMethod = function (outputType) { | ||
@@ -54,13 +96,28 @@ return function (message) { | ||
var nodeWrap = function (method) { | ||
var crypto = eval("require('crypto')"); | ||
var Buffer = eval("require('buffer').Buffer"); | ||
var crypto = require('crypto') | ||
var Buffer = require('buffer').Buffer; | ||
var bufferFrom; | ||
if (Buffer.from && !root.JS_SHA1_NO_BUFFER_FROM) { | ||
bufferFrom = Buffer.from; | ||
} else { | ||
bufferFrom = function (message) { | ||
return new Buffer(message); | ||
}; | ||
} | ||
var nodeMethod = function (message) { | ||
if (typeof message === 'string') { | ||
return crypto.createHash('sha1').update(message, 'utf8').digest('hex'); | ||
} else if (message.constructor === ArrayBuffer) { | ||
message = new Uint8Array(message); | ||
} else if (message.length === undefined) { | ||
} else { | ||
if (message === null || message === undefined) { | ||
throw new Error(INPUT_ERROR); | ||
} else if (message.constructor === ArrayBuffer) { | ||
message = new Uint8Array(message); | ||
} | ||
} | ||
if (isArray(message) || isView(message) || | ||
message.constructor === Buffer) { | ||
return crypto.createHash('sha1').update(bufferFrom(message)).digest('hex'); | ||
} else { | ||
return method(message); | ||
} | ||
return crypto.createHash('sha1').update(new Buffer(message)).digest('hex'); | ||
}; | ||
@@ -70,2 +127,23 @@ return nodeMethod; | ||
var createHmacOutputMethod = function (outputType) { | ||
return function (key, message) { | ||
return new HmacSha1(key, true).update(message)[outputType](); | ||
}; | ||
}; | ||
var createHmacMethod = function () { | ||
var method = createHmacOutputMethod('hex'); | ||
method.create = function (key) { | ||
return new HmacSha1(key); | ||
}; | ||
method.update = function (key, message) { | ||
return method.create(key).update(message); | ||
}; | ||
for (var i = 0; i < OUTPUT_TYPES.length; ++i) { | ||
var type = OUTPUT_TYPES[i]; | ||
method[type] = createHmacOutputMethod(type); | ||
} | ||
return method; | ||
}; | ||
function Sha1(sharedMemory) { | ||
@@ -95,8 +173,8 @@ if (sharedMemory) { | ||
if (this.finalized) { | ||
return; | ||
throw new Error(FINALIZE_ERROR); | ||
} | ||
var notString = typeof(message) !== 'string'; | ||
if (notString && message.constructor === root.ArrayBuffer) { | ||
message = new Uint8Array(message); | ||
} | ||
var result = formatMessage(message); | ||
message = result[0]; | ||
var isString = result[1]; | ||
var code, index = 0, i, length = message.length || 0, blocks = this.blocks; | ||
@@ -108,3 +186,3 @@ | ||
blocks[0] = this.block; | ||
blocks[16] = blocks[1] = blocks[2] = blocks[3] = | ||
this.block = blocks[16] = blocks[1] = blocks[2] = blocks[3] = | ||
blocks[4] = blocks[5] = blocks[6] = blocks[7] = | ||
@@ -115,26 +193,26 @@ blocks[8] = blocks[9] = blocks[10] = blocks[11] = | ||
if(notString) { | ||
if(isString) { | ||
for (i = this.start; index < length && i < 64; ++index) { | ||
blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; | ||
} | ||
} else { | ||
for (i = this.start; index < length && i < 64; ++index) { | ||
code = message.charCodeAt(index); | ||
if (code < 0x80) { | ||
blocks[i >> 2] |= code << SHIFT[i++ & 3]; | ||
blocks[i >>> 2] |= code << SHIFT[i++ & 3]; | ||
} else if (code < 0x800) { | ||
blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; | ||
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; | ||
blocks[i >>> 2] |= (0xc0 | (code >>> 6)) << SHIFT[i++ & 3]; | ||
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; | ||
} else if (code < 0xd800 || code >= 0xe000) { | ||
blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; | ||
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; | ||
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; | ||
blocks[i >>> 2] |= (0xe0 | (code >>> 12)) << SHIFT[i++ & 3]; | ||
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3]; | ||
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; | ||
} else { | ||
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); | ||
blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; | ||
blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; | ||
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; | ||
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; | ||
blocks[i >>> 2] |= (0xf0 | (code >>> 18)) << SHIFT[i++ & 3]; | ||
blocks[i >>> 2] |= (0x80 | ((code >>> 12) & 0x3f)) << SHIFT[i++ & 3]; | ||
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3]; | ||
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; | ||
} | ||
} | ||
} else { | ||
for (i = this.start; index < length && i < 64; ++index) { | ||
blocks[i >>> 2] |= message[index] << SHIFT[i++ & 3]; | ||
} | ||
} | ||
@@ -167,3 +245,3 @@ | ||
blocks[16] = this.block; | ||
blocks[i >> 2] |= EXTRA[i & 3]; | ||
blocks[i >>> 2] |= EXTRA[i & 3]; | ||
this.block = blocks[16]; | ||
@@ -314,22 +392,22 @@ if (i >= 56) { | ||
return HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] + | ||
HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] + | ||
HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] + | ||
HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] + | ||
HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] + | ||
HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] + | ||
HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] + | ||
HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] + | ||
HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] + | ||
HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] + | ||
HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] + | ||
HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] + | ||
HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] + | ||
HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] + | ||
HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] + | ||
HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] + | ||
HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] + | ||
HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] + | ||
HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] + | ||
HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F]; | ||
return HEX_CHARS[(h0 >>> 28) & 0x0F] + HEX_CHARS[(h0 >>> 24) & 0x0F] + | ||
HEX_CHARS[(h0 >>> 20) & 0x0F] + HEX_CHARS[(h0 >>> 16) & 0x0F] + | ||
HEX_CHARS[(h0 >>> 12) & 0x0F] + HEX_CHARS[(h0 >>> 8) & 0x0F] + | ||
HEX_CHARS[(h0 >>> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] + | ||
HEX_CHARS[(h1 >>> 28) & 0x0F] + HEX_CHARS[(h1 >>> 24) & 0x0F] + | ||
HEX_CHARS[(h1 >>> 20) & 0x0F] + HEX_CHARS[(h1 >>> 16) & 0x0F] + | ||
HEX_CHARS[(h1 >>> 12) & 0x0F] + HEX_CHARS[(h1 >>> 8) & 0x0F] + | ||
HEX_CHARS[(h1 >>> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] + | ||
HEX_CHARS[(h2 >>> 28) & 0x0F] + HEX_CHARS[(h2 >>> 24) & 0x0F] + | ||
HEX_CHARS[(h2 >>> 20) & 0x0F] + HEX_CHARS[(h2 >>> 16) & 0x0F] + | ||
HEX_CHARS[(h2 >>> 12) & 0x0F] + HEX_CHARS[(h2 >>> 8) & 0x0F] + | ||
HEX_CHARS[(h2 >>> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] + | ||
HEX_CHARS[(h3 >>> 28) & 0x0F] + HEX_CHARS[(h3 >>> 24) & 0x0F] + | ||
HEX_CHARS[(h3 >>> 20) & 0x0F] + HEX_CHARS[(h3 >>> 16) & 0x0F] + | ||
HEX_CHARS[(h3 >>> 12) & 0x0F] + HEX_CHARS[(h3 >>> 8) & 0x0F] + | ||
HEX_CHARS[(h3 >>> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] + | ||
HEX_CHARS[(h4 >>> 28) & 0x0F] + HEX_CHARS[(h4 >>> 24) & 0x0F] + | ||
HEX_CHARS[(h4 >>> 20) & 0x0F] + HEX_CHARS[(h4 >>> 16) & 0x0F] + | ||
HEX_CHARS[(h4 >>> 12) & 0x0F] + HEX_CHARS[(h4 >>> 8) & 0x0F] + | ||
HEX_CHARS[(h4 >>> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F]; | ||
}; | ||
@@ -345,7 +423,7 @@ | ||
return [ | ||
(h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF, | ||
(h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF, | ||
(h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF, | ||
(h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF, | ||
(h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF | ||
(h0 >>> 24) & 0xFF, (h0 >>> 16) & 0xFF, (h0 >>> 8) & 0xFF, h0 & 0xFF, | ||
(h1 >>> 24) & 0xFF, (h1 >>> 16) & 0xFF, (h1 >>> 8) & 0xFF, h1 & 0xFF, | ||
(h2 >>> 24) & 0xFF, (h2 >>> 16) & 0xFF, (h2 >>> 8) & 0xFF, h2 & 0xFF, | ||
(h3 >>> 24) & 0xFF, (h3 >>> 16) & 0xFF, (h3 >>> 8) & 0xFF, h3 & 0xFF, | ||
(h4 >>> 24) & 0xFF, (h4 >>> 16) & 0xFF, (h4 >>> 8) & 0xFF, h4 & 0xFF | ||
]; | ||
@@ -369,3 +447,64 @@ }; | ||
function HmacSha1(key, sharedMemory) { | ||
var i, result = formatMessage(key); | ||
key = result[0]; | ||
if (result[1]) { | ||
var bytes = [], length = key.length, index = 0, code; | ||
for (i = 0; i < length; ++i) { | ||
code = key.charCodeAt(i); | ||
if (code < 0x80) { | ||
bytes[index++] = code; | ||
} else if (code < 0x800) { | ||
bytes[index++] = (0xc0 | (code >>> 6)); | ||
bytes[index++] = (0x80 | (code & 0x3f)); | ||
} else if (code < 0xd800 || code >= 0xe000) { | ||
bytes[index++] = (0xe0 | (code >>> 12)); | ||
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f)); | ||
bytes[index++] = (0x80 | (code & 0x3f)); | ||
} else { | ||
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff)); | ||
bytes[index++] = (0xf0 | (code >>> 18)); | ||
bytes[index++] = (0x80 | ((code >>> 12) & 0x3f)); | ||
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f)); | ||
bytes[index++] = (0x80 | (code & 0x3f)); | ||
} | ||
} | ||
key = bytes; | ||
} | ||
if (key.length > 64) { | ||
key = (new Sha1(true)).update(key).array(); | ||
} | ||
var oKeyPad = [], iKeyPad = []; | ||
for (i = 0; i < 64; ++i) { | ||
var b = key[i] || 0; | ||
oKeyPad[i] = 0x5c ^ b; | ||
iKeyPad[i] = 0x36 ^ b; | ||
} | ||
Sha1.call(this, sharedMemory); | ||
this.update(iKeyPad); | ||
this.oKeyPad = oKeyPad; | ||
this.inner = true; | ||
this.sharedMemory = sharedMemory; | ||
} | ||
HmacSha1.prototype = new Sha1(); | ||
HmacSha1.prototype.finalize = function () { | ||
Sha1.prototype.finalize.call(this); | ||
if (this.inner) { | ||
this.inner = false; | ||
var innerHash = this.array(); | ||
Sha1.call(this, this.sharedMemory); | ||
this.update(this.oKeyPad); | ||
this.update(innerHash); | ||
Sha1.prototype.finalize.call(this); | ||
} | ||
}; | ||
var exports = createMethod(); | ||
exports.sha1 = exports; | ||
exports.sha1.hmac = createHmacMethod(); | ||
@@ -372,0 +511,0 @@ if (COMMON_JS) { |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
33547
7
584
95
6
2