From bdf11500979645fe91732fa58467f8a313d9ca2c Mon Sep 17 00:00:00 2001 From: Yi-Cyuan Chen Date: Wed, 27 Sep 2023 21:35:56 +0800 Subject: [PATCH] ## v0.8.0 / 2023-09-27 ### Added - TypeScript interfaces. - HMAC feature. ### Fixed - deprecated `new Buffer`, replace with `Buffer.from`. - dependencies and security issues. - don't modify global Array and ArrayBuffer prototypes. - refactor: simplify formatMessage internal logic. ### Changed - remove `eval` and use `require` directly. - throw error by Error oject. - throw error if update after finalize - use unsigned right shift. --- .gitignore | 2 + CHANGELOG.md | 17 + LICENSE.txt | 2 +- README.md | 20 +- bower.json | 2 +- build/md5.min.js | 6 +- doc/HmacMd5.html | 1263 ++++++++++++++++++++++++ doc/Md5_.html | 578 ++++++++--- doc/global.html | 8 +- doc/index.html | 71 +- doc/md5.hmac.html | 1750 ++++++++++++++++++++++++++++++++++ doc/md5.html | 571 +++-------- doc/md5.js.html | 353 +++++-- doc/scripts/linenumber.js | 18 +- doc/styles/jsdoc-default.css | 4 +- index.d.ts | 148 +++ package-lock.json | 297 +++--- package.json | 8 +- src/md5.js | 349 +++++-- tests/hmac-test.js | 202 ++++ tests/index.html | 1 + tests/node-test.js | 10 +- tests/requirejs.html | 2 +- tests/test.js | 15 +- tests/worker-test.js | 3 + tests/worker.html | 1 - 26 files changed, 4814 insertions(+), 887 deletions(-) create mode 100644 doc/HmacMd5.html create mode 100644 doc/md5.hmac.html create mode 100644 index.d.ts create mode 100644 tests/hmac-test.js diff --git a/.gitignore b/.gitignore index 856f74c..aebac7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /node_modules/ /covreporter/ +/.nyc_output/ +/coverage/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f6a1be..6a252c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,22 @@ # Change Log +## v0.8.0 / 2023-09-27 +### Added +- TypeScript interfaces. +- HMAC feature. + +### Fixed +- deprecated `new Buffer`, replace with `Buffer.from`. +- dependencies and security issues. +- don't modify global Array and ArrayBuffer prototypes. +- refactor: simplify formatMessage internal logic. + +### Changed +- remove `eval` and use `require` directly. +- throw error by Error oject. +- throw error if update after finalize +- use unsigned right shift. + ## v0.7.3 / 2017-12-18 ### Fixed - incorrect result when first bit is 1 of bytes. #18 diff --git a/LICENSE.txt b/LICENSE.txt index 74ac20d..eb8e520 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright 2014-2017 Chen, Yi-Cyuan +Copyright 2014-2023 Chen, Yi-Cyuan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/README.md b/README.md index 9453292..398c890 100644 --- a/README.md +++ b/README.md @@ -32,11 +32,22 @@ md5('Message to hash'); var hash = md5.create(); hash.update('Message to hash'); hash.hex(); + +// HMAC +md5.hmac('key', 'Message to hash'); + +var hash = md5.hmac.create('key'); +hash.update('Message to hash'); +hash.hex(); ``` + +### Node.js If you use node.js, you should require the module first: ```JavaScript -md5 = require('js-md5'); +var md5 = require('js-md5'); ``` + +## RequireJS It supports AMD: ```JavaScript 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 md5.arrayBuffer(''); // ArrayBuffer md5.buffer(''); // ArrayBuffer, deprecated, This maybe confuse with Buffer in node.js. Please use arrayBuffer instead. md5.base64(''); // 1B2M2Y8AsgTpgAmY7PhCfg== + +// HMAC +md5.hmac.hex('key', 'Message to hash'); +md5.hmac.array('key', 'Message to hash'); +// ... ``` ## License -The project is released under the [MIT license](http://www.opensource.org/licenses/MIT). +The project is released under the [MIT license](https://opensource.org/license/mit/). ## Contact The project's website is located at https://github.com/emn178/js-md5 diff --git a/bower.json b/bower.json index 9f031b3..76db6cb 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "js-md5", - "version": "0.7.3", + "version": "0.8.0", "main": ["src/md5.js"], "ignore": [ "tests" diff --git a/build/md5.min.js b/build/md5.min.js index 7418ff8..76bcc0c 100644 --- a/build/md5.min.js +++ b/build/md5.min.js @@ -2,9 +2,9 @@ * [js-md5]{@link https://github.com/emn178/js-md5} * * @namespace md5 - * @version 0.7.3 + * @version 0.8.0 * @author Chen, Yi-Cyuan [emn178@gmail.com] - * @copyright Chen, Yi-Cyuan 2014-2017 + * @copyright Chen, Yi-Cyuan 2014-2023 * @license MIT */ -!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>2]|=t[f]<>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>2]|=s<>2]|=(192|s>>6)<>2]|=(128|63&s)<=57344?(n[h>>2]|=(224|s>>12)<>2]|=(128|s>>6&63)<>2]|=(128|63&s)<>2]|=(240|s>>18)<>2]|=(128|s>>12&63)<>2]|=(128|s>>6&63)<>2]|=(128|63&s)<=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 _}))}(); \ No newline at end of file +!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>>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>>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>>2]|=e<>>2]|=(192|e>>>6)<>>2]|=(128|63&e)<=57344?(a[i>>>2]|=(224|e>>>12)<>>2]|=(128|e>>>6&63)<>>2]|=(128|63&e)<>>2]|=(240|e>>>18)<>>2]|=(128|e>>>12&63)<>>2]|=(128|e>>>6&63)<>>2]|=(128|63&e)<>>2]|=t[h]<=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 + + + + JSDoc: Class: HmacMd5 + + + + + + + + + + +
+ +

Class: HmacMd5

+ + + + + + +
+ +
+ +

HmacMd5()

+ + +
+ +
+
+ + + + + + +

new HmacMd5()

+ + + + + + +
+ This is internal class. +
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + + + + + + + + + + + +
+ + +

Extends

+ + + + + + + + + + + + + + + + + + + + + + +

Methods

+ + + + + + + +

array() → {Array}

+ + + + + + +
+ Output hash as bytes array +
+ + + + + + + + + + + + + +
+ + + + + + +
Inherited From:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ Bytes array +
+ + + +
+
+ Type +
+
+ +Array + + +
+
+ + + + + + +
Example
+ +
hash.array();
+ + + + + + + + + +

arrayBuffer() → {ArrayBuffer}

+ + + + + + +
+ Output hash as ArrayBuffer +
+ + + + + + + + + + + + + +
+ + + + + + +
Inherited From:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ ArrayBuffer +
+ + + +
+
+ Type +
+
+ +ArrayBuffer + + +
+
+ + + + + + +
Example
+ +
hash.arrayBuffer();
+ + + + + + + + + +

base64() → {String}

+ + + + + + +
+ Output hash as base64 string +
+ + + + + + + + + + + + + +
+ + + + + + +
Inherited From:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ base64 string +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + + + +
Example
+ +
hash.base64();
+ + + + + + + + + +

buffer() → {ArrayBuffer}

+ + + + + + +
+ Output hash as ArrayBuffer +
+ + + + + + + + + + + + + +
+ + + + + + +
Inherited From:
+
+ + + + + + + + + + + +
Deprecated:
  • This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
+ + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ ArrayBuffer +
+ + + +
+
+ Type +
+
+ +ArrayBuffer + + +
+
+ + + + + + +
Example
+ +
hash.buffer();
+ + + + + + + + + +

digest() → {Array}

+ + + + + + +
+ Output hash as bytes array +
+ + + + + + + + + + + + + +
+ + + + + + +
Inherited From:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ Bytes array +
+ + + +
+
+ Type +
+
+ +Array + + +
+
+ + + + + + +
Example
+ +
hash.digest();
+ + + + + + + + + +

hex() → {String}

+ + + + + + +
+ Output hash as hex string +
+ + + + + + + + + + + + + +
+ + + + + + +
Inherited From:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ Hex string +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + + + +
Example
+ +
hash.hex();
+ + + + + + + + + +

toString() → {String}

+ + + + + + +
+ Output hash as hex string +
+ + + + + + + + + + + + + +
+ + + + + + +
Inherited From:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ Hex string +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + + + +
Example
+ +
hash.toString();
+ + + + + + + + + +

update(message) → {Md5}

+ + + + + + +
+ Update hash +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + + + + + +
+ + + + + + +
Inherited From:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + +
See:
+
+ +
+ + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ Md5 object. +
+ + + +
+
+ Type +
+
+ +Md5 + + +
+
+ + + + + + + + + + + + + +
+ +
+ + + + +
+ + + +
+ +
+ Documentation generated by JSDoc 4.0.2 on Wed Sep 27 2023 21:32:04 GMT+0800 (台北標準時間) +
+ + + + + \ No newline at end of file diff --git a/doc/Md5_.html b/doc/Md5_.html index d21980e..c447e32 100644 --- a/doc/Md5_.html +++ b/doc/Md5_.html @@ -2,7 +2,7 @@ - JSDoc: Class: Md5 + JSDoc: Namespace: md5 @@ -17,7 +17,7 @@
-

Class: Md5

+

Namespace: md5

@@ -28,7 +28,7 @@
-

Md5()

+

md5

@@ -37,36 +37,18 @@
+ + - - - -

new Md5()

- - - - - - -
- This is internal class. -
- - - - - - - - - - - +
+
Version:
+
  • 0.8.0
+ @@ -83,51 +65,42 @@ +
Author:
+
+
    +
  • Chen, Yi-Cyuan [emn178@gmail.com]
  • +
