js-md5 0.7.3 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Change Log
2
2
 
3
+ ## v0.8.0 / 2023-09-27
4
+ ### Added
5
+ - TypeScript interfaces.
6
+ - HMAC feature.
7
+
8
+ ### Fixed
9
+ - deprecated `new Buffer`, replace with `Buffer.from`.
10
+ - dependencies and security issues.
11
+ - don't modify global Array and ArrayBuffer prototypes.
12
+ - refactor: simplify formatMessage internal logic.
13
+
14
+ ### Changed
15
+ - remove `eval` and use `require` directly.
16
+ - throw error by Error oject.
17
+ - throw error if update after finalize
18
+ - use unsigned right shift.
19
+
3
20
  ## v0.7.3 / 2017-12-18
4
21
  ### Fixed
5
22
  - incorrect result when first bit is 1 of bytes. #18
package/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2014-2017 Chen, Yi-Cyuan
1
+ Copyright 2014-2023 Chen, Yi-Cyuan
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
package/README.md CHANGED
@@ -32,11 +32,22 @@ md5('Message to hash');
32
32
  var hash = md5.create();
33
33
  hash.update('Message to hash');
34
34
  hash.hex();
35
+
36
+ // HMAC
37
+ md5.hmac('key', 'Message to hash');
38
+
39
+ var hash = md5.hmac.create('key');
40
+ hash.update('Message to hash');
41
+ hash.hex();
35
42
  ```
43
+
44
+ ### Node.js
36
45
  If you use node.js, you should require the module first:
37
46
  ```JavaScript
38
- md5 = require('js-md5');
47
+ var md5 = require('js-md5');
39
48
  ```
49
+
50
+ ## RequireJS
40
51
  It supports AMD:
41
52
  ```JavaScript