+
+ + + + +
License:
+
  • MIT
+
Source:
-
See:
-
- -
-
- - - - - - - - - - - - - - - - +
@@ -142,6 +115,13 @@ +

Namespaces

+ +
+
hmac
+
+
+ @@ -154,7 +134,7 @@ -

array() → {Array}

+

(static) array(message) → {Array}

@@ -173,6 +153,64 @@ +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + @@ -206,20 +244,13 @@
Source:
-
See:
-
- -
- @@ -236,6 +267,8 @@ + +
Returns:
@@ -264,7 +297,7 @@
Example
-
hash.array();
+
md5.array('The quick brown fox jumps over the lazy dog');
@@ -274,7 +307,7 @@ -

arrayBuffer() → {ArrayBuffer}

+

(static) arrayBuffer(message) → {ArrayBuffer}

@@ -293,6 +326,64 @@ +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + @@ -326,20 +417,13 @@
Source:
-
See:
-
- -
- @@ -356,6 +440,8 @@ + +
Returns:
@@ -384,7 +470,7 @@
Example
-
hash.arrayBuffer();
+
md5.arrayBuffer('The quick brown fox jumps over the lazy dog');
@@ -394,7 +480,7 @@ -

base64() → {String}

+

(static) base64(message) → {String}

@@ -413,6 +499,64 @@ +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + @@ -446,20 +590,13 @@
Source:
-
See:
-
- -
- @@ -476,6 +613,8 @@ + +
Returns:
@@ -504,7 +643,7 @@
Example
-
hash.base64();
+
md5.base64('The quick brown fox jumps over the lazy dog');
@@ -514,7 +653,7 @@ -

buffer() → {ArrayBuffer}

+

(static) buffer(message) → {ArrayBuffer}

@@ -533,6 +672,64 @@ +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + @@ -568,20 +765,13 @@
Source:
-
See:
-
- -
- @@ -598,6 +788,8 @@ + +
Returns:
@@ -626,7 +818,7 @@
Example
-
hash.buffer();
+
md5.buffer('The quick brown fox jumps over the lazy dog');
@@ -636,7 +828,7 @@ -

digest() → {Array}

+

(static) create() → {Md5}

@@ -644,7 +836,7 @@
- Output hash as bytes array + Create Md5 object
@@ -688,20 +880,13 @@
Source:
-
See:
-
- -
- @@ -718,11 +903,13 @@ + +
Returns:
- Bytes array + Md5 object.
@@ -733,7 +920,7 @@
-Array +Md5
@@ -746,7 +933,7 @@
Example
-
hash.digest();
+
var hash = md5.create();
@@ -756,7 +943,7 @@ -

hex() → {String}

+

(static) digest(message) → {Array}

@@ -764,7 +951,7 @@
- Output hash as hex string + Output hash as bytes array
@@ -775,6 +962,64 @@ +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + @@ -808,20 +1053,13 @@
Source:
-
See:
-
- -
- @@ -838,11 +1076,13 @@ + +
Returns:
- Hex string + Bytes array
@@ -853,7 +1093,7 @@
-String +Array
@@ -866,7 +1106,7 @@
Example
-
hash.hex();
+
md5.digest('The quick brown fox jumps over the lazy dog');
@@ -876,7 +1116,7 @@ -

toString() → {String}

+

(static) hex(message) → {String}

@@ -895,6 +1135,64 @@ +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + @@ -928,20 +1226,13 @@
Source:
-
See:
-
- -
- @@ -958,6 +1249,8 @@ + +
Returns:
@@ -986,7 +1279,9 @@
Example
-
hash.toString();
+
md5.hex('The quick brown fox jumps over the lazy dog');
+// equal to
+md5('The quick brown fox jumps over the lazy dog');
@@ -996,7 +1291,7 @@ -

update(message) → {Md5}

+

(static) update(message) → {Md5}

@@ -1004,7 +1299,7 @@
- Update hash + Create and update Md5 object
@@ -1106,20 +1401,13 @@
Source:
-
See:
-
- -
- @@ -1136,6 +1424,8 @@ + +
Returns:
@@ -1151,7 +1441,7 @@
-Md5 +Md5
@@ -1162,6 +1452,14 @@ +
Example
+ +
var hash = md5.update('The quick brown fox jumps over the lazy dog');
+// equal to
+var hash = md5.create();
+hash.update('The quick brown fox jumps over the lazy dog');
+ + @@ -1179,13 +1477,13 @@

- Documentation generated by JSDoc 3.5.5 on Mon Dec 18 2017 18:48:59 GMT+0800 (CST) + Documentation generated by JSDoc 4.0.2 on Wed Sep 27 2023 21:32:04 GMT+0800 (台北標準時間)
diff --git a/doc/global.html b/doc/global.html index b14ab72..9f20165 100644 --- a/doc/global.html +++ b/doc/global.html @@ -214,7 +214,7 @@
Source:
@@ -237,6 +237,8 @@ + +
Returns:
@@ -294,13 +296,13 @@ md5(new Uint8Array([])); // d41d8cd98f00b204e9800998ecf8427e
- Documentation generated by JSDoc 3.5.5 on Mon Dec 18 2017 18:48:59 GMT+0800 (CST) + Documentation generated by JSDoc 4.0.2 on Wed Sep 27 2023 21:32:04 GMT+0800 (台北標準時間)
diff --git a/doc/index.html b/doc/index.html index e3ab7b9..7244fe0 100644 --- a/doc/index.html +++ b/doc/index.html @@ -43,24 +43,53 @@
-

js-md5

Build Status -Coverage Status
NPM

+

js-md5

+

Build Status +Coverage Status
+NPM

A simple MD5 hash function for JavaScript supports UTF-8 encoding.

-

Demo

MD5 Online
MD5 File Checksum Online

-

Download

Compress
Uncompress

-

Installation

You can also install js-md5 by using Bower.

-
bower install md5

For node.js, you can use this command to install:

-
npm install js-md5

Notice

buffer method is deprecated. This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.

-

Usage

You could use like this:

+

Demo

+

MD5 Online
+MD5 File Checksum Online

+

Download

+

Compress
+Uncompress

+

Installation

+

You can also install js-md5 by using Bower.

+
bower install md5
+
+

For node.js, you can use this command to install:

+
npm install js-md5
+
+

Notice

+

buffer method is deprecated. This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.

+

Usage

+

You could use like this:

md5('Message to hash');
 var hash = md5.create();
 hash.update('Message to hash');
-hash.hex();

If you use node.js, you should require the module first:

-
md5 = require('js-md5');

It supports AMD:

+hash.hex(); + +// HMAC +md5.hmac('key', 'Message to hash'); + +var hash = md5.hmac.create('key'); +hash.update('Message to hash'); +hash.hex(); + +

Node.js

+

If you use node.js, you should require the module first:

+
var md5 = require('js-md5');
+
+

RequireJS

+

It supports AMD:

require(['your/path/md5.js'], function(md5) {
 // ...
-});

See document

-

Example

md5(''); // d41d8cd98f00b204e9800998ecf8427e
+});
+
+

See document

+

Example

+
md5(''); // d41d8cd98f00b204e9800998ecf8427e
 md5('The quick brown fox jumps over the lazy dog'); // 9e107d9d372bb6826bd81d3542a419d6
 md5('The quick brown fox jumps over the lazy dog.'); // e4d909c290d0fb1ca068ffaddf22cbd0
 