42
53
  require(['your/path/md5.js'], function(md5) {
@@ -66,10 +77,15 @@ md5.digest(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 24
66
77
  md5.arrayBuffer(''); // ArrayBuffer
67
78
  md5.buffer(''); // ArrayBuffer, deprecated, This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
68
79
  md5.base64(''); // 1B2M2Y8AsgTpgAmY7PhCfg==
80
+
81
+ // HMAC
82
+ md5.hmac.hex('key', 'Message to hash');
83
+ md5.hmac.array('key', 'Message to hash');
84
+ // ...
69
85
  ```
70
86
 
71
87
  ## License
72
- The project is released under the [MIT license](http://www.opensource.org/licenses/MIT).
88
+ The project is released under the [MIT license](https://opensource.org/license/mit/).
73
89
 
74
90
  ## Contact
75
91
  The project's website is located at https://github.com/emn178/js-md5
package/build/md5.min.js CHANGED
@@ -2,9 +2,9 @@
2
2
  * [js-md5]{@link https://github.com/emn178/js-md5}
3
3
  *
4
4
  * @namespace md5
5
- * @version 0.7.3
5
+ * @version 0.8.0
6
6
  * @author Chen, Yi-Cyuan [emn178@gmail.com]
7
- * @copyright Chen, Yi-Cyuan 2014-2017
7
+ * @copyright Chen, Yi-Cyuan 2014-2023
8
8
  * @license MIT
9
9
  */
10
- !function(){"use strict";function t(t){if(t)d[0]=d[16]=d[1]=d[2]=d[3]=d[4]=d[5]=d[6]=d[7]=d[8]=d[9]=d[10]=d[11]=d[12]=d[13]=d[14]=d[15]=0,this.blocks=d,this.buffer8=l;else if(a){var r=new ArrayBuffer(68);this.buffer8=new Uint8Array(r),this.blocks=new Uint32Array(r)}else this.blocks=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];this.h0=this.h1=this.h2=this.h3=this.start=this.bytes=this.hBytes=0,this.finalized=this.hashed=!1,this.first=!0}var r="input is invalid type",e="object"==typeof window,i=e?window:{};i.JS_MD5_NO_WINDOW&&(e=!1);var s=!e&&"object"==typeof self,h=!i.JS_MD5_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node;h?i=global:s&&(i=self);var f=!i.JS_MD5_NO_COMMON_JS&&"object"==typeof module&&module.exports,o="function"==typeof define&&define.amd,a=!i.JS_MD5_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,n="0123456789abcdef".split(""),u=[128,32768,8388608,-2147483648],y=[0,8,16,24],c=["hex","array","digest","buffer","arrayBuffer","base64"],p="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(""),d=[],l;if(a){var A=new ArrayBuffer(68);l=new Uint8Array(A),d=new Uint32Array(A)}!i.JS_MD5_NO_NODE_JS&&Array.isArray||(Array.isArray=function(t){return"[object Array]"===Object.prototype.toString.call(t)}),!a||!i.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW&&ArrayBuffer.isView||(ArrayBuffer.isView=function(t){return"object"==typeof t&&t.buffer&&t.buffer.constructor===ArrayBuffer});var b=function(r){return function(e){return new t(!0).update(e)[r]()}},v=function(){var r=b("hex");h&&(r=w(r)),r.create=function(){return new t},r.update=function(t){return r.create().update(t)};for(var e=0;e<c.length;++e){var i=c[e];r[i]=b(i)}return r},w=function(t){var e=eval("require('crypto')"),i=eval("require('buffer').Buffer"),s=function(s){if("string"==typeof s)return e.createHash("md5").update(s,"utf8").digest("hex");if(null===s||void 0===s)throw r;return s.constructor===ArrayBuffer&&(s=new Uint8Array(s)),Array.isArray(s)||ArrayBuffer.isView(s)||s.constructor===i?e.createHash("md5").update(new i(s)).digest("hex"):t(s)};return s};t.prototype.update=function(t){if(!this.finalized){var e,i=typeof t;if("string"!==i){if("object"!==i)throw r;if(null===t)throw r;if(a&&t.constructor===ArrayBuffer)t=new Uint8Array(t);else if(!(Array.isArray(t)||a&&ArrayBuffer.isView(t)))throw r;e=!0}for(var s,h,f=0,o=t.length,n=this.blocks,u=this.buffer8;f<o;){if(this.hashed&&(this.hashed=!1,n[0]=n[16],n[16]=n[1]=n[2]=n[3]=n[4]=n[5]=n[6]=n[7]=n[8]=n[9]=n[10]=n[11]=n[12]=n[13]=n[14]=n[15]=0),e)if(a)for(h=this.start;f<o&&h<64;++f)u[h++]=t[f];else for(h=this.start;f<o&&h<64;++f)n[h>>2]|=t[f]<<y[3&h++];else if(a)for(h=this.start;f<o&&h<64;++f)(s=t.charCodeAt(f))<128?u[h++]=s:s<2048?(u[h++]=192|s>>6,u[h++]=128|63&s):s<55296||s>=57344?(u[h++]=224|s>>12,u[h++]=128|s>>6&63,u[h++]=128|63&s):(s=65536+((1023&s)<<10|1023&t.charCodeAt(++f)),u[h++]=240|s>>18,u[h++]=128|s>>12&63,u[h++]=128|s>>6&63,u[h++]=128|63&s);else for(h=this.start;f<o&&h<64;++f)(s=t.charCodeAt(f))<128?n[h>>2]|=s<<y[3&h++]:s<2048?(n[h>>2]|=(192|s>>6)<<y[3&h++],n[h>>2]|=(128|63&s)<<y[3&h++]):s<55296||s>=57344?(n[h>>2]|=(224|s>>12)<<y[3&h++],n[h>>2]|=(128|s>>6&63)<<y[3&h++],n[h>>2]|=(128|63&s)<<y[3&h++]):(s=65536+((1023&s)<<10|1023&t.charCodeAt(++f)),n[h>>2]|=(240|s>>18)<<y[3&h++],n[h>>2]|=(128|s>>12&63)<<y[3&h++],n[h>>2]|=(128|s>>6&63)<<y[3&h++],n[h>>2]|=(128|63&s)<<y[3&h++]);this.lastByteIndex=h,this.bytes+=h-this.start,h>=64?(this.start=h-64,this.hash(),this.hashed=!0):this.start=h}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[r>>2]|=u[3&r],r>=56&&(this.hashed||this.hash(),t[0]=t[16],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.bytes<<3,t[15]=this.hBytes<<3|this.bytes>>>29,this.hash()}},t.prototype.hash=function(){var t,r,e,i,s,h,f=this.blocks;this.first?r=((r=((t=((t=f[0]-680876937)<<7|t>>>25)-271733879<<0)^(e=((e=(-271733879^(i=((i=(-1732584194^2004318071&t)+f[1]-117830708)<<12|i>>>20)+t<<0)&(-271733879^t))+f[2]-1126478375)<<17|e>>>15)+i<<0)&(i^t))+f[3]-1316259209)<<22|r>>>10)+e<<0:(t=this.h0,r=this.h1,e=this.h2,r=((r+=((t=((t+=((i=this.h3)^r&(e^i))+f[0]-680876936)<<7|t>>>25)+r<<0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+f[1]-389564586)<<12|i>>>20)+t<<0)&(t^r))+f[2]+606105819)<<17|e>>>15)+i<<0)&(i^t))+f[3]-1044525330)<<22|r>>>10)+e<<0),r=((r+=((t=((t+=(i^r&(e^i))+f[4]-176418897)<<7|t>>>25)+r<<0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+f[5]+1200080426)<<12|i>>>20)+t<<0)&(t^r))+f[6]-1473231341)<<17|e>>>15)+i<<0)&(i^t))+f[7]-45705983)<<22|r>>>10)+e<<0,r=((r+=((t=((t+=(i^r&(e^i))+f[8]+1770035416)<<7|t>>>25)+r<<0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+f[9]-1958414417)<<12|i>>>20)+t<<0)&(t^r))+f[10]-42063)<<17|e>>>15)+i<<0)&(i^t))+f[11]-1990404162)<<22|r>>>10)+e<<0,r=((r+=((t=((t+=(i^r&(e^i))+f[12]+1804603682)<<7|t>>>25)+r<<0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+f[13]-40341101)<<12|i>>>20)+t<<0)&(t^r))+f[14]-1502002290)<<17|e>>>15)+i<<0)&(i^t))+f[15]+1236535329)<<22|r>>>10)+e<<0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+f[1]-165796510)<<5|t>>>27)+r<<0)^r))+f[6]-1069501632)<<9|i>>>23)+t<<0)^t&((e=((e+=(t^r&(i^t))+f[11]+643717713)<<14|e>>>18)+i<<0)^i))+f[0]-373897302)<<20|r>>>12)+e<<0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+f[5]-701558691)<<5|t>>>27)+r<<0)^r))+f[10]+38016083)<<9|i>>>23)+t<<0)^t&((e=((e+=(t^r&(i^t))+f[15]-660478335)<<14|e>>>18)+i<<0)^i))+f[4]-405537848)<<20|r>>>12)+e<<0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+f[9]+568446438)<<5|t>>>27)+r<<0)^r))+f[14]-1019803690)<<9|i>>>23)+t<<0)^t&((e=((e+=(t^r&(i^t))+f[3]-187363961)<<14|e>>>18)+i<<0)^i))+f[8]+1163531501)<<20|r>>>12)+e<<0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+f[13]-1444681467)<<5|t>>>27)+r<<0)^r))+f[2]-51403784)<<9|i>>>23)+t<<0)^t&((e=((e+=(t^r&(i^t))+f[7]+1735328473)<<14|e>>>18)+i<<0)^i))+f[12]-1926607734)<<20|r>>>12)+e<<0,r=((r+=((h=(i=((i+=((s=r^e)^(t=((t+=(s^i)+f[5]-378558)<<4|t>>>28)+r<<0))+f[8]-2022574463)<<11|i>>>21)+t<<0)^t)^(e=((e+=(h^r)+f[11]+1839030562)<<16|e>>>16)+i<<0))+f[14]-35309556)<<23|r>>>9)+e<<0,r=((r+=((h=(i=((i+=((s=r^e)^(t=((t+=(s^i)+f[1]-1530992060)<<4|t>>>28)+r<<0))+f[4]+1272893353)<<11|i>>>21)+t<<0)^t)^(e=((e+=(h^r)+f[7]-155497632)<<16|e>>>16)+i<<0))+f[10]-1094730640)<<23|r>>>9)+e<<0,r=((r+=((h=(i=((i+=((s=r^e)^(t=((t+=(s^i)+f[13]+681279174)<<4|t>>>28)+r<<0))+f[0]-358537222)<<11|i>>>21)+t<<0)^t)^(e=((e+=(h^r)+f[3]-722521979)<<16|e>>>16)+i<<0))+f[6]+76029189)<<23|r>>>9)+e<<0,r=((r+=((h=(i=((i+=((s=r^e)^(t=((t+=(s^i)+f[9]-640364487)<<4|t>>>28)+r<<0))+f[12]-421815835)<<11|i>>>21)+t<<0)^t)^(e=((e+=(h^r)+f[15]+530742520)<<16|e>>>16)+i<<0))+f[2]-995338651)<<23|r>>>9)+e<<0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+f[0]-198630844)<<6|t>>>26)+r<<0)|~e))+f[7]+1126891415)<<10|i>>>22)+t<<0)^((e=((e+=(t^(i|~r))+f[14]-1416354905)<<15|e>>>17)+i<<0)|~t))+f[5]-57434055)<<21|r>>>11)+e<<0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+f[12]+1700485571)<<6|t>>>26)+r<<0)|~e))+f[3]-1894986606)<<10|i>>>22)+t<<0)^((e=((e+=(t^(i|~r))+f[10]-1051523)<<15|e>>>17)+i<<0)|~t))+f[1]-2054922799)<<21|r>>>11)+e<<0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+f[8]+1873313359)<<6|t>>>26)+r<<0)|~e))+f[15]-30611744)<<10|i>>>22)+t<<0)^((e=((e+=(t^(i|~r))+f[6]-1560198380)<<15|e>>>17)+i<<0)|~t))+f[13]+1309151649)<<21|r>>>11)+e<<0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+f[4]-145523070)<<6|t>>>26)+r<<0)|~e))+f[11]-1120210379)<<10|i>>>22)+t<<0)^((e=((e+=(t^(i|~r))+f[2]+718787259)<<15|e>>>17)+i<<0)|~t))+f[9]-343485551)<<21|r>>>11)+e<<0,this.first?(this.h0=t+1732584193<<0,this.h1=r-271733879<<0,this.h2=e-1732584194<<0,this.h3=i+271733878<<0,this.first=!1):(this.h0=this.h0+t<<0,this.h1=this.h1+r<<0,this.h2=this.h2+e<<0,this.h3=this.h3+i<<0)},t.prototype.hex=function(){this.finalize();var t=this.h0,r=this.h1,e=this.h2,i=this.h3;return n[t>>4&15]+n[15&t]+n[t>>12&15]+n[t>>8&15]+n[t>>20&15]+n[t>>16&15]+n[t>>28&15]+n[t>>24&15]+n[r>>4&15]+n[15&r]+n[r>>12&15]+n[r>>8&15]+n[r>>20&15]+n[r>>16&15]+n[r>>28&15]+n[r>>24&15]+n[e>>4&15]+n[15&e]+n[e>>12&15]+n[e>>8&15]+n[e>>20&15]+n[e>>16&15]+n[e>>28&15]+n[e>>24&15]+n[i>>4&15]+n[15&i]+n[i>>12&15]+n[i>>8&15]+n[i>>20&15]+n[i>>16&15]+n[i>>28&15]+n[i>>24&15]},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;return[255&t,t>>8&255,t>>16&255,t>>24&255,255&r,r>>8&255,r>>16&255,r>>24&255,255&e,e>>8&255,e>>16&255,e>>24&255,255&i,i>>8&255,i>>16&255,i>>24&255]},t.prototype.array=t.prototype.digest,t.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(16),r=new Uint32Array(t);return r[0]=this.h0,r[1]=this.h1,r[2]=this.h2,r[3]=this.h3,t},t.prototype.buffer=t.prototype.arrayBuffer,t.prototype.base64=function(){for(var t,r,e,i="",s=this.array(),h=0;h<15;)t=s[h++],r=s[h++],e=s[h++],i+=p[t>>>2]+p[63&(t<<4|r>>>4)]+p[63&(r<<2|e>>>6)]+p[63&e];return t=s[h],i+=p[t>>>2]+p[t<<4&63]+"=="};var _=v();f?module.exports=_:(i.md5=_,o&&define(function(){return _}))}();
10
+ !function(){"use strict";function t(t){if(t)b[0]=b[16]=b[1]=b[2]=b[3]=b[4]=b[5]=b[6]=b[7]=b[8]=b[9]=b[10]=b[11]=b[12]=b[13]=b[14]=b[15]=0,this.blocks=b,this.buffer8=a;else if(u){var r=new ArrayBuffer(68);this.buffer8=new Uint8Array(r),this.blocks=new Uint32Array(r)}else this.blocks=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];this.h0=this.h1=this.h2=this.h3=this.start=this.bytes=this.hBytes=0,this.finalized=this.hashed=!1,this.first=!0}function r(r,e){var i,s=_(r);if(r=s[0],s[1]){var h,n=[],a=r.length,o=0;for(i=0;i<a;++i)(h=r.charCodeAt(i))<128?n[o++]=h:h<2048?(n[o++]=192|h>>>6,n[o++]=128|63&h):h<55296||h>=57344?(n[o++]=224|h>>>12,n[o++]=128|h>>>6&63,n[o++]=128|63&h):(h=65536+((1023&h)<<10|1023&r.charCodeAt(++i)),n[o++]=240|h>>>18,n[o++]=128|h>>>12&63,n[o++]=128|h>>>6&63,n[o++]=128|63&h);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,s=i?window:{};s.JS_MD5_NO_WINDOW&&(i=!1);var h=!i&&"object"==typeof self,n=!s.JS_MD5_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node;n?s=global:h&&(s=self);var a,o=!s.JS_MD5_NO_COMMON_JS&&"object"==typeof module&&module.exports,f="function"==typeof define&&define.amd,u=!s.JS_MD5_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,c="0123456789abcdef".split(""),y=[128,32768,8388608,-2147483648],p=[0,8,16,24],d=["hex","array","digest","buffer","arrayBuffer","base64"],l="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(""),b=[];if(u){var v=new ArrayBuffer(68);a=new Uint8Array(v),b=new Uint32Array(v)}var w=Array.isArray;!s.JS_MD5_NO_NODE_JS&&w||(w=function(t){return"[object Array]"===Object.prototype.toString.call(t)});var A=ArrayBuffer.isView;!u||!s.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW&&A||(A=function(t){return"object"==typeof t&&t.buffer&&t.buffer.constructor===ArrayBuffer});var _=function(t){var r=typeof t;if("string"===r)return[t,!0];if("object"!==r||null===t)throw new Error(e);if(u&&t.constructor===ArrayBuffer)return[new Uint8Array(t),!1];if(!w(t)&&!A(t))throw new Error(e);return[t,!1]},B=function(r){return function(e){return new t(!0).update(e)[r]()}},g=function(t){var r,i=require("crypto"),h=require("buffer").Buffer;r=h.from&&!s.JS_MD5_NO_BUFFER_FROM?h.from:function(t){return new h(t)};return function(s){if("string"==typeof s)return i.createHash("md5").update(s,"utf8").digest("hex");if(null===s||void 0===s)throw new Error(e);return s.constructor===ArrayBuffer&&(s=new Uint8Array(s)),w(s)||A(s)||s.constructor===h?i.createHash("md5").update(r(s)).digest("hex"):t(s)}},m=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=_(t);t=r[0];for(var e,i,s=r[1],h=0,n=t.length,a=this.blocks,o=this.buffer8;h<n;){if(this.hashed&&(this.hashed=!1,a[0]=a[16],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)if(u)for(i=this.start;h<n&&i<64;++h)(e=t.charCodeAt(h))<128?o[i++]=e:e<2048?(o[i++]=192|e>>>6,o[i++]=128|63&e):e<55296||e>=57344?(o[i++]=224|e>>>12,o[i++]=128|e>>>6&63,o[i++]=128|63&e):(e=65536+((1023&e)<<10|1023&t.charCodeAt(++h)),o[i++]=240|e>>>18,o[i++]=128|e>>>12&63,o[i++]=128|e>>>6&63,o[i++]=128|63&e);else for(i=this.start;h<n&&i<64;++h)(e=t.charCodeAt(h))<128?a[i>>>2]|=e<<p[3&i++]:e<2048?(a[i>>>2]|=(192|e>>>6)<<p[3&i++],a[i>>>2]|=(128|63&e)<<p[3&i++]):e<55296||e>=57344?(a[i>>>2]|=(224|e>>>12)<<p[3&i++],a[i>>>2]|=(128|e>>>6&63)<<p[3&i++],a[i>>>2]|=(128|63&e)<<p[3&i++]):(e=65536+((1023&e)<<10|1023&t.charCodeAt(++h)),a[i>>>2]|=(240|e>>>18)<<p[3&i++],a[i>>>2]|=(128|e>>>12&63)<<p[3&i++],a[i>>>2]|=(128|e>>>6&63)<<p[3&i++],a[i>>>2]|=(128|63&e)<<p[3&i++]);else if(u)for(i=this.start;h<n&&i<64;++h)o[i++]=t[h];else for(i=this.start;h<n&&i<64;++h)a[i>>>2]|=t[h]<<p[3&i++];this.lastByteIndex=i,this.bytes+=i-this.start,i>=64?(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[r>>>2]|=y[3&r],r>=56&&(this.hashed||this.hash(),t[0]=t[16],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.bytes<<3,t[15]=this.hBytes<<3|this.bytes>>>29,this.hash()}},t.prototype.hash=function(){var t,r,e,i,s,h,n=this.blocks;this.first?r=((r=((t=((t=n[0]-680876937)<<7|t>>>25)-271733879<<0)^(e=((e=(-271733879^(i=((i=(-1732584194^2004318071&t)+n[1]-117830708)<<12|i>>>20)+t<<0)&(-271733879^t))+n[2]-1126478375)<<17|e>>>15)+i<<0)&(i^t))+n[3]-1316259209)<<22|r>>>10)+e<<0:(t=this.h0,r=this.h1,e=this.h2,r=((r+=((t=((t+=((i=this.h3)^r&(e^i))+n[0]-680876936)<<7|t>>>25)+r<<0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+n[1]-389564586)<<12|i>>>20)+t<<0)&(t^r))+n[2]+606105819)<<17|e>>>15)+i<<0)&(i^t))+n[3]-1044525330)<<22|r>>>10)+e<<0),r=((r+=((t=((t+=(i^r&(e^i))+n[4]-176418897)<<7|t>>>25)+r<<0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+n[5]+1200080426)<<12|i>>>20)+t<<0)&(t^r))+n[6]-1473231341)<<17|e>>>15)+i<<0)&(i^t))+n[7]-45705983)<<22|r>>>10)+e<<0,r=((r+=((t=((t+=(i^r&(e^i))+n[8]+1770035416)<<7|t>>>25)+r<<0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+n[9]-1958414417)<<12|i>>>20)+t<<0)&(t^r))+n[10]-42063)<<17|e>>>15)+i<<0)&(i^t))+n[11]-1990404162)<<22|r>>>10)+e<<0,r=((r+=((t=((t+=(i^r&(e^i))+n[12]+1804603682)<<7|t>>>25)+r<<0)^(e=((e+=(r^(i=((i+=(e^t&(r^e))+n[13]-40341101)<<12|i>>>20)+t<<0)&(t^r))+n[14]-1502002290)<<17|e>>>15)+i<<0)&(i^t))+n[15]+1236535329)<<22|r>>>10)+e<<0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+n[1]-165796510)<<5|t>>>27)+r<<0)^r))+n[6]-1069501632)<<9|i>>>23)+t<<0)^t&((e=((e+=(t^r&(i^t))+n[11]+643717713)<<14|e>>>18)+i<<0)^i))+n[0]-373897302)<<20|r>>>12)+e<<0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+n[5]-701558691)<<5|t>>>27)+r<<0)^r))+n[10]+38016083)<<9|i>>>23)+t<<0)^t&((e=((e+=(t^r&(i^t))+n[15]-660478335)<<14|e>>>18)+i<<0)^i))+n[4]-405537848)<<20|r>>>12)+e<<0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+n[9]+568446438)<<5|t>>>27)+r<<0)^r))+n[14]-1019803690)<<9|i>>>23)+t<<0)^t&((e=((e+=(t^r&(i^t))+n[3]-187363961)<<14|e>>>18)+i<<0)^i))+n[8]+1163531501)<<20|r>>>12)+e<<0,r=((r+=((i=((i+=(r^e&((t=((t+=(e^i&(r^e))+n[13]-1444681467)<<5|t>>>27)+r<<0)^r))+n[2]-51403784)<<9|i>>>23)+t<<0)^t&((e=((e+=(t^r&(i^t))+n[7]+1735328473)<<14|e>>>18)+i<<0)^i))+n[12]-1926607734)<<20|r>>>12)+e<<0,r=((r+=((h=(i=((i+=((s=r^e)^(t=((t+=(s^i)+n[5]-378558)<<4|t>>>28)+r<<0))+n[8]-2022574463)<<11|i>>>21)+t<<0)^t)^(e=((e+=(h^r)+n[11]+1839030562)<<16|e>>>16)+i<<0))+n[14]-35309556)<<23|r>>>9)+e<<0,r=((r+=((h=(i=((i+=((s=r^e)^(t=((t+=(s^i)+n[1]-1530992060)<<4|t>>>28)+r<<0))+n[4]+1272893353)<<11|i>>>21)+t<<0)^t)^(e=((e+=(h^r)+n[7]-155497632)<<16|e>>>16)+i<<0))+n[10]-1094730640)<<23|r>>>9)+e<<0,r=((r+=((h=(i=((i+=((s=r^e)^(t=((t+=(s^i)+n[13]+681279174)<<4|t>>>28)+r<<0))+n[0]-358537222)<<11|i>>>21)+t<<0)^t)^(e=((e+=(h^r)+n[3]-722521979)<<16|e>>>16)+i<<0))+n[6]+76029189)<<23|r>>>9)+e<<0,r=((r+=((h=(i=((i+=((s=r^e)^(t=((t+=(s^i)+n[9]-640364487)<<4|t>>>28)+r<<0))+n[12]-421815835)<<11|i>>>21)+t<<0)^t)^(e=((e+=(h^r)+n[15]+530742520)<<16|e>>>16)+i<<0))+n[2]-995338651)<<23|r>>>9)+e<<0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+n[0]-198630844)<<6|t>>>26)+r<<0)|~e))+n[7]+1126891415)<<10|i>>>22)+t<<0)^((e=((e+=(t^(i|~r))+n[14]-1416354905)<<15|e>>>17)+i<<0)|~t))+n[5]-57434055)<<21|r>>>11)+e<<0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+n[12]+1700485571)<<6|t>>>26)+r<<0)|~e))+n[3]-1894986606)<<10|i>>>22)+t<<0)^((e=((e+=(t^(i|~r))+n[10]-1051523)<<15|e>>>17)+i<<0)|~t))+n[1]-2054922799)<<21|r>>>11)+e<<0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+n[8]+1873313359)<<6|t>>>26)+r<<0)|~e))+n[15]-30611744)<<10|i>>>22)+t<<0)^((e=((e+=(t^(i|~r))+n[6]-1560198380)<<15|e>>>17)+i<<0)|~t))+n[13]+1309151649)<<21|r>>>11)+e<<0,r=((r+=((i=((i+=(r^((t=((t+=(e^(r|~i))+n[4]-145523070)<<6|t>>>26)+r<<0)|~e))+n[11]-1120210379)<<10|i>>>22)+t<<0)^((e=((e+=(t^(i|~r))+n[2]+718787259)<<15|e>>>17)+i<<0)|~t))+n[9]-343485551)<<21|r>>>11)+e<<0,this.first?(this.h0=t+1732584193<<0,this.h1=r-271733879<<0,this.h2=e-1732584194<<0,this.h3=i+271733878<<0,this.first=!1):(this.h0=this.h0+t<<0,this.h1=this.h1+r<<0,this.h2=this.h2+e<<0,this.h3=this.h3+i<<0)},t.prototype.hex=function(){this.finalize();var t=this.h0,r=this.h1,e=this.h2,i=this.h3;return c[t>>>4&15]+c[15&t]+c[t>>>12&15]+c[t>>>8&15]+c[t>>>20&15]+c[t>>>16&15]+c[t>>>28&15]+c[t>>>24&15]+c[r>>>4&15]+c[15&r]+c[r>>>12&15]+c[r>>>8&15]+c[r>>>20&15]+c[r>>>16&15]+c[r>>>28&15]+c[r>>>24&15]+c[e>>>4&15]+c[15&e]+c[e>>>12&15]+c[e>>>8&15]+c[e>>>20&15]+c[e>>>16&15]+c[e>>>28&15]+c[e>>>24&15]+c[i>>>4&15]+c[15&i]+c[i>>>12&15]+c[i>>>8&15]+c[i>>>20&15]+c[i>>>16&15]+c[i>>>28&15]+c[i>>>24&15]},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;return[255&t,t>>>8&255,t>>>16&255,t>>>24&255,255&r,r>>>8&255,r>>>16&255,r>>>24&255,255&e,e>>>8&255,e>>>16&255,e>>>24&255,255&i,i>>>8&255,i>>>16&255,i>>>24&255]},t.prototype.array=t.prototype.digest,t.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(16),r=new Uint32Array(t);return r[0]=this.h0,r[1]=this.h1,r[2]=this.h2,r[3]=this.h3,t},t.prototype.buffer=t.prototype.arrayBuffer,t.prototype.base64=function(){for(var t,r,e,i="",s=this.array(),h=0;h<15;)t=s[h++],r=s[h++],e=s[h++],i+=l[t>>>2]+l[63&(t<<4|r>>>4)]+l[63&(r<<2|e>>>6)]+l[63&e];return t=s[h],i+=l[t>>>2]+l[t<<4&63]+"=="},(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 O=function(){var r=B("hex");n&&(r=g(r)),r.create=function(){return new t},r.update=function(t){return r.create().update(t)};for(var e=0;e<d.length;++e){var i=d[e];r[i]=B(i)}return r}();O.md5=O,O.md5.hmac=function(){var t=m("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<d.length;++e){var i=d[e];t[i]=m(i)}return t}(),o?module.exports=O:(s.md5=O,f&&define(function(){return O}))}();
package/index.d.ts ADDED
@@ -0,0 +1,148 @@
1
+ type Message = string | number[] | ArrayBuffer | Uint8Array;
2
+
3
+ interface Hasher {
4
+ /**
5
+ * Update hash
6
+ *
7
+ * @param message The message you want to hash.
8
+ */
9
+ update(message: Message): Hasher;
10
+
11
+ /**
12
+ * Return hash in hex string.
13
+ */
14
+ hex(): string;
15
+
16
+ /**
17
+ * Return hash in hex string.
18
+ */
19
+ toString(): string;
20
+
21
+ /**
22
+ * Return hash in ArrayBuffer.
23
+ */
24
+ arrayBuffer(): ArrayBuffer;
25
+
26
+ /**
27
+ * Return hash in integer array.
28
+ */
29
+ digest(): number[];
30
+
31
+ /**
32
+ * Return hash in integer array.
33
+ */
34
+ array(): number[];
35
+ }
36
+
37
+ interface Hmac {
38
+ /**
39
+ * Computes a Hash-based message authentication code (HMAC) using a secret key
40
+ *
41
+ * @param secretKey The Secret Key
42
+ * @param message The message you want to hash.
43
+ */
44
+ (secretKey: Message, message: Message): string;
45
+
46
+ /**
47
+ * Create a hash object using a secret key.
48
+ *
49
+ * @param secretKey The Secret Key
50
+ */
51
+ create(secretKey: Message): Hasher;
52
+
53
+ /**
54
+ * Create a hash object and hash message using a secret key
55
+ *
56
+ * @param secretKey The Secret Key
57
+ * @param message The message you want to hash.
58
+ */
59
+ update(secretKey: Message, message: Message): Hasher;
60
+
61
+ /**
62
+ * Return hash in hex string.
63
+ *
64
+ * @param secretKey The Secret Key
65
+ * @param message The message you want to hash.
66
+ */
67
+ hex(secretKey: Message, message: Message): string;
68
+
69
+ /**
70
+ * Return hash in ArrayBuffer.
71
+ *
72
+ * @param secretKey The Secret Key
73
+ * @param message The message you want to hash.
74
+ */
75
+ arrayBuffer(secretKey: Message, message: Message): ArrayBuffer;
76
+
77
+ /**
78
+ * Return hash in integer array.
79
+ *
80
+ * @param secretKey The Secret Key
81
+ * @param message The message you want to hash.
82
+ */
83
+ digest(secretKey: Message, message: Message): number[];
84
+
85
+ /**
86
+ * Return hash in integer array.
87
+ *
88
+ * @param secretKey The Secret Key
89
+ * @param message The message you want to hash.
90
+ */
91
+ array(secretKey: Message, message: Message): number[];
92
+ }
93
+
94
+ interface Hash {
95
+ /**
96
+ * Hash and return hex string.
97
+ *
98
+ * @param message The message you want to hash.
99
+ */
100
+ (message: Message): string;
101
+
102
+ /**
103
+ * Create a hash object.
104
+ */
105
+ create(): Hasher;
106
+
107
+ /**
108
+ * Create a hash object and hash message.
109
+ *
110
+ * @param message The message you want to hash.
111
+ */
112
+ update(message: Message): Hasher;
113
+
114
+ /**
115
+ * Return hash in hex string.
116
+ *
117
+ * @param message The message you want to hash.
118
+ */
119
+ hex(message: Message): string;
120
+
121
+ /**
122
+ * Return hash in ArrayBuffer.
123
+ *
124
+ * @param message The message you want to hash.
125
+ */
126
+ arrayBuffer(message: Message): ArrayBuffer;
127
+
128
+ /**
129
+ * Return hash in integer array.
130
+ *
131
+ * @param message The message you want to hash.
132
+ */
133
+ digest(message: Message): number[];
134
+
135
+ /**
136
+ * Return hash in integer array.
137
+ *
138
+ * @param message The message you want to hash.
139
+ */
140
+ array(message: Message): number[];
141
+
142
+ /**
143
+ * HMAC interface
144
+ */
145
+ hmac: Hmac;
146
+ }
147
+
148
+ export var md5: Hash;
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "js-md5",
3
- "version": "0.7.3",
3
+ "version": "0.8.0",
4
4
  "description": "A simple MD5 hash function for JavaScript supports UTF-8 encoding.",
5
5
  "main": "src/md5.js",
6
6
  "devDependencies": {
7
7
  "expect.js": "~0.3.1",
8
- "jsdoc": "^3.4.0",
9
- "mocha": "~2.3.4",
10
- "nyc": "^11.3.0",
8
+ "jsdoc": "~4.0.2",
9
+ "mocha": "~10.2.0",
10
+ "nyc": "^15.1.0",
11
11
  "requirejs": "^2.1.22",
12
- "uglify-js": "^3.1.9",
13
- "webworker-threads": "^0.7.11"
12
+ "tiny-worker": "^2.3.0",
13
+ "uglify-js": "^3.1.9"
14
14
  },
15
15
  "scripts": {
16
16
  "test": "nyc mocha tests/node-test.js",
package/src/md5.js CHANGED
@@ -2,15 +2,16 @@
2
2
  * [js-md5]{@link https://github.com/emn178/js-md5}
3
3
  *
4
4
  * @namespace md5
5
- * @version 0.7.3
5
+ * @version 0.8.0
6
6
  * @author Chen, Yi-Cyuan [emn178@gmail.com]
7
- * @copyright Chen, Yi-Cyuan 2014-2017
7
+ * @copyright Chen, Yi-Cyuan 2014-2023
8
8
  * @license MIT
9
9
  */
10
10
  (function () {
11
11
  'use strict';
12
12
 
13
- var ERROR = 'input is invalid type';
13
+ var INPUT_ERROR = 'input is invalid type';
14
+ var FINALIZE_ERROR = 'finalize already called';
14
15
  var WINDOW = typeof window === 'object';
15
16
  var root = WINDOW ? window : {};
16
17
  if (root.JS_MD5_NO_WINDOW) {
@@ -39,18 +40,38 @@
39
40
  blocks = new Uint32Array(buffer);
40
41
  }
41
42
 
42
- if (root.JS_MD5_NO_NODE_JS || !Array.isArray) {
43
- Array.isArray = function (obj) {
43
+ var isArray = Array.isArray;
44
+ if (root.JS_MD5_NO_NODE_JS || !isArray) {
45
+ isArray = function (obj) {
44
46
  return Object.prototype.toString.call(obj) === '[object Array]';
45
47
  };
46
48
  }
47
49
 
48
- if (ARRAY_BUFFER && (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
49
- ArrayBuffer.isView = function (obj) {
50
+ var isView = ArrayBuffer.isView;
51
+ if (ARRAY_BUFFER && (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
52
+ isView = function (obj) {
50
53
  return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
51
54
  };
52
55
  }
53
56
 
57
+ // [message: string, isString: bool]
58
+ var formatMessage = function (message) {
59
+ var type = typeof message;
60
+ if (type === 'string') {
61
+ return [message, true];
62
+ }
63
+ if (type !== 'object' || message === null) {
64
+ throw new Error(INPUT_ERROR);
65
+ }
66
+ if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
67
+ return [new Uint8Array(message), false];
68
+ }
69
+ if (!isArray(message) && !isView(message)) {
70
+ throw new Error(INPUT_ERROR);
71
+ }
72
+ return [message, false];
73
+ }
74
+
54
75
  /**
55
76
  * @method hex
56
77
  * @memberof md5
@@ -153,21 +174,29 @@
153
174
  };
154
175
 
155
176
  var nodeWrap = function (method) {
156
- var crypto = eval("require('crypto')");
157
- var Buffer = eval("require('buffer').Buffer");
177
+ var crypto = require('crypto')
178
+ var Buffer = require('buffer').Buffer;
179
+ var bufferFrom;
180
+ if (Buffer.from && !root.JS_MD5_NO_BUFFER_FROM) {
181
+ bufferFrom = Buffer.from;
182
+ } else {
183
+ bufferFrom = function (message) {
184
+ return new Buffer(message);
185
+ };
186
+ }
158
187
  var nodeMethod = function (message) {
159
188
  if (typeof message === 'string') {
160
189
  return crypto.createHash('md5').update(message, 'utf8').digest('hex');
161
190
  } else {
162
191
  if (message === null || message === undefined) {
163
- throw ERROR;
192
+ throw new Error(INPUT_ERROR);
164
193
  } else if (message.constructor === ArrayBuffer) {
165
194
  message = new Uint8Array(message);
166
195
  }
167
196
  }
168
- if (Array.isArray(message) || ArrayBuffer.isView(message) ||
197
+ if (isArray(message) || isView(message) ||
169
198
  message.constructor === Buffer) {
170
- return crypto.createHash('md5').update(new Buffer(message)).digest('hex');
199
+ return crypto.createHash('md5').update(bufferFrom(message)).digest('hex');
171
200
  } else {
172
201
  return method(message);
173
202
  }
@@ -175,6 +204,116 @@
175
204
  return nodeMethod;
176
205
  };
177
206
 
207
+ /**
208
+ * @namespace md5.hmac
209
+ */
210
+ /**
211
+ * @method hex
212
+ * @memberof md5.hmac
213
+ * @description Output hash as hex string
214
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
215
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
216
+ * @returns {String} Hex string
217
+ * @example
218
+ * md5.hmac.hex('key', 'The quick brown fox jumps over the lazy dog');
219
+ * // equal to
220
+ * md5.hmac('key', 'The quick brown fox jumps over the lazy dog');
221
+ */
222
+
223
+ /**
224
+ * @method digest
225
+ * @memberof md5.hmac
226
+ * @description Output hash as bytes array
227
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
228
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
229
+ * @returns {Array} Bytes array
230
+ * @example
231
+ * md5.hmac.digest('key', 'The quick brown fox jumps over the lazy dog');
232
+ */
233
+ /**
234
+ * @method array
235
+ * @memberof md5.hmac
236
+ * @description Output hash as bytes array
237
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
238
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
239
+ * @returns {Array} Bytes array
240
+ * @example
241
+ * md5.hmac.array('key', 'The quick brown fox jumps over the lazy dog');
242
+ */
243
+ /**
244
+ * @method arrayBuffer
245
+ * @memberof md5.hmac
246
+ * @description Output hash as ArrayBuffer
247
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
248
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
249
+ * @returns {ArrayBuffer} ArrayBuffer
250
+ * @example
251
+ * md5.hmac.arrayBuffer('key', 'The quick brown fox jumps over the lazy dog');
252
+ */
253
+ /**
254
+ * @method buffer
255
+ * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
256
+ * @memberof md5.hmac
257
+ * @description Output hash as ArrayBuffer
258
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
259
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
260
+ * @returns {ArrayBuffer} ArrayBuffer
261
+ * @example
262
+ * md5.hmac.buffer('key', 'The quick brown fox jumps over the lazy dog');
263
+ */
264
+ /**
265
+ * @method base64
266
+ * @memberof md5.hmac
267
+ * @description Output hash as base64 string
268
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
269
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
270
+ * @returns {String} base64 string
271
+ * @example
272
+ * md5.hmac.base64('key', 'The quick brown fox jumps over the lazy dog');
273
+ */
274
+ var createHmacOutputMethod = function (outputType) {
275
+ return function (key, message) {
276
+ return new HmacMd5(key, true).update(message)[outputType]();
277
+ };
278
+ };
279
+
280
+ /**
281
+ * @method create
282
+ * @memberof md5.hmac
283
+ * @description Create HmacMd5 object
284
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
285
+ * @returns {HmacMd5} HmacMd5 object.
286
+ * @example
287
+ * var hash = md5.hmac.create('key');
288
+ */
289
+ /**
290
+ * @method update
291
+ * @memberof md5.hmac
292
+ * @description Create and update HmacMd5 object
293
+ * @param {String|Array|Uint8Array|ArrayBuffer} key key
294
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
295
+ * @returns {HmacMd5} HmacMd5 object.
296
+ * @example
297
+ * var hash = md5.hmac.update('key', 'The quick brown fox jumps over the lazy dog');
298
+ * // equal to
299
+ * var hash = md5.hmac.create('key');
300
+ * hash.update('The quick brown fox jumps over the lazy dog');
301
+ */
302
+ var createHmacMethod = function () {
303
+ var method = createHmacOutputMethod('hex');
304
+ method.create = function (key) {
305
+ return new HmacMd5(key);
306
+ };
307
+ method.update = function (key, message) {
308
+ return method.create(key).update(message);
309
+ };
310
+ for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
311
+ var type = OUTPUT_TYPES[i];
312
+ method[type] = createHmacOutputMethod(type);
313
+ }
314
+ return method;
315
+ };
316
+
178
317
  /**
179
318
  * Md5 class
180
319
  * @class Md5
@@ -214,26 +353,12 @@
214
353
  */
215
354
  Md5.prototype.update = function (message) {
216
355
  if (this.finalized) {
217
- return;
356
+ throw new Error(FINALIZE_ERROR);
218
357
  }
219
358
 
220
- var notString, type = typeof message;
221
- if (type !== 'string') {
222
- if (type === 'object') {
223
- if (message === null) {
224
- throw ERROR;
225
- } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
226
- message = new Uint8Array(message);
227
- } else if (!Array.isArray(message)) {
228
- if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
229
- throw ERROR;
230
- }
231
- }
232
- } else {
233
- throw ERROR;
234
- }
235
- notString = true;
236
- }
359
+ var result = formatMessage(message);
360
+ message = result[0];
361
+ var isString = result[1];
237
362
  var code, index = 0, i, length = message.length, blocks = this.blocks;
238
363
  var buffer8 = this.buffer8;
239
364
 
@@ -247,34 +372,24 @@
247
372
  blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
248
373
  }
249
374
 
250
- if (notString) {
251
- if (ARRAY_BUFFER) {
252
- for (i = this.start; index < length && i < 64; ++index) {
253
- buffer8[i++] = message[index];
254
- }
255
- } else {
256
- for (i = this.start; index < length && i < 64; ++index) {
257
- blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
258
- }
259
- }
260
- } else {
375
+ if (isString) {
261
376
  if (ARRAY_BUFFER) {
262
377
  for (i = this.start; index < length && i < 64; ++index) {
263
378
  code = message.charCodeAt(index);
264
379
  if (code < 0x80) {
265
380
  buffer8[i++] = code;
266
381
  } else if (code < 0x800) {
267
- buffer8[i++] = 0xc0 | (code >> 6);
382
+ buffer8[i++] = 0xc0 | (code >>> 6);
268
383
  buffer8[i++] = 0x80 | (code & 0x3f);
269
384
  } else if (code < 0xd800 || code >= 0xe000) {
270
- buffer8[i++] = 0xe0 | (code >> 12);
271
- buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
385
+ buffer8[i++] = 0xe0 | (code >>> 12);
386
+ buffer8[i++] = 0x80 | ((code >>> 6) & 0x3f);
272
387
  buffer8[i++] = 0x80 | (code & 0x3f);
273
388
  } else {
274
389
  code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
275
- buffer8[i++] = 0xf0 | (code >> 18);
276
- buffer8[i++] = 0x80 | ((code >> 12) & 0x3f);
277
- buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
390
+ buffer8[i++] = 0xf0 | (code >>> 18);
391
+ buffer8[i++] = 0x80 | ((code >>> 12) & 0x3f);
392
+ buffer8[i++] = 0x80 | ((code >>> 6) & 0x3f);
278
393
  buffer8[i++] = 0x80 | (code & 0x3f);
279
394
  }
280
395
  }
@@ -282,23 +397,33 @@
282
397
  for (i = this.start; index < length && i < 64; ++index) {
283
398
  code = message.charCodeAt(index);
284
399
  if (code < 0x80) {
285
- blocks[i >> 2] |= code << SHIFT[i++ & 3];
400
+ blocks[i >>> 2] |= code << SHIFT[i++ & 3];
286
401
  } else if (code < 0x800) {
287
- blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
288
- blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
402
+ blocks[i >>> 2] |= (0xc0 | (code >>> 6)) << SHIFT[i++ & 3];
403
+ blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
289
404
  } else if (code < 0xd800 || code >= 0xe000) {
290
- blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
291
- blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
292
- blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
405
+ blocks[i >>> 2] |= (0xe0 | (code >>> 12)) << SHIFT[i++ & 3];
406
+ blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
407
+ blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
293
408
  } else {
294
409
  code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
295
- blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
296
- blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
297
- blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
298
- blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
410
+ blocks[i >>> 2] |= (0xf0 | (code >>> 18)) << SHIFT[i++ & 3];
411
+ blocks[i >>> 2] |= (0x80 | ((code >>> 12) & 0x3f)) << SHIFT[i++ & 3];
412
+ blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
413
+ blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
299
414
  }
300
415
  }
301
416
  }
417
+ } else {
418
+ if (ARRAY_BUFFER) {
419
+ for (i = this.start; index < length && i < 64; ++index) {
420
+ buffer8[i++] = message[index];
421
+ }
422
+ } else {
423
+ for (i = this.start; index < length && i < 64; ++index) {
424
+ blocks[i >>> 2] |= message[index] << SHIFT[i++ & 3];
425
+ }
426
+ }
302
427
  }
303
428
  this.lastByteIndex = i;
304
429
  this.bytes += i - this.start;
@@ -323,7 +448,7 @@
323
448
  }
324
449
  this.finalized = true;
325
450
  var blocks = this.blocks, i = this.lastByteIndex;
326
- blocks[i >> 2] |= EXTRA[i & 3];
451
+ blocks[i >>> 2] |= EXTRA[i & 3];
327
452
  if (i >= 56) {
328
453
  if (!this.hashed) {
329
454
  this.hash();
@@ -524,22 +649,22 @@
524
649
 
525
650
  var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
526
651
 
527
- return HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
528
- HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
529
- HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
530
- HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
531
- HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
532
- HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
533
- HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
534
- HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
535
- HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
536
- HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
537
- HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
538
- HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
539
- HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
540
- HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
541
- HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
542
- HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F];
652
+ return HEX_CHARS[(h0 >>> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
653
+ HEX_CHARS[(h0 >>> 12) & 0x0F] + HEX_CHARS[(h0 >>> 8) & 0x0F] +
654
+ HEX_CHARS[(h0 >>> 20) & 0x0F] + HEX_CHARS[(h0 >>> 16) & 0x0F] +
655
+ HEX_CHARS[(h0 >>> 28) & 0x0F] + HEX_CHARS[(h0 >>> 24) & 0x0F] +
656
+ HEX_CHARS[(h1 >>> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
657
+ HEX_CHARS[(h1 >>> 12) & 0x0F] + HEX_CHARS[(h1 >>> 8) & 0x0F] +
658
+ HEX_CHARS[(h1 >>> 20) & 0x0F] + HEX_CHARS[(h1 >>> 16) & 0x0F] +
659
+ HEX_CHARS[(h1 >>> 28) & 0x0F] + HEX_CHARS[(h1 >>> 24) & 0x0F] +
660
+ HEX_CHARS[(h2 >>> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
661
+ HEX_CHARS[(h2 >>> 12) & 0x0F] + HEX_CHARS[(h2 >>> 8) & 0x0F] +
662
+ HEX_CHARS[(h2 >>> 20) & 0x0F] + HEX_CHARS[(h2 >>> 16) & 0x0F] +
663
+ HEX_CHARS[(h2 >>> 28) & 0x0F] + HEX_CHARS[(h2 >>> 24) & 0x0F] +
664
+ HEX_CHARS[(h3 >>> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
665
+ HEX_CHARS[(h3 >>> 12) & 0x0F] + HEX_CHARS[(h3 >>> 8) & 0x0F] +
666
+ HEX_CHARS[(h3 >>> 20) & 0x0F] + HEX_CHARS[(h3 >>> 16) & 0x0F] +
667
+ HEX_CHARS[(h3 >>> 28) & 0x0F] + HEX_CHARS[(h3 >>> 24) & 0x0F];
543
668
  };
544
669
 
545
670
  /**
@@ -569,10 +694,10 @@
569
694
 
570
695
  var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
571
696
  return [
572
- h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 24) & 0xFF,
573
- h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 24) & 0xFF,
574
- h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 24) & 0xFF,
575
- h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 24) & 0xFF
697
+ h0 & 0xFF, (h0 >>> 8) & 0xFF, (h0 >>> 16) & 0xFF, (h0 >>> 24) & 0xFF,
698
+ h1 & 0xFF, (h1 >>> 8) & 0xFF, (h1 >>> 16) & 0xFF, (h1 >>> 24) & 0xFF,
699
+ h2 & 0xFF, (h2 >>> 8) & 0xFF, (h2 >>> 16) & 0xFF, (h2 >>> 24) & 0xFF,
700
+ h3 & 0xFF, (h3 >>> 8) & 0xFF, (h3 >>> 16) & 0xFF, (h3 >>> 24) & 0xFF
576
701
  ];
577
702
  };
578
703
 
@@ -651,7 +776,75 @@
651
776
  return base64Str;
652
777
  };
653
778
 
779
+ /**
780
+ * HmacMd5 class
781
+ * @class HmacMd5
782
+ * @extends Md5
783
+ * @description This is internal class.
784
+ * @see {@link md5.hmac.create}
785
+ */
786
+ function HmacMd5(key, sharedMemory) {
787
+ var i, result = formatMessage(key);
788
+ key = result[0];
789
+ if (result[1]) {
790
+ var bytes = [], length = key.length, index = 0, code;
791
+ for (i = 0; i < length; ++i) {
792
+ code = key.charCodeAt(i);
793
+ if (code < 0x80) {
794
+ bytes[index++] = code;
795
+ } else if (code < 0x800) {
796
+ bytes[index++] = (0xc0 | (code >>> 6));
797
+ bytes[index++] = (0x80 | (code & 0x3f));
798
+ } else if (code < 0xd800 || code >= 0xe000) {
799
+ bytes[index++] = (0xe0 | (code >>> 12));
800
+ bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
801
+ bytes[index++] = (0x80 | (code & 0x3f));
802
+ } else {
803
+ code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
804
+ bytes[index++] = (0xf0 | (code >>> 18));
805
+ bytes[index++] = (0x80 | ((code >>> 12) & 0x3f));
806
+ bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
807
+ bytes[index++] = (0x80 | (code & 0x3f));
808
+ }
809
+ }
810
+ key = bytes;
811
+ }
812
+
813
+ if (key.length > 64) {
814
+ key = (new Md5(true)).update(key).array();
815
+ }
816
+
817
+ var oKeyPad = [], iKeyPad = [];
818
+ for (i = 0; i < 64; ++i) {
819
+ var b = key[i] || 0;
820
+ oKeyPad[i] = 0x5c ^ b;
821
+ iKeyPad[i] = 0x36 ^ b;
822
+ }
823
+
824
+ Md5.call(this, sharedMemory);
825
+
826
+ this.update(iKeyPad);
827
+ this.oKeyPad = oKeyPad;
828
+ this.inner = true;
829
+ this.sharedMemory = sharedMemory;
830
+ }
831
+ HmacMd5.prototype = new Md5();
832
+
833
+ HmacMd5.prototype.finalize = function () {
834
+ Md5.prototype.finalize.call(this);
835
+ if (this.inner) {
836
+ this.inner = false;
837
+ var innerHash = this.array();
838
+ Md5.call(this, this.sharedMemory);
839
+ this.update(this.oKeyPad);
840
+ this.update(innerHash);
841
+ Md5.prototype.finalize.call(this);
842
+ }
843
+ };
844
+
654
845
  var exports = createMethod();
846
+ exports.md5 = exports;
847
+ exports.md5.hmac = createHmacMethod();
655
848
 
656
849
  if (COMMON_JS) {
657
850
  module.exports = exports;