@@ -78,8 +107,18 @@ md5.array(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248
 md5.digest(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126]
 md5.arrayBuffer(''); // ArrayBuffer
 md5.buffer(''); // ArrayBuffer, deprecated, This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
-md5.base64(''); // 1B2M2Y8AsgTpgAmY7PhCfg==

License

The project is released under the MIT license.

-

Contact

The project's website is located at https://github.com/emn178/js-md5
Author: Chen, Yi-Cyuan (emn178@gmail.com)

+md5.base64(''); // 1B2M2Y8AsgTpgAmY7PhCfg== + +// HMAC +md5.hmac.hex('key', 'Message to hash'); +md5.hmac.array('key', 'Message to hash'); +// ... + +

License

+

The project is released under the MIT license.

+

Contact

+

The project's website is located at https://github.com/emn178/js-md5
+Author: Chen, Yi-Cyuan (emn178@gmail.com)

@@ -90,13 +129,13 @@ md5.base64(''); // 1B2M2Y8AsgTpgAmY7PhCfg==

License

The p


- Documentation generated by JSDoc 3.5.5 on Mon Dec 18 2017 18:48:59 GMT+0800 (CST) + Documentation generated by JSDoc 4.0.2 on Wed Sep 27 2023 21:32:04 GMT+0800 (台北標準時間)
diff --git a/doc/md5.hmac.html b/doc/md5.hmac.html new file mode 100644 index 0000000..99008e7 --- /dev/null +++ b/doc/md5.hmac.html @@ -0,0 +1,1750 @@ + + + + + JSDoc: Namespace: hmac + + + + + + + + + + +
+ +

Namespace: hmac

+ + + + + + +
+ +
+ +

+ md5.hmac

+ + +
+ +
+
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + +

Methods

+ + + + + + + +

(static) array(key, message) → {Array}

+ + + + + + +
+ Output hash as bytes array +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
key + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + key
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ Bytes array +
+ + + +
+
+ Type +
+
+ +Array + + +
+
+ + + + + + +
Example
+ +
md5.hmac.array('key', 'The quick brown fox jumps over the lazy dog');
+ + + + + + + + + +

(static) arrayBuffer(key, message) → {ArrayBuffer}

+ + + + + + +
+ Output hash as ArrayBuffer +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
key + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + key
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ ArrayBuffer +
+ + + +
+
+ Type +
+
+ +ArrayBuffer + + +
+
+ + + + + + +
Example
+ +
md5.hmac.arrayBuffer('key', 'The quick brown fox jumps over the lazy dog');
+ + + + + + + + + +

(static) base64(key, message) → {String}

+ + + + + + +
+ Output hash as base64 string +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
key + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + key
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ base64 string +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + + + +
Example
+ +
md5.hmac.base64('key', 'The quick brown fox jumps over the lazy dog');
+ + + + + + + + + +

(static) buffer(key, message) → {ArrayBuffer}

+ + + + + + +
+ Output hash as ArrayBuffer +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
key + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + key
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + + + + + +
+ + + + + + + + + + + + + + + + +
Deprecated:
  • This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
+ + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ ArrayBuffer +
+ + + +
+
+ Type +
+
+ +ArrayBuffer + + +
+
+ + + + + + +
Example
+ +
md5.hmac.buffer('key', 'The quick brown fox jumps over the lazy dog');
+ + + + + + + + + +

(static) create(key) → {HmacMd5}

+ + + + + + +
+ Create HmacMd5 object +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
key + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + key
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ HmacMd5 object. +
+ + + +
+
+ Type +
+
+ +HmacMd5 + + +
+
+ + + + + + +
Example
+ +
var hash = md5.hmac.create('key');
+ + + + + + + + + +

(static) digest(key, message) → {Array}

+ + + + + + +
+ Output hash as bytes array +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
key + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + key
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ Bytes array +
+ + + +
+
+ Type +
+
+ +Array + + +
+
+ + + + + + +
Example
+ +
md5.hmac.digest('key', 'The quick brown fox jumps over the lazy dog');
+ + + + + + + + + +

(static) hex(key, message) → {String}

+ + + + + + +
+ Output hash as hex string +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
key + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + key
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ Hex string +
+ + + +
+
+ Type +
+
+ +String + + +
+
+ + + + + + +
Example
+ +
md5.hmac.hex('key', 'The quick brown fox jumps over the lazy dog');
+// equal to
+md5.hmac('key', 'The quick brown fox jumps over the lazy dog');
+ + + + + + + + + +

(static) update(key, message) → {HmacMd5}

+ + + + + + +
+ Create and update HmacMd5 object +
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
key + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + key
message + + +String +| + +Array +| + +Uint8Array +| + +ArrayBuffer + + + + message to hash
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+ HmacMd5 object. +
+ + + +
+
+ Type +
+
+ +HmacMd5 + + +
+
+ + + + + + +
Example
+ +
var hash = md5.hmac.update('key', 'The quick brown fox jumps over the lazy dog');
+// equal to
+var hash = md5.hmac.create('key');
+hash.update('The quick brown fox jumps over the lazy dog');
+ + + + + + + + + +
+ +
+ + + + +
+ + + +
+ +
+ Documentation generated by JSDoc 4.0.2 on Wed Sep 27 2023 21:32:04 GMT+0800 (台北標準時間) +
+ + + + + \ No newline at end of file diff --git a/doc/md5.html b/doc/md5.html index 68718c6..0fc4b2b 100644 --- a/doc/md5.html +++ b/doc/md5.html @@ -2,7 +2,7 @@ - JSDoc: Namespace: md5 + JSDoc: Class: Md5 @@ -17,7 +17,7 @@
-

Namespace: md5

+

Class: Md5

@@ -28,7 +28,7 @@
-

md5

+

Md5()

@@ -37,22 +37,34 @@
- - - - - -
+ -
Version:
-
  • 0.7.3
+

new Md5()

- + + +
+ This is internal class. +
+ + + + + + + + + + + + + +
@@ -65,22 +77,15 @@ -
Author:
-
-
    -
  • Chen, Yi-Cyuan [emn178@gmail.com]
  • -
-
+ - - + -
License:
-
  • MIT
+ @@ -88,120 +93,86 @@
Source:
+
See:
+
+ +
+
- - -
- - - - - - - - -

Methods

- - - - - -

(static) array(message) → {Array}

- - -
- Output hash as bytes array -
+ +
+ + + + -
Parameters:
- - - - - - + - + - + +

Methods

+ - - - + - + +

array() → {Array}

- - - - + - - - - - - - -
NameTypeDescription
message - - -String -| -Array -| -Uint8Array -| +
+ Output hash as bytes array +
-ArrayBuffer - -
message to hash
@@ -237,13 +208,20 @@
Source:
+
See:
+
+ +
+ @@ -260,6 +238,8 @@ + +
Returns:
@@ -288,7 +268,7 @@
Example
-
md5.array('The quick brown fox jumps over the lazy dog');
+
hash.array();
@@ -298,7 +278,7 @@ -

(static) arrayBuffer(message) → {ArrayBuffer}

+

arrayBuffer() → {ArrayBuffer}

@@ -317,64 +297,6 @@ -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
message - - -String -| - -Array -| - -Uint8Array -| - -ArrayBuffer - - - - message to hash
- - @@ -408,13 +330,20 @@
Source:
+
See:
+
+ +
+ @@ -431,6 +360,8 @@ + +
Returns:
@@ -459,7 +390,7 @@
Example
-
md5.arrayBuffer('The quick brown fox jumps over the lazy dog');
+
hash.arrayBuffer();
@@ -469,7 +400,7 @@ -

(static) base64(message) → {String}

+

base64() → {String}

@@ -488,64 +419,6 @@ -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
message - - -String -| - -Array -| - -Uint8Array -| - -ArrayBuffer - - - - message to hash
- - @@ -579,13 +452,20 @@
Source:
+
See:
+
+ +
+ @@ -602,6 +482,8 @@ + +
Returns:
@@ -630,7 +512,7 @@
Example
-
md5.base64('The quick brown fox jumps over the lazy dog');
+
hash.base64();
@@ -640,7 +522,7 @@ -

(static) buffer(message) → {ArrayBuffer}

+

buffer() → {ArrayBuffer}

@@ -659,64 +541,6 @@ -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
message - - -String -| - -Array -| - -Uint8Array -| - -ArrayBuffer - - - - message to hash
- - @@ -752,13 +576,20 @@
Source:
+
See:
+
+ +
+ @@ -775,6 +606,8 @@ + +
Returns:
@@ -803,7 +636,7 @@
Example
-
md5.buffer('The quick brown fox jumps over the lazy dog');
+
hash.buffer();
@@ -813,7 +646,7 @@ -

(static) create() → {Md5}

+

digest() → {Array}

@@ -821,7 +654,7 @@
- Create Md5 object + Output hash as bytes array
@@ -865,13 +698,20 @@
Source:
+
See:
+
+ +
+ @@ -888,11 +728,13 @@ + +
Returns:
- Md5 object. + Bytes array
@@ -903,7 +745,7 @@
-Md5 +Array
@@ -916,7 +758,7 @@
Example
-
var hash = md5.create();
+
hash.digest();
@@ -926,7 +768,7 @@ -

(static) digest(message) → {Array}

+

hex() → {String}

@@ -934,7 +776,7 @@
- Output hash as bytes array + Output hash as hex string
@@ -945,64 +787,6 @@ -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
message - - -String -| - -Array -| - -Uint8Array -| - -ArrayBuffer - - - - message to hash
- - @@ -1036,13 +820,20 @@
Source:
+
See:
+
+ +
+ @@ -1059,11 +850,13 @@ + +
Returns:
- Bytes array + Hex string
@@ -1074,7 +867,7 @@
-Array +String
@@ -1087,7 +880,7 @@
Example
-
md5.digest('The quick brown fox jumps over the lazy dog');
+
hash.hex();
@@ -1097,7 +890,7 @@ -

(static) hex(message) → {String}

+

toString() → {String}

@@ -1116,64 +909,6 @@ -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
message - - -String -| - -Array -| - -Uint8Array -| - -ArrayBuffer - - - - message to hash
- - @@ -1207,13 +942,20 @@
Source:
+
See:
+
+ +
+ @@ -1230,6 +972,8 @@ + +
Returns:
@@ -1258,9 +1002,7 @@
Example
-
md5.hex('The quick brown fox jumps over the lazy dog');
-// equal to
-md5('The quick brown fox jumps over the lazy dog');
+
hash.toString();
@@ -1270,7 +1012,7 @@ md5('The quick brown fox jumps over the lazy dog'); -

(static) update(message) → {Md5}

+

update(message) → {Md5}

@@ -1278,7 +1020,7 @@ md5('The quick brown fox jumps over the lazy dog');
- Create and update Md5 object + Update hash
@@ -1380,13 +1122,20 @@ md5('The quick brown fox jumps over the lazy dog');
Source:
+
See:
+
+ +
+ @@ -1403,6 +1152,8 @@ md5('The quick brown fox jumps over the lazy dog'); + +
Returns:
@@ -1418,7 +1169,7 @@ md5('The quick brown fox jumps over the lazy dog');
-Md5 +Md5
@@ -1429,14 +1180,6 @@ md5('The quick brown fox jumps over the lazy dog'); -
Example
- -
var hash = md5.update('The quick brown fox jumps over the lazy dog');
-// equal to
-var hash = md5.create();
-hash.update('The quick brown fox jumps over the lazy dog');
- - @@ -1454,13 +1197,13 @@ hash.update('The quick brown fox jumps over the lazy dog');
- Documentation generated by JSDoc 3.5.5 on Mon Dec 18 2017 18:48:59 GMT+0800 (CST) + Documentation generated by JSDoc 4.0.2 on Wed Sep 27 2023 21:32:04 GMT+0800 (台北標準時間)
diff --git a/doc/md5.js.html b/doc/md5.js.html index f5376b3..9fb028d 100644 --- a/doc/md5.js.html +++ b/doc/md5.js.html @@ -30,15 +30,16 @@ * [js-md5]{@link https://github.com/emn178/js-md5} * * @namespace md5 - * @version 0.7.3 + * @version 0.8.0 * @author Chen, Yi-Cyuan [emn178@gmail.com] - * @copyright Chen, Yi-Cyuan 2014-2017 + * @copyright Chen, Yi-Cyuan 2014-2023 * @license MIT */ (function () { 'use strict'; - var ERROR = 'input is invalid type'; + 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_MD5_NO_WINDOW) { @@ -67,18 +68,38 @@ blocks = new Uint32Array(buffer); } - if (root.JS_MD5_NO_NODE_JS || !Array.isArray) { - Array.isArray = function (obj) { + var isArray = Array.isArray; + if (root.JS_MD5_NO_NODE_JS || !isArray) { + isArray = function (obj) { return Object.prototype.toString.call(obj) === '[object Array]'; }; } - if (ARRAY_BUFFER && (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) { - ArrayBuffer.isView = function (obj) { + var isView = ArrayBuffer.isView; + if (ARRAY_BUFFER && (root.JS_MD5_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]; + } + /** * @method hex * @memberof md5 @@ -181,21 +202,29 @@ }; 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_MD5_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('md5').update(message, 'utf8').digest('hex'); } else { if (message === null || message === undefined) { - throw ERROR; + throw new Error(INPUT_ERROR); } else if (message.constructor === ArrayBuffer) { message = new Uint8Array(message); } } - if (Array.isArray(message) || ArrayBuffer.isView(message) || + if (isArray(message) || isView(message) || message.constructor === Buffer) { - return crypto.createHash('md5').update(new Buffer(message)).digest('hex'); + return crypto.createHash('md5').update(bufferFrom(message)).digest('hex'); } else { return method(message); } @@ -203,6 +232,116 @@ return nodeMethod; }; + /** + * @namespace md5.hmac + */ + /** + * @method hex + * @memberof md5.hmac + * @description Output hash as hex string + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {String} Hex string + * @example + * md5.hmac.hex('key', 'The quick brown fox jumps over the lazy dog'); + * // equal to + * md5.hmac('key', 'The quick brown fox jumps over the lazy dog'); + */ + + /** + * @method digest + * @memberof md5.hmac + * @description Output hash as bytes array + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {Array} Bytes array + * @example + * md5.hmac.digest('key', 'The quick brown fox jumps over the lazy dog'); + */ + /** + * @method array + * @memberof md5.hmac + * @description Output hash as bytes array + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {Array} Bytes array + * @example + * md5.hmac.array('key', 'The quick brown fox jumps over the lazy dog'); + */ + /** + * @method arrayBuffer + * @memberof md5.hmac + * @description Output hash as ArrayBuffer + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {ArrayBuffer} ArrayBuffer + * @example + * md5.hmac.arrayBuffer('key', 'The quick brown fox jumps over the lazy dog'); + */ + /** + * @method buffer + * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead. + * @memberof md5.hmac + * @description Output hash as ArrayBuffer + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {ArrayBuffer} ArrayBuffer + * @example + * md5.hmac.buffer('key', 'The quick brown fox jumps over the lazy dog'); + */ + /** + * @method base64 + * @memberof md5.hmac + * @description Output hash as base64 string + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {String} base64 string + * @example + * md5.hmac.base64('key', 'The quick brown fox jumps over the lazy dog'); + */ + var createHmacOutputMethod = function (outputType) { + return function (key, message) { + return new HmacMd5(key, true).update(message)[outputType](); + }; + }; + + /** + * @method create + * @memberof md5.hmac + * @description Create HmacMd5 object + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @returns {HmacMd5} HmacMd5 object. + * @example + * var hash = md5.hmac.create('key'); + */ + /** + * @method update + * @memberof md5.hmac + * @description Create and update HmacMd5 object + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {HmacMd5} HmacMd5 object. + * @example + * var hash = md5.hmac.update('key', 'The quick brown fox jumps over the lazy dog'); + * // equal to + * var hash = md5.hmac.create('key'); + * hash.update('The quick brown fox jumps over the lazy dog'); + */ + var createHmacMethod = function () { + var method = createHmacOutputMethod('hex'); + method.create = function (key) { + return new HmacMd5(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; + }; + /** * Md5 class * @class Md5 @@ -242,26 +381,12 @@ */ Md5.prototype.update = function (message) { if (this.finalized) { - return; + throw new Error(FINALIZE_ERROR); } - var notString, type = typeof message; - if (type !== 'string') { - if (type === 'object') { - if (message === null) { - throw ERROR; - } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { - message = new Uint8Array(message); - } else if (!Array.isArray(message)) { - if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) { - throw ERROR; - } - } - } else { - throw ERROR; - } - notString = true; - } + var result = formatMessage(message); + message = result[0]; + var isString = result[1]; var code, index = 0, i, length = message.length, blocks = this.blocks; var buffer8 = this.buffer8; @@ -275,34 +400,24 @@ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; } - if (notString) { - if (ARRAY_BUFFER) { - for (i = this.start; index < length && i < 64; ++index) { - buffer8[i++] = message[index]; - } - } else { - for (i = this.start; index < length && i < 64; ++index) { - blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; - } - } - } else { + if (isString) { if (ARRAY_BUFFER) { for (i = this.start; index < length && i < 64; ++index) { code = message.charCodeAt(index); if (code < 0x80) { buffer8[i++] = code; } else if (code < 0x800) { - buffer8[i++] = 0xc0 | (code >> 6); + buffer8[i++] = 0xc0 | (code >>> 6); buffer8[i++] = 0x80 | (code & 0x3f); } else if (code < 0xd800 || code >= 0xe000) { - buffer8[i++] = 0xe0 | (code >> 12); - buffer8[i++] = 0x80 | ((code >> 6) & 0x3f); + buffer8[i++] = 0xe0 | (code >>> 12); + buffer8[i++] = 0x80 | ((code >>> 6) & 0x3f); buffer8[i++] = 0x80 | (code & 0x3f); } else { code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); - buffer8[i++] = 0xf0 | (code >> 18); - buffer8[i++] = 0x80 | ((code >> 12) & 0x3f); - buffer8[i++] = 0x80 | ((code >> 6) & 0x3f); + buffer8[i++] = 0xf0 | (code >>> 18); + buffer8[i++] = 0x80 | ((code >>> 12) & 0x3f); + buffer8[i++] = 0x80 | ((code >>> 6) & 0x3f); buffer8[i++] = 0x80 | (code & 0x3f); } } @@ -310,23 +425,33 @@ 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 { + if (ARRAY_BUFFER) { + for (i = this.start; index < length && i < 64; ++index) { + buffer8[i++] = message[index]; + } + } else { + for (i = this.start; index < length && i < 64; ++index) { + blocks[i >>> 2] |= message[index] << SHIFT[i++ & 3]; + } + } } this.lastByteIndex = i; this.bytes += i - this.start; @@ -351,7 +476,7 @@ } this.finalized = true; var blocks = this.blocks, i = this.lastByteIndex; - blocks[i >> 2] |= EXTRA[i & 3]; + blocks[i >>> 2] |= EXTRA[i & 3]; if (i >= 56) { if (!this.hashed) { this.hash(); @@ -552,22 +677,22 @@ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3; - return HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] + - HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] + - HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] + - HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] + - HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] + - HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] + - HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] + - HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] + - HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] + - HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] + - HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] + - HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] + - HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] + - HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] + - HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] + - HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F]; + return HEX_CHARS[(h0 >>> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] + + HEX_CHARS[(h0 >>> 12) & 0x0F] + HEX_CHARS[(h0 >>> 8) & 0x0F] + + HEX_CHARS[(h0 >>> 20) & 0x0F] + HEX_CHARS[(h0 >>> 16) & 0x0F] + + HEX_CHARS[(h0 >>> 28) & 0x0F] + HEX_CHARS[(h0 >>> 24) & 0x0F] + + HEX_CHARS[(h1 >>> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] + + HEX_CHARS[(h1 >>> 12) & 0x0F] + HEX_CHARS[(h1 >>> 8) & 0x0F] + + HEX_CHARS[(h1 >>> 20) & 0x0F] + HEX_CHARS[(h1 >>> 16) & 0x0F] + + HEX_CHARS[(h1 >>> 28) & 0x0F] + HEX_CHARS[(h1 >>> 24) & 0x0F] + + HEX_CHARS[(h2 >>> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] + + HEX_CHARS[(h2 >>> 12) & 0x0F] + HEX_CHARS[(h2 >>> 8) & 0x0F] + + HEX_CHARS[(h2 >>> 20) & 0x0F] + HEX_CHARS[(h2 >>> 16) & 0x0F] + + HEX_CHARS[(h2 >>> 28) & 0x0F] + HEX_CHARS[(h2 >>> 24) & 0x0F] + + HEX_CHARS[(h3 >>> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] + + HEX_CHARS[(h3 >>> 12) & 0x0F] + HEX_CHARS[(h3 >>> 8) & 0x0F] + + HEX_CHARS[(h3 >>> 20) & 0x0F] + HEX_CHARS[(h3 >>> 16) & 0x0F] + + HEX_CHARS[(h3 >>> 28) & 0x0F] + HEX_CHARS[(h3 >>> 24) & 0x0F]; }; /** @@ -597,10 +722,10 @@ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3; return [ - h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 24) & 0xFF, - h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 24) & 0xFF, - h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 24) & 0xFF, - h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 24) & 0xFF + h0 & 0xFF, (h0 >>> 8) & 0xFF, (h0 >>> 16) & 0xFF, (h0 >>> 24) & 0xFF, + h1 & 0xFF, (h1 >>> 8) & 0xFF, (h1 >>> 16) & 0xFF, (h1 >>> 24) & 0xFF, + h2 & 0xFF, (h2 >>> 8) & 0xFF, (h2 >>> 16) & 0xFF, (h2 >>> 24) & 0xFF, + h3 & 0xFF, (h3 >>> 8) & 0xFF, (h3 >>> 16) & 0xFF, (h3 >>> 24) & 0xFF ]; }; @@ -679,7 +804,75 @@ return base64Str; }; + /** + * HmacMd5 class + * @class HmacMd5 + * @extends Md5 + * @description This is internal class. + * @see {@link md5.hmac.create} + */ + function HmacMd5(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 Md5(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; + } + + Md5.call(this, sharedMemory); + + this.update(iKeyPad); + this.oKeyPad = oKeyPad; + this.inner = true; + this.sharedMemory = sharedMemory; + } + HmacMd5.prototype = new Md5(); + + HmacMd5.prototype.finalize = function () { + Md5.prototype.finalize.call(this); + if (this.inner) { + this.inner = false; + var innerHash = this.array(); + Md5.call(this, this.sharedMemory); + this.update(this.oKeyPad); + this.update(innerHash); + Md5.prototype.finalize.call(this); + } + }; + var exports = createMethod(); + exports.md5 = exports; + exports.md5.hmac = createHmacMethod(); if (COMMON_JS) { module.exports = exports; @@ -719,13 +912,13 @@
- Documentation generated by JSDoc 3.5.5 on Mon Dec 18 2017 18:48:59 GMT+0800 (CST) + Documentation generated by JSDoc 4.0.2 on Wed Sep 27 2023 21:32:04 GMT+0800 (台北標準時間)
diff --git a/doc/scripts/linenumber.js b/doc/scripts/linenumber.js index 8d52f7e..4354785 100644 --- a/doc/scripts/linenumber.js +++ b/doc/scripts/linenumber.js @@ -1,12 +1,12 @@ /*global document */ -(function() { - var source = document.getElementsByClassName('prettyprint source linenums'); - var i = 0; - var lineNumber = 0; - var lineId; - var lines; - var totalLines; - var anchorHash; +(() => { + const source = document.getElementsByClassName('prettyprint source linenums'); + let i = 0; + let lineNumber = 0; + let lineId; + let lines; + let totalLines; + let anchorHash; if (source && source[0]) { anchorHash = document.location.hash.substring(1); @@ -15,7 +15,7 @@ for (; i < totalLines; i++) { lineNumber++; - lineId = 'line' + lineNumber; + lineId = `line${lineNumber}`; lines[i].id = lineId; if (lineId === anchorHash) { lines[i].className += ' selected'; diff --git a/doc/styles/jsdoc-default.css b/doc/styles/jsdoc-default.css index 9207bc8..7d1729d 100644 --- a/doc/styles/jsdoc-default.css +++ b/doc/styles/jsdoc-default.css @@ -273,7 +273,7 @@ tr > th:last-child { border-right: 1px solid #ddd; } margin: 0; } -.prettyprint +.source { border: 1px solid #ddd; width: 80%; @@ -284,7 +284,7 @@ tr > th:last-child { border-right: 1px solid #ddd; } width: inherit; } -.prettyprint code +.source code { font-size: 100%; line-height: 18px; diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..bf64806 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,148 @@ +type Message = string | number[] | ArrayBuffer | Uint8Array; + +interface Hasher { + /** + * Update hash + * + * @param message The message you want to hash. + */ + update(message: Message): Hasher; + + /** + * Return hash in hex string. + */ + hex(): string; + + /** + * Return hash in hex string. + */ + toString(): string; + + /** + * Return hash in ArrayBuffer. + */ + arrayBuffer(): ArrayBuffer; + + /** + * Return hash in integer array. + */ + digest(): number[]; + + /** + * Return hash in integer array. + */ + array(): number[]; +} + +interface Hmac { + /** + * Computes a Hash-based message authentication code (HMAC) using a secret key + * + * @param secretKey The Secret Key + * @param message The message you want to hash. + */ + (secretKey: Message, message: Message): string; + + /** + * Create a hash object using a secret key. + * + * @param secretKey The Secret Key + */ + create(secretKey: Message): Hasher; + + /** + * Create a hash object and hash message using a secret key + * + * @param secretKey The Secret Key + * @param message The message you want to hash. + */ + update(secretKey: Message, message: Message): Hasher; + + /** + * Return hash in hex string. + * + * @param secretKey The Secret Key + * @param message The message you want to hash. + */ + hex(secretKey: Message, message: Message): string; + + /** + * Return hash in ArrayBuffer. + * + * @param secretKey The Secret Key + * @param message The message you want to hash. + */ + arrayBuffer(secretKey: Message, message: Message): ArrayBuffer; + + /** + * Return hash in integer array. + * + * @param secretKey The Secret Key + * @param message The message you want to hash. + */ + digest(secretKey: Message, message: Message): number[]; + + /** + * Return hash in integer array. + * + * @param secretKey The Secret Key + * @param message The message you want to hash. + */ + array(secretKey: Message, message: Message): number[]; +} + +interface Hash { + /** + * Hash and return hex string. + * + * @param message The message you want to hash. + */ + (message: Message): string; + + /** + * Create a hash object. + */ + create(): Hasher; + + /** + * Create a hash object and hash message. + * + * @param message The message you want to hash. + */ + update(message: Message): Hasher; + + /** + * Return hash in hex string. + * + * @param message The message you want to hash. + */ + hex(message: Message): string; + + /** + * Return hash in ArrayBuffer. + * + * @param message The message you want to hash. + */ + arrayBuffer(message: Message): ArrayBuffer; + + /** + * Return hash in integer array. + * + * @param message The message you want to hash. + */ + digest(message: Message): number[]; + + /** + * Return hash in integer array. + * + * @param message The message you want to hash. + */ + array(message: Message): number[]; + + /** + * HMAC interface + */ + hmac: Hmac; +} + +export var md5: Hash; diff --git a/package-lock.json b/package-lock.json index e48c8f4..dbec3f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -417,6 +417,37 @@ "@jridgewell/sourcemap-codec": "1.4.14" } }, + "@jsdoc/salty": { + "version": "0.2.5", + "resolved": "https://registry.npmjs.org/@jsdoc/salty/-/salty-0.2.5.tgz", + "integrity": "sha512-TfRP53RqunNe2HBobVBJ0VLhK1HbfvBYeTC1ahnN64PWvyYyGebmMiPkuwvD9fpw2ZbkoPb8Q7mwy0aR8Z9rvw==", + "dev": true, + "requires": { + "lodash": "^4.17.21" + } + }, + "@types/linkify-it": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/@types/linkify-it/-/linkify-it-3.0.3.tgz", + "integrity": "sha512-pTjcqY9E4nOI55Wgpz7eiI8+LzdYnw3qxXCfHyBDdPbYvbyLgWLJGh8EdPvqawwMK1Uo1794AUkkR38Fr0g+2g==", + "dev": true + }, + "@types/markdown-it": { + "version": "12.2.3", + "resolved": "https://registry.npmjs.org/@types/markdown-it/-/markdown-it-12.2.3.tgz", + "integrity": "sha512-GKMHFfv3458yYy+v/N8gjufHO6MSZKCOXpZc5GXIWWy8uldwfmPn98vp81gZ5f9SVw8YYBctgfJ22a2d7AOMeQ==", + "dev": true, + "requires": { + "@types/linkify-it": "*", + "@types/mdurl": "*" + } + }, + "@types/mdurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@types/mdurl/-/mdurl-1.0.2.tgz", + "integrity": "sha512-eC4U9MlIcu2q0KQmXszyn5Akca/0jrQmwDRgpAMJai7qBWq4amIQhZyNau4VYGtCeALvW1/NtjzJJ567aZxfKA==", + "dev": true + }, "aggregate-error": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz", @@ -479,12 +510,6 @@ "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", "dev": true }, - "babylon": { - "version": "7.0.0-beta.19", - "resolved": "https://registry.npmjs.org/babylon/-/babylon-7.0.0-beta.19.tgz", - "integrity": "sha512-Vg0C9s/REX6/WIXN37UKpv5ZhRi6A4pjHlpkE34+8/a6c2W1Q692n3hmc+SZG5lKRnaExLUbxtJ1SVT+KaCQ/A==", - "dev": true - }, "balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -497,16 +522,10 @@ "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", "dev": true }, - "bindings": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.3.0.tgz", - "integrity": "sha512-DpLh5EzMR2kzvX1KIlVC0VkC3iZtHKTgdtZ0a3pglBZdaQFjt5S9g9xd1lE+YvXyfd6mtCeRnrUfOLYiTMlNSw==", - "dev": true - }, "bluebird": { - "version": "3.5.1", - "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", - "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==", + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", + "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==", "dev": true }, "brace-expansion": { @@ -571,12 +590,12 @@ "dev": true }, "catharsis": { - "version": "0.8.9", - "resolved": "https://registry.npmjs.org/catharsis/-/catharsis-0.8.9.tgz", - "integrity": "sha1-mMyJDKZS3S7w5ws3klMQ/56Q/Is=", + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/catharsis/-/catharsis-0.9.0.tgz", + "integrity": "sha512-prMTQVpcns/tzFgFVkVp6ak6RykZyWb3gu8ckUpd6YkTlacOd3DXGJjIpD4Q6zJirizvaiAjSSHlOsA+6sNh2A==", "dev": true, "requires": { - "underscore-contrib": "0.3.0" + "lodash": "^4.17.15" } }, "chalk": { @@ -727,6 +746,12 @@ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", "dev": true }, + "entities": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-2.1.0.tgz", + "integrity": "sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w==", + "dev": true + }, "es6-error": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/es6-error/-/es6-error-4.1.1.tgz", @@ -745,6 +770,12 @@ "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", "dev": true }, + "esm": { + "version": "3.2.25", + "resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz", + "integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==", + "dev": true + }, "esprima": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", @@ -881,9 +912,9 @@ "dev": true }, "graceful-fs": { - "version": "4.1.11", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz", - "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=", + "version": "4.2.11", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", + "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==", "dev": true }, "has-flag": { @@ -1114,32 +1145,43 @@ } }, "js2xmlparser": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/js2xmlparser/-/js2xmlparser-3.0.0.tgz", - "integrity": "sha1-P7YOqgicVED5MZ9RdgzNB+JJlzM=", + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/js2xmlparser/-/js2xmlparser-4.0.2.tgz", + "integrity": "sha512-6n4D8gLlLf1n5mNLQPRfViYzu9RATblzPEtm1SthMX1Pjao0r9YI9nw7ZIfRxQMERS87mcswrg+r/OYrPRX6jA==", "dev": true, "requires": { - "xmlcreate": "1.0.2" + "xmlcreate": "^2.0.4" } }, "jsdoc": { - "version": "3.5.5", - "resolved": "https://registry.npmjs.org/jsdoc/-/jsdoc-3.5.5.tgz", - "integrity": "sha512-6PxB65TAU4WO0Wzyr/4/YhlGovXl0EVYfpKbpSroSj0qBxT4/xod/l40Opkm38dRHRdQgdeY836M0uVnJQG7kg==", - "dev": true, - "requires": { - "babylon": "7.0.0-beta.19", - "bluebird": "3.5.1", - "catharsis": "0.8.9", - "escape-string-regexp": "1.0.5", - "js2xmlparser": "3.0.0", - "klaw": "2.0.0", - "marked": "0.3.6", - "mkdirp": "0.5.1", - "requizzle": "0.2.1", - "strip-json-comments": "2.0.1", - "taffydb": "2.6.2", - "underscore": "1.8.3" + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/jsdoc/-/jsdoc-4.0.2.tgz", + "integrity": "sha512-e8cIg2z62InH7azBBi3EsSEqrKx+nUtAS5bBcYTSpZFA+vhNPyhv8PTFZ0WsjOPDj04/dOLlm08EDcQJDqaGQg==", + "dev": true, + "requires": { + "@babel/parser": "^7.20.15", + "@jsdoc/salty": "^0.2.1", + "@types/markdown-it": "^12.2.3", + "bluebird": "^3.7.2", + "catharsis": "^0.9.0", + "escape-string-regexp": "^2.0.0", + "js2xmlparser": "^4.0.2", + "klaw": "^3.0.0", + "markdown-it": "^12.3.2", + "markdown-it-anchor": "^8.4.1", + "marked": "^4.0.10", + "mkdirp": "^1.0.4", + "requizzle": "^0.2.3", + "strip-json-comments": "^3.1.0", + "underscore": "~1.13.2" + }, + "dependencies": { + "escape-string-regexp": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-2.0.0.tgz", + "integrity": "sha512-UpzcLCXolUWcNu5HtVMHYdXJjArjsF9C0aNnquZYY4uW/Vu0miy5YoWvbV345HauVvcAUnpRuhMMcqTcGOY2+w==", + "dev": true + } } }, "jsesc": { @@ -1155,12 +1197,21 @@ "dev": true }, "klaw": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/klaw/-/klaw-2.0.0.tgz", - "integrity": "sha1-WcEo4Nxc5BAgEVEZTuucv4WGUPY=", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/klaw/-/klaw-3.0.0.tgz", + "integrity": "sha512-0Fo5oir+O9jnXu5EefYbVK+mHMBeEVEy2cmctR1O1NECcCkPRreJKrS6Qt/j3KC2C148Dfo9i3pCmCMsdqGr0g==", + "dev": true, + "requires": { + "graceful-fs": "^4.1.9" + } + }, + "linkify-it": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-3.0.3.tgz", + "integrity": "sha512-ynTsyrFSdE5oZ/O9GEf00kPngmOfVwazR5GKDq6EYfhlpFug3J2zybX56a2PRRpc9P+FuSoGNAwjlbDs9jJBPQ==", "dev": true, "requires": { - "graceful-fs": "4.1.11" + "uc.micro": "^1.0.1" } }, "locate-path": { @@ -1172,6 +1223,12 @@ "p-locate": "^5.0.0" } }, + "lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true + }, "lodash.flattendeep": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz", @@ -1206,10 +1263,35 @@ "semver": "^6.0.0" } }, + "markdown-it": { + "version": "12.3.2", + "resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-12.3.2.tgz", + "integrity": "sha512-TchMembfxfNVpHkbtriWltGWc+m3xszaRD0CZup7GFFhzIgQqxIfn3eGj1yZpfuflzPvfkt611B2Q/Bsk1YnGg==", + "dev": true, + "requires": { + "argparse": "^2.0.1", + "entities": "~2.1.0", + "linkify-it": "^3.0.1", + "mdurl": "^1.0.1", + "uc.micro": "^1.0.5" + } + }, + "markdown-it-anchor": { + "version": "8.6.7", + "resolved": "https://registry.npmjs.org/markdown-it-anchor/-/markdown-it-anchor-8.6.7.tgz", + "integrity": "sha512-FlCHFwNnutLgVTflOYHPW2pPcl2AACqVzExlkGQNsi4CJgqOHN7YTgDd4LuhgN1BFO3TS0vLAruV1Td6dwWPJA==", + "dev": true + }, "marked": { - "version": "0.3.6", - "resolved": "https://registry.npmjs.org/marked/-/marked-0.3.6.tgz", - "integrity": "sha1-ssbGGPzOzk74bE/Gy4p8v1rtqNc=", + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/marked/-/marked-4.3.0.tgz", + "integrity": "sha512-PRsaiG84bK+AMvxziE/lCFss8juXjNaWzVbN5tXAm4XjeaS9NAHhop+PjQxz2A9h8Q4M/xGmzP8vqNwy6JeK0A==", + "dev": true + }, + "mdurl": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/mdurl/-/mdurl-1.0.1.tgz", + "integrity": "sha512-/sKlQJCBYVY9Ers9hqzKou4H6V5UWc/M59TH2dvkt+84itfnq7uFOMLpOiOS4ujvHP4etln18fmIxA5R5fll0g==", "dev": true }, "minimatch": { @@ -1232,20 +1314,11 @@ } } }, - "minimist": { - "version": "0.0.8", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", - "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", - "dev": true - }, "mkdirp": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", - "integrity": "sha512-SknJC52obPfGQPnjIkXbmA6+5H15E+fR+E4iR2oQ3zzCLbd7/ONua69R/Gw7AgkTLsRG+r5fzksYwWe1AgTyWA==", - "dev": true, - "requires": { - "minimist": "0.0.8" - } + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", + "dev": true }, "mocha": { "version": "10.2.0", @@ -1296,12 +1369,6 @@ "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", "dev": true }, - "nan": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/nan/-/nan-2.7.0.tgz", - "integrity": "sha1-2Vv3IeyHfgjbJ27T/G63j5CDrUY=", - "dev": true - }, "nanoid": { "version": "3.3.3", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.3.tgz", @@ -1667,20 +1734,12 @@ "dev": true }, "requizzle": { - "version": "0.2.1", - "resolved": "https://registry.npmjs.org/requizzle/-/requizzle-0.2.1.tgz", - "integrity": "sha1-aUPDUwxNmn5G8c3dUcFY/GcM294=", + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/requizzle/-/requizzle-0.2.4.tgz", + "integrity": "sha512-JRrFk1D4OQ4SqovXOgdav+K8EAhSB/LJZqCz8tbX0KObcdeM15Ss59ozWMBWmmINMagCwmqn4ZNryUGpBsl6Jw==", "dev": true, "requires": { - "underscore": "1.6.0" - }, - "dependencies": { - "underscore": { - "version": "1.6.0", - "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.6.0.tgz", - "integrity": "sha1-izixDKze9jM3uLJOT/htRa6lKag=", - "dev": true - } + "lodash": "^4.17.21" } }, "resolve-from": { @@ -1705,9 +1764,9 @@ "dev": true }, "semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "dev": true }, "serialize-javascript": { @@ -1799,9 +1858,9 @@ "dev": true }, "strip-json-comments": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", - "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=", + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", + "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==", "dev": true }, "supports-color": { @@ -1813,12 +1872,6 @@ "has-flag": "^4.0.0" } }, - "taffydb": { - "version": "2.6.2", - "resolved": "https://registry.npmjs.org/taffydb/-/taffydb-2.6.2.tgz", - "integrity": "sha1-fLy2S1oUG2ou/CxdLGe04VCyomg=", - "dev": true - }, "test-exclude": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-6.0.0.tgz", @@ -1841,6 +1894,15 @@ } } }, + "tiny-worker": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/tiny-worker/-/tiny-worker-2.3.0.tgz", + "integrity": "sha512-pJ70wq5EAqTAEl9IkGzA+fN0836rycEuz2Cn6yeZ6FRzlVS5IDOkFHpIoEsksPRQV34GDqXm65+OlnZqUSyK2g==", + "dev": true, + "requires": { + "esm": "^3.2.25" + } + }, "to-fast-properties": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz", @@ -1871,14 +1933,20 @@ "is-typedarray": "^1.0.0" } }, + "uc.micro": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz", + "integrity": "sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==", + "dev": true + }, "uglify-js": { "version": "3.2.2", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.2.2.tgz", "integrity": "sha512-++1NO/zZIEdWf6cDIGceSJQPX31SqIpbVAHwFG5+240MtZqPG/NIPoinj8zlXQtAfMBqEt1Jyv2FiLP3n9gVhQ==", "dev": true, "requires": { - "commander": "2.12.2", - "source-map": "0.6.1" + "commander": "~2.12.1", + "source-map": "~0.6.1" }, "dependencies": { "commander": { @@ -1890,28 +1958,11 @@ } }, "underscore": { - "version": "1.8.3", - "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", - "integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=", + "version": "1.13.6", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.6.tgz", + "integrity": "sha512-+A5Sja4HP1M08MaXya7p5LvjuM7K6q/2EaC0+iovj/wOcMsTzMvDFbasi/oSapiwOlt252IqsKqPjCl7huKS0A==", "dev": true }, - "underscore-contrib": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/underscore-contrib/-/underscore-contrib-0.3.0.tgz", - "integrity": "sha1-ZltmwkeD+PorGMn4y7Dix9SMJsc=", - "dev": true, - "requires": { - "underscore": "1.6.0" - }, - "dependencies": { - "underscore": { - "version": "1.6.0", - "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.6.0.tgz", - "integrity": "sha1-izixDKze9jM3uLJOT/htRa6lKag=", - "dev": true - } - } - }, "update-browserslist-db": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.10.tgz", @@ -1928,16 +1979,6 @@ "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", "dev": true }, - "webworker-threads": { - "version": "0.7.13", - "resolved": "https://registry.npmjs.org/webworker-threads/-/webworker-threads-0.7.13.tgz", - "integrity": "sha1-yEsYtrokElu503NC5E3rgVFi+4M=", - "dev": true, - "requires": { - "bindings": "1.3.0", - "nan": "2.7.0" - } - }, "which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", @@ -1989,9 +2030,9 @@ } }, "xmlcreate": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/xmlcreate/-/xmlcreate-1.0.2.tgz", - "integrity": "sha1-+mv3YqYKQT+z3Y9LA8WyaSONMI8=", + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/xmlcreate/-/xmlcreate-2.0.4.tgz", + "integrity": "sha512-nquOebG4sngPmGPICTS5EnxqhKbCmz5Ox5hsszI2T6U5qdrJizBc+0ilYSEjTSzU0yZcmvppztXe/5Al5fUwdg==", "dev": true }, "y18n": { diff --git a/package.json b/package.json index cc037d1..9438771 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,16 @@ { "name": "js-md5", - "version": "0.7.3", + "version": "0.8.0", "description": "A simple MD5 hash function for JavaScript supports UTF-8 encoding.", "main": "src/md5.js", "devDependencies": { "expect.js": "~0.3.1", - "jsdoc": "^3.4.0", + "jsdoc": "~4.0.2", "mocha": "~10.2.0", "nyc": "^15.1.0", "requirejs": "^2.1.22", - "uglify-js": "^3.1.9", - "webworker-threads": "^0.7.11" + "tiny-worker": "^2.3.0", + "uglify-js": "^3.1.9" }, "scripts": { "test": "nyc mocha tests/node-test.js", diff --git a/src/md5.js b/src/md5.js index ba6867d..ae9000c 100644 --- a/src/md5.js +++ b/src/md5.js @@ -2,15 +2,16 @@ * [js-md5]{@link https://github.com/emn178/js-md5} * * @namespace md5 - * @version 0.7.3 + * @version 0.8.0 * @author Chen, Yi-Cyuan [emn178@gmail.com] - * @copyright Chen, Yi-Cyuan 2014-2017 + * @copyright Chen, Yi-Cyuan 2014-2023 * @license MIT */ (function () { 'use strict'; - var ERROR = 'input is invalid type'; + 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_MD5_NO_WINDOW) { @@ -39,18 +40,38 @@ blocks = new Uint32Array(buffer); } - if (root.JS_MD5_NO_NODE_JS || !Array.isArray) { - Array.isArray = function (obj) { + var isArray = Array.isArray; + if (root.JS_MD5_NO_NODE_JS || !isArray) { + isArray = function (obj) { return Object.prototype.toString.call(obj) === '[object Array]'; }; } - if (ARRAY_BUFFER && (root.JS_MD5_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) { - ArrayBuffer.isView = function (obj) { + var isView = ArrayBuffer.isView; + if (ARRAY_BUFFER && (root.JS_MD5_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]; + } + /** * @method hex * @memberof md5 @@ -153,21 +174,29 @@ }; 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_MD5_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('md5').update(message, 'utf8').digest('hex'); } else { if (message === null || message === undefined) { - throw ERROR; + throw new Error(INPUT_ERROR); } else if (message.constructor === ArrayBuffer) { message = new Uint8Array(message); } } - if (Array.isArray(message) || ArrayBuffer.isView(message) || + if (isArray(message) || isView(message) || message.constructor === Buffer) { - return crypto.createHash('md5').update(new Buffer(message)).digest('hex'); + return crypto.createHash('md5').update(bufferFrom(message)).digest('hex'); } else { return method(message); } @@ -175,6 +204,116 @@ return nodeMethod; }; + /** + * @namespace md5.hmac + */ + /** + * @method hex + * @memberof md5.hmac + * @description Output hash as hex string + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {String} Hex string + * @example + * md5.hmac.hex('key', 'The quick brown fox jumps over the lazy dog'); + * // equal to + * md5.hmac('key', 'The quick brown fox jumps over the lazy dog'); + */ + + /** + * @method digest + * @memberof md5.hmac + * @description Output hash as bytes array + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {Array} Bytes array + * @example + * md5.hmac.digest('key', 'The quick brown fox jumps over the lazy dog'); + */ + /** + * @method array + * @memberof md5.hmac + * @description Output hash as bytes array + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {Array} Bytes array + * @example + * md5.hmac.array('key', 'The quick brown fox jumps over the lazy dog'); + */ + /** + * @method arrayBuffer + * @memberof md5.hmac + * @description Output hash as ArrayBuffer + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {ArrayBuffer} ArrayBuffer + * @example + * md5.hmac.arrayBuffer('key', 'The quick brown fox jumps over the lazy dog'); + */ + /** + * @method buffer + * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead. + * @memberof md5.hmac + * @description Output hash as ArrayBuffer + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {ArrayBuffer} ArrayBuffer + * @example + * md5.hmac.buffer('key', 'The quick brown fox jumps over the lazy dog'); + */ + /** + * @method base64 + * @memberof md5.hmac + * @description Output hash as base64 string + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {String} base64 string + * @example + * md5.hmac.base64('key', 'The quick brown fox jumps over the lazy dog'); + */ + var createHmacOutputMethod = function (outputType) { + return function (key, message) { + return new HmacMd5(key, true).update(message)[outputType](); + }; + }; + + /** + * @method create + * @memberof md5.hmac + * @description Create HmacMd5 object + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @returns {HmacMd5} HmacMd5 object. + * @example + * var hash = md5.hmac.create('key'); + */ + /** + * @method update + * @memberof md5.hmac + * @description Create and update HmacMd5 object + * @param {String|Array|Uint8Array|ArrayBuffer} key key + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {HmacMd5} HmacMd5 object. + * @example + * var hash = md5.hmac.update('key', 'The quick brown fox jumps over the lazy dog'); + * // equal to + * var hash = md5.hmac.create('key'); + * hash.update('The quick brown fox jumps over the lazy dog'); + */ + var createHmacMethod = function () { + var method = createHmacOutputMethod('hex'); + method.create = function (key) { + return new HmacMd5(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; + }; + /** * Md5 class * @class Md5 @@ -214,26 +353,12 @@ */ Md5.prototype.update = function (message) { if (this.finalized) { - return; + throw new Error(FINALIZE_ERROR); } - var notString, type = typeof message; - if (type !== 'string') { - if (type === 'object') { - if (message === null) { - throw ERROR; - } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { - message = new Uint8Array(message); - } else if (!Array.isArray(message)) { - if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) { - throw ERROR; - } - } - } else { - throw ERROR; - } - notString = true; - } + var result = formatMessage(message); + message = result[0]; + var isString = result[1]; var code, index = 0, i, length = message.length, blocks = this.blocks; var buffer8 = this.buffer8; @@ -247,34 +372,24 @@ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; } - if (notString) { - if (ARRAY_BUFFER) { - for (i = this.start; index < length && i < 64; ++index) { - buffer8[i++] = message[index]; - } - } else { - for (i = this.start; index < length && i < 64; ++index) { - blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; - } - } - } else { + if (isString) { if (ARRAY_BUFFER) { for (i = this.start; index < length && i < 64; ++index) { code = message.charCodeAt(index); if (code < 0x80) { buffer8[i++] = code; } else if (code < 0x800) { - buffer8[i++] = 0xc0 | (code >> 6); + buffer8[i++] = 0xc0 | (code >>> 6); buffer8[i++] = 0x80 | (code & 0x3f); } else if (code < 0xd800 || code >= 0xe000) { - buffer8[i++] = 0xe0 | (code >> 12); - buffer8[i++] = 0x80 | ((code >> 6) & 0x3f); + buffer8[i++] = 0xe0 | (code >>> 12); + buffer8[i++] = 0x80 | ((code >>> 6) & 0x3f); buffer8[i++] = 0x80 | (code & 0x3f); } else { code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); - buffer8[i++] = 0xf0 | (code >> 18); - buffer8[i++] = 0x80 | ((code >> 12) & 0x3f); - buffer8[i++] = 0x80 | ((code >> 6) & 0x3f); + buffer8[i++] = 0xf0 | (code >>> 18); + buffer8[i++] = 0x80 | ((code >>> 12) & 0x3f); + buffer8[i++] = 0x80 | ((code >>> 6) & 0x3f); buffer8[i++] = 0x80 | (code & 0x3f); } } @@ -282,23 +397,33 @@ 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 { + if (ARRAY_BUFFER) { + for (i = this.start; index < length && i < 64; ++index) { + buffer8[i++] = message[index]; + } + } else { + for (i = this.start; index < length && i < 64; ++index) { + blocks[i >>> 2] |= message[index] << SHIFT[i++ & 3]; + } + } } this.lastByteIndex = i; this.bytes += i - this.start; @@ -323,7 +448,7 @@ } this.finalized = true; var blocks = this.blocks, i = this.lastByteIndex; - blocks[i >> 2] |= EXTRA[i & 3]; + blocks[i >>> 2] |= EXTRA[i & 3]; if (i >= 56) { if (!this.hashed) { this.hash(); @@ -524,22 +649,22 @@ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3; - return HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] + - HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] + - HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] + - HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] + - HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] + - HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] + - HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] + - HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] + - HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] + - HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] + - HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] + - HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] + - HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] + - HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] + - HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] + - HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F]; + return HEX_CHARS[(h0 >>> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] + + HEX_CHARS[(h0 >>> 12) & 0x0F] + HEX_CHARS[(h0 >>> 8) & 0x0F] + + HEX_CHARS[(h0 >>> 20) & 0x0F] + HEX_CHARS[(h0 >>> 16) & 0x0F] + + HEX_CHARS[(h0 >>> 28) & 0x0F] + HEX_CHARS[(h0 >>> 24) & 0x0F] + + HEX_CHARS[(h1 >>> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] + + HEX_CHARS[(h1 >>> 12) & 0x0F] + HEX_CHARS[(h1 >>> 8) & 0x0F] + + HEX_CHARS[(h1 >>> 20) & 0x0F] + HEX_CHARS[(h1 >>> 16) & 0x0F] + + HEX_CHARS[(h1 >>> 28) & 0x0F] + HEX_CHARS[(h1 >>> 24) & 0x0F] + + HEX_CHARS[(h2 >>> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] + + HEX_CHARS[(h2 >>> 12) & 0x0F] + HEX_CHARS[(h2 >>> 8) & 0x0F] + + HEX_CHARS[(h2 >>> 20) & 0x0F] + HEX_CHARS[(h2 >>> 16) & 0x0F] + + HEX_CHARS[(h2 >>> 28) & 0x0F] + HEX_CHARS[(h2 >>> 24) & 0x0F] + + HEX_CHARS[(h3 >>> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] + + HEX_CHARS[(h3 >>> 12) & 0x0F] + HEX_CHARS[(h3 >>> 8) & 0x0F] + + HEX_CHARS[(h3 >>> 20) & 0x0F] + HEX_CHARS[(h3 >>> 16) & 0x0F] + + HEX_CHARS[(h3 >>> 28) & 0x0F] + HEX_CHARS[(h3 >>> 24) & 0x0F]; }; /** @@ -569,10 +694,10 @@ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3; return [ - h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 24) & 0xFF, - h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 24) & 0xFF, - h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 24) & 0xFF, - h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 24) & 0xFF + h0 & 0xFF, (h0 >>> 8) & 0xFF, (h0 >>> 16) & 0xFF, (h0 >>> 24) & 0xFF, + h1 & 0xFF, (h1 >>> 8) & 0xFF, (h1 >>> 16) & 0xFF, (h1 >>> 24) & 0xFF, + h2 & 0xFF, (h2 >>> 8) & 0xFF, (h2 >>> 16) & 0xFF, (h2 >>> 24) & 0xFF, + h3 & 0xFF, (h3 >>> 8) & 0xFF, (h3 >>> 16) & 0xFF, (h3 >>> 24) & 0xFF ]; }; @@ -651,7 +776,75 @@ return base64Str; }; + /** + * HmacMd5 class + * @class HmacMd5 + * @extends Md5 + * @description This is internal class. + * @see {@link md5.hmac.create} + */ + function HmacMd5(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 Md5(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; + } + + Md5.call(this, sharedMemory); + + this.update(iKeyPad); + this.oKeyPad = oKeyPad; + this.inner = true; + this.sharedMemory = sharedMemory; + } + HmacMd5.prototype = new Md5(); + + HmacMd5.prototype.finalize = function () { + Md5.prototype.finalize.call(this); + if (this.inner) { + this.inner = false; + var innerHash = this.array(); + Md5.call(this, this.sharedMemory); + this.update(this.oKeyPad); + this.update(innerHash); + Md5.prototype.finalize.call(this); + } + }; + var exports = createMethod(); + exports.md5 = exports; + exports.md5.hmac = createHmacMethod(); if (COMMON_JS) { module.exports = exports; diff --git a/tests/hmac-test.js b/tests/hmac-test.js new file mode 100644 index 0000000..1c07d39 --- /dev/null +++ b/tests/hmac-test.js @@ -0,0 +1,202 @@ +(function (md5) { + Array.prototype.toHexString = ArrayBuffer.prototype.toHexString = function () { + var array = new Uint8Array(this); + var hex = ''; + for (var i = 0; i < array.length; ++i) { + var c = array[i].toString('16'); + hex += c.length === 1 ? '0' + c : c; + } + return hex; + }; + + var testCases = { + md5_hmac: { + 'Test Vectors': { + '9294727a3638bb1c13f48ef8158bfc9d': [ + [0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b], + 'Hi There' + ], + '750c783e6ab0b503eaa86e310a5db738': [ + 'Jefe', + 'what do ya want for nothing?' + ], + '56be34521d144c88dbb8c733f0e8b3f6': [ + [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], + [0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd] + ], + '697eaf0aca3a3aea3a75164746ffaa79': [ + [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19], + [0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd] + ], + '56461ef2342edc00f9bab995690efd4c': [ + [0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c], + 'Test With Truncation' + ], + '6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd': [ + [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], + 'Test Using Larger Than Block-Size Key - Hash Key First' + ], + '6f630fad67cda0ee1fb1f562db3aa53e': [ + [0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa], + 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data' + ] + }, + 'UTF8': { + '05fe3b294344f4e93c811e10f9825a38': ['中文', '中文'], + 'c1bc6df91f38e6332b8562324005103d': ['aécio', 'aécio'], + '14eef78533c35078177d11fb4593a19e': ['𠜎', '𠜎'] + } + } + }; + + if (!(typeof JS_MD5_NO_ARRAY_BUFFER === 'boolean' && JS_MD5_NO_ARRAY_BUFFER)) { + testCases.md5_hmac.Uint8Array = { + '72c33c78cac0b7a581ac263a344ed01d': [ + new Uint8Array(0), + 'Hi There' + ] + }; + testCases.md5_hmac.ArrayBuffer = { + '72c33c78cac0b7a581ac263a344ed01d': [ + new ArrayBuffer(0), + 'Hi There' + ] + }; + } + + var errorTestCases = [null, undefined, { length: 0 }, 0, 1, false, true, NaN, Infinity, function () {}]; + + function runTestCases(name, algorithm) { + var methods = [ + { + name: name, + call: algorithm, + }, + { + name: name + '.hex', + call: algorithm.hex + }, + { + name: name + '.array', + call: function (key, message) { + return algorithm.array(key, message).toHexString(); + } + }, + { + name: name + '.digest', + call: function (key, message) { + return algorithm.digest(key, message).toHexString(); + } + }, + { + name: name + '.arrayBuffer', + call: function (key, message) { + return algorithm.arrayBuffer(key, message).toHexString(); + } + } + ]; + + var classMethods = [ + { + name: 'create', + call: function (key, message) { + return algorithm.create(key).update(message).toString(); + } + }, + { + name: 'update', + call: function (key, message) { + return algorithm.update(key, message).toString(); + } + }, + { + name: 'hex', + call: function (key, message) { + return algorithm.update(key, message).hex(); + } + }, + { + name: 'array', + call: function (key, message) { + return algorithm.update(key, message).array().toHexString(); + } + }, + { + name: 'digest', + call: function (key, message) { + return algorithm.update(key, message).digest().toHexString(); + } + }, + { + name: 'arrayBuffer', + call: function (key, message) { + return algorithm.update(key, message).arrayBuffer().toHexString(); + } + }, + { + name: 'finalize', + call: function (key, message) { + var hash = algorithm.update(key, message); + hash.hex(); + return hash.hex(); + } + } + ]; + + var subTestCases = testCases[name]; + + describe(name, function () { + methods.forEach(function (method) { + describe('#' + method.name, function () { + for (var testCaseName in subTestCases) { + (function (testCaseName) { + var testCase = subTestCases[testCaseName]; + context('when ' + testCaseName, function () { + for (var hash in testCase) { + (function (message, hash) { + it('should be equal', function () { + expect(method.call(message[0], message[1])).to.be(hash); + }); + })(testCase[hash], hash); + } + }); + })(testCaseName); + } + }); + }); + + classMethods.forEach(function (method) { + describe('#' + method.name, function () { + for (var testCaseName in subTestCases) { + (function (testCaseName) { + var testCase = subTestCases[testCaseName]; + context('when ' + testCaseName, function () { + for (var hash in testCase) { + (function (message, hash) { + it('should be equal', function () { + expect(method.call(message[0], message[1])).to.be(hash); + }); + })(testCase[hash], hash); + } + }); + })(testCaseName); + } + }); + }); + + describe('#' + name, function () { + errorTestCases.forEach(function (testCase) { + context('when ' + testCase, function () { + it('should throw error', function () { + expect(function () { + algorithm(testCase, ''); + }).to.throwError(/input is invalid type/); + }); + }); + }); + }); + }); + } + + runTestCases('md5_hmac', md5.hmac); +})(md5); diff --git a/tests/index.html b/tests/index.html index b3f74df..53d9fc0 100644 --- a/tests/index.html +++ b/tests/index.html @@ -14,6 +14,7 @@ mocha.setup('bdd'); +