From 309beb285b8e41b158b39c6a645191193ebfb3f2 Mon Sep 17 00:00:00 2001 From: "Chen, Yi-Cyuan" Date: Tue, 24 Jan 2017 15:15:13 +0800 Subject: [PATCH] Added missing arrayBuffer method. --- CHANGELOG.md | 5 +++++ README.md | 5 +++++ bower.json | 2 +- build/md4.min.js | 4 ++-- doc/Md4_.html | 18 +++++++++--------- doc/global.html | 4 ++-- doc/index.html | 9 +++++++-- doc/md4.html | 4 ++-- doc/md4.js.html | 26 ++++++++++++++++++++++---- package.json | 2 +- src/md4.js | 24 +++++++++++++++++++++--- tests/test.js | 38 +++++++++++++++++++------------------- 12 files changed, 96 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 631dab1..52da89c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,13 @@ # Change Log +## v0.3.2 / 2017-01-24 +### Added +- missing arrayBuffer method. + ## v0.3.1 / 2017-01-23 ### Fixed - ArrayBuffer dosen't work in Webpack. + ### Deprecated - `buffer` maybe confuse with Buffer in node.js. Please use `arrayBuffer` instead. diff --git a/README.md b/README.md index a75426e..09d9b4b 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,14 @@ For node.js, you can use this command to install: You could use like this: ```JavaScript md4('Message to hash'); + var hash = md4.create(); hash.update('Message to hash'); hash.hex(); + +var hash2 = md4.update('Message to hash'); +hash2.update('Message2 to hash'); +hash2.array(); ``` If you use node.js, you should require the module first: ```JavaScript diff --git a/bower.json b/bower.json index 5929af4..39d88ef 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "js-md4", - "version": "0.3.1", + "version": "0.3.2", "main": ["src/md4.js"], "ignore": [ "samples", diff --git a/build/md4.min.js b/build/md4.min.js index 41401d3..f2cd2db 100644 --- a/build/md4.min.js +++ b/build/md4.min.js @@ -2,9 +2,9 @@ * [js-md4]{@link https://github.com/emn178/js-md4} * * @namespace md4 - * @version 0.3.1 + * @version 0.3.2 * @author Yi-Cyuan Chen [emn178@gmail.com] * @copyright Yi-Cyuan Chen 2015-2027 * @license MIT */ -!function(){"use strict";function t(t){if(t)p[0]=p[16]=p[1]=p[2]=p[3]=p[4]=p[5]=p[6]=p[7]=p[8]=p[9]=p[10]=p[11]=p[12]=p[13]=p[14]=p[15]=0,this.blocks=p,this.buffer8=s;else if(n){var i=new ArrayBuffer(68);this.buffer8=new Uint8Array(i),this.blocks=new Uint32Array(i)}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=0,this.finalized=this.hashed=!1,this.first=!0}var i="object"==typeof window?window:{},h=!i.JS_MD4_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node;h&&(i=global);var s,e=!i.JS_MD4_NO_COMMON_JS&&"object"==typeof module&&module.exports,r="function"==typeof define&&define.amd,n=!i.JS_MD4_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,o="0123456789abcdef".split(""),f=[128,32768,8388608,-2147483648],a=[0,8,16,24],u=["hex","array","digest","buffer"],p=[];if(n){var y=new ArrayBuffer(68);s=new Uint8Array(y),p=new Uint32Array(y)}var c=function(i){return function(h){return new t(!0).update(h)[i]()}},d=function(){var i=c("hex");i.create=function(){return new t},i.update=function(t){return i.create().update(t)};for(var h=0;he;){if(this.hashed&&(this.hashed=!1,o[0]=o[16],o[16]=o[1]=o[2]=o[3]=o[4]=o[5]=o[6]=o[7]=o[8]=o[9]=o[10]=o[11]=o[12]=o[13]=o[14]=o[15]=0),i)if(n)for(s=this.start;r>e&&64>s;++e)f[s++]=t[e];else for(s=this.start;r>e&&64>s;++e)o[s>>2]|=t[e]<e&&64>s;++e)h=t.charCodeAt(e),128>h?f[s++]=h:2048>h?(f[s++]=192|h>>6,f[s++]=128|63&h):55296>h||h>=57344?(f[s++]=224|h>>12,f[s++]=128|h>>6&63,f[s++]=128|63&h):(h=65536+((1023&h)<<10|1023&t.charCodeAt(++e)),f[s++]=240|h>>18,f[s++]=128|h>>12&63,f[s++]=128|h>>6&63,f[s++]=128|63&h);else for(s=this.start;r>e&&64>s;++e)h=t.charCodeAt(e),128>h?o[s>>2]|=h<h?(o[s>>2]|=(192|h>>6)<>2]|=(128|63&h)<h||h>=57344?(o[s>>2]|=(224|h>>12)<>2]|=(128|h>>6&63)<>2]|=(128|63&h)<>2]|=(240|h>>18)<>2]|=(128|h>>12&63)<>2]|=(128|h>>6&63)<>2]|=(128|63&h)<=64?(this.start=s-64,this.hash(),this.hashed=!0):this.start=s}return this}},t.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,i=this.lastByteIndex;t[i>>2]|=f[3&i],i>=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,this.hash()}},t.prototype.hash=function(){var t,i,h,s,e,r,n,o,f=this.blocks;this.first?(t=f[0]-1,t=t<<3|t>>>29,s=(4023233417&t|2562383102&~t)+f[1]+271733878,s=s<<7|s>>>25,h=(s&t|4023233417&~s)+f[2]-1732584194,h=h<<11|h>>>21,i=(h&s|~h&t)+f[3]-271733879,i=i<<19|i>>>13):(t=this.h0,i=this.h1,h=this.h2,s=this.h3,t+=(i&h|~i&s)+f[0],t=t<<3|t>>>29,s+=(t&i|~t&h)+f[1],s=s<<7|s>>>25,h+=(s&t|~s&i)+f[2],h=h<<11|h>>>21,i+=(h&s|~h&t)+f[3],i=i<<19|i>>>13),t+=(i&h|~i&s)+f[4],t=t<<3|t>>>29,s+=(t&i|~t&h)+f[5],s=s<<7|s>>>25,h+=(s&t|~s&i)+f[6],h=h<<11|h>>>21,i+=(h&s|~h&t)+f[7],i=i<<19|i>>>13,t+=(i&h|~i&s)+f[8],t=t<<3|t>>>29,s+=(t&i|~t&h)+f[9],s=s<<7|s>>>25,h+=(s&t|~s&i)+f[10],h=h<<11|h>>>21,i+=(h&s|~h&t)+f[11],i=i<<19|i>>>13,t+=(i&h|~i&s)+f[12],t=t<<3|t>>>29,s+=(t&i|~t&h)+f[13],s=s<<7|s>>>25,h+=(s&t|~s&i)+f[14],h=h<<11|h>>>21,i+=(h&s|~h&t)+f[15],i=i<<19|i>>>13,r=i&h,t+=(r|i&s|h&s)+f[0]+1518500249,t=t<<3|t>>>29,e=t&i,s+=(e|t&h|r)+f[4]+1518500249,s=s<<5|s>>>27,o=s&t,h+=(o|s&i|e)+f[8]+1518500249,h=h<<9|h>>>23,n=h&s,i+=(n|h&t|o)+f[12]+1518500249,i=i<<13|i>>>19,r=i&h,t+=(r|i&s|n)+f[1]+1518500249,t=t<<3|t>>>29,e=t&i,s+=(e|t&h|r)+f[5]+1518500249,s=s<<5|s>>>27,o=s&t,h+=(o|s&i|e)+f[9]+1518500249,h=h<<9|h>>>23,n=h&s,i+=(n|h&t|o)+f[13]+1518500249,i=i<<13|i>>>19,r=i&h,t+=(r|i&s|n)+f[2]+1518500249,t=t<<3|t>>>29,e=t&i,s+=(e|t&h|r)+f[6]+1518500249,s=s<<5|s>>>27,o=s&t,h+=(o|s&i|e)+f[10]+1518500249,h=h<<9|h>>>23,n=h&s,i+=(n|h&t|o)+f[14]+1518500249,i=i<<13|i>>>19,r=i&h,t+=(r|i&s|n)+f[3]+1518500249,t=t<<3|t>>>29,e=t&i,s+=(e|t&h|r)+f[7]+1518500249,s=s<<5|s>>>27,o=s&t,h+=(o|s&i|e)+f[11]+1518500249,h=h<<9|h>>>23,i+=(h&s|h&t|o)+f[15]+1518500249,i=i<<13|i>>>19,r=i^h,t+=(r^s)+f[0]+1859775393,t=t<<3|t>>>29,s+=(r^t)+f[8]+1859775393,s=s<<9|s>>>23,o=s^t,h+=(o^i)+f[4]+1859775393,h=h<<11|h>>>21,i+=(o^h)+f[12]+1859775393,i=i<<15|i>>>17,r=i^h,t+=(r^s)+f[2]+1859775393,t=t<<3|t>>>29,s+=(r^t)+f[10]+1859775393,s=s<<9|s>>>23,o=s^t,h+=(o^i)+f[6]+1859775393,h=h<<11|h>>>21,i+=(o^h)+f[14]+1859775393,i=i<<15|i>>>17,r=i^h,t+=(r^s)+f[1]+1859775393,t=t<<3|t>>>29,s+=(r^t)+f[9]+1859775393,s=s<<9|s>>>23,o=s^t,h+=(o^i)+f[5]+1859775393,h=h<<11|h>>>21,i+=(o^h)+f[13]+1859775393,i=i<<15|i>>>17,r=i^h,t+=(r^s)+f[3]+1859775393,t=t<<3|t>>>29,s+=(r^t)+f[11]+1859775393,s=s<<9|s>>>23,o=s^t,h+=(o^i)+f[7]+1859775393,h=h<<11|h>>>21,i+=(o^h)+f[15]+1859775393,i=i<<15|i>>>17,this.first?(this.h0=t+1732584193<<0,this.h1=i-271733879<<0,this.h2=h-1732584194<<0,this.h3=s+271733878<<0,this.first=!1):(this.h0=this.h0+t<<0,this.h1=this.h1+i<<0,this.h2=this.h2+h<<0,this.h3=this.h3+s<<0)},t.prototype.hex=function(){this.finalize();var t=this.h0,i=this.h1,h=this.h2,s=this.h3;return o[t>>4&15]+o[15&t]+o[t>>12&15]+o[t>>8&15]+o[t>>20&15]+o[t>>16&15]+o[t>>28&15]+o[t>>24&15]+o[i>>4&15]+o[15&i]+o[i>>12&15]+o[i>>8&15]+o[i>>20&15]+o[i>>16&15]+o[i>>28&15]+o[i>>24&15]+o[h>>4&15]+o[15&h]+o[h>>12&15]+o[h>>8&15]+o[h>>20&15]+o[h>>16&15]+o[h>>28&15]+o[h>>24&15]+o[s>>4&15]+o[15&s]+o[s>>12&15]+o[s>>8&15]+o[s>>20&15]+o[s>>16&15]+o[s>>28&15]+o[s>>24&15]},t.prototype.toString=t.prototype.hex,t.prototype.digest=function(){this.finalize();var t=this.h0,i=this.h1,h=this.h2,s=this.h3;return[255&t,t>>8&255,t>>16&255,t>>24&255,255&i,i>>8&255,i>>16&255,i>>24&255,255&h,h>>8&255,h>>16&255,h>>24&255,255&s,s>>8&255,s>>16&255,s>>24&255]},t.prototype.array=t.prototype.digest,t.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(16),i=new Uint32Array(t);return i[0]=this.h0,i[1]=this.h1,i[2]=this.h2,i[3]=this.h3,t},t.prototype.buffer=t.prototype.arrayBuffer;var l=d();e?module.exports=l:(i.md4=l,r&&define(function(){return l}))}(); \ No newline at end of file +!function(){"use strict";function t(t){if(t)p[0]=p[16]=p[1]=p[2]=p[3]=p[4]=p[5]=p[6]=p[7]=p[8]=p[9]=p[10]=p[11]=p[12]=p[13]=p[14]=p[15]=0,this.blocks=p,this.buffer8=r;else if(n){var e=new ArrayBuffer(68);this.buffer8=new Uint8Array(e),this.blocks=new Uint32Array(e)}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=0,this.finalized=this.hashed=!1,this.first=!0}var e="object"==typeof window?window:{},i=!e.JS_MD4_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node;i&&(e=global);var r,h=!e.JS_MD4_NO_COMMON_JS&&"object"==typeof module&&module.exports,s="function"==typeof define&&define.amd,n=!e.JS_MD4_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,f="0123456789abcdef".split(""),a=[128,32768,8388608,-2147483648],o=[0,8,16,24],u=["hex","array","digest","buffer","arrayBuffer"],p=[];if(n){var d=new ArrayBuffer(68);r=new Uint8Array(d),p=new Uint32Array(d)}var y=function(e){return function(i){return new t(!0).update(i)[e]()}},c=function(){var e=y("hex");i&&(e=l(e)),e.create=function(){return new t},e.update=function(t){return e.create().update(t)};for(var r=0;rh;){if(this.hashed&&(this.hashed=!1,f[0]=f[16],f[16]=f[1]=f[2]=f[3]=f[4]=f[5]=f[6]=f[7]=f[8]=f[9]=f[10]=f[11]=f[12]=f[13]=f[14]=f[15]=0),e)if(n)for(r=this.start;s>h&&64>r;++h)a[r++]=t[h];else for(r=this.start;s>h&&64>r;++h)f[r>>2]|=t[h]<h&&64>r;++h)i=t.charCodeAt(h),128>i?a[r++]=i:2048>i?(a[r++]=192|i>>6,a[r++]=128|63&i):55296>i||i>=57344?(a[r++]=224|i>>12,a[r++]=128|i>>6&63,a[r++]=128|63&i):(i=65536+((1023&i)<<10|1023&t.charCodeAt(++h)),a[r++]=240|i>>18,a[r++]=128|i>>12&63,a[r++]=128|i>>6&63,a[r++]=128|63&i);else for(r=this.start;s>h&&64>r;++h)i=t.charCodeAt(h),128>i?f[r>>2]|=i<i?(f[r>>2]|=(192|i>>6)<>2]|=(128|63&i)<i||i>=57344?(f[r>>2]|=(224|i>>12)<>2]|=(128|i>>6&63)<>2]|=(128|63&i)<>2]|=(240|i>>18)<>2]|=(128|i>>12&63)<>2]|=(128|i>>6&63)<>2]|=(128|63&i)<=64?(this.start=r-64,this.hash(),this.hashed=!0):this.start=r}return this}},t.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,e=this.lastByteIndex;t[e>>2]|=a[3&e],e>=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,this.hash()}},t.prototype.hash=function(){var t,e,i,r,h,s,n,f,a=this.blocks;this.first?(t=a[0]-1,t=t<<3|t>>>29,r=(4023233417&t|2562383102&~t)+a[1]+271733878,r=r<<7|r>>>25,i=(r&t|4023233417&~r)+a[2]-1732584194,i=i<<11|i>>>21,e=(i&r|~i&t)+a[3]-271733879,e=e<<19|e>>>13):(t=this.h0,e=this.h1,i=this.h2,r=this.h3,t+=(e&i|~e&r)+a[0],t=t<<3|t>>>29,r+=(t&e|~t&i)+a[1],r=r<<7|r>>>25,i+=(r&t|~r&e)+a[2],i=i<<11|i>>>21,e+=(i&r|~i&t)+a[3],e=e<<19|e>>>13),t+=(e&i|~e&r)+a[4],t=t<<3|t>>>29,r+=(t&e|~t&i)+a[5],r=r<<7|r>>>25,i+=(r&t|~r&e)+a[6],i=i<<11|i>>>21,e+=(i&r|~i&t)+a[7],e=e<<19|e>>>13,t+=(e&i|~e&r)+a[8],t=t<<3|t>>>29,r+=(t&e|~t&i)+a[9],r=r<<7|r>>>25,i+=(r&t|~r&e)+a[10],i=i<<11|i>>>21,e+=(i&r|~i&t)+a[11],e=e<<19|e>>>13,t+=(e&i|~e&r)+a[12],t=t<<3|t>>>29,r+=(t&e|~t&i)+a[13],r=r<<7|r>>>25,i+=(r&t|~r&e)+a[14],i=i<<11|i>>>21,e+=(i&r|~i&t)+a[15],e=e<<19|e>>>13,s=e&i,t+=(s|e&r|i&r)+a[0]+1518500249,t=t<<3|t>>>29,h=t&e,r+=(h|t&i|s)+a[4]+1518500249,r=r<<5|r>>>27,f=r&t,i+=(f|r&e|h)+a[8]+1518500249,i=i<<9|i>>>23,n=i&r,e+=(n|i&t|f)+a[12]+1518500249,e=e<<13|e>>>19,s=e&i,t+=(s|e&r|n)+a[1]+1518500249,t=t<<3|t>>>29,h=t&e,r+=(h|t&i|s)+a[5]+1518500249,r=r<<5|r>>>27,f=r&t,i+=(f|r&e|h)+a[9]+1518500249,i=i<<9|i>>>23,n=i&r,e+=(n|i&t|f)+a[13]+1518500249,e=e<<13|e>>>19,s=e&i,t+=(s|e&r|n)+a[2]+1518500249,t=t<<3|t>>>29,h=t&e,r+=(h|t&i|s)+a[6]+1518500249,r=r<<5|r>>>27,f=r&t,i+=(f|r&e|h)+a[10]+1518500249,i=i<<9|i>>>23,n=i&r,e+=(n|i&t|f)+a[14]+1518500249,e=e<<13|e>>>19,s=e&i,t+=(s|e&r|n)+a[3]+1518500249,t=t<<3|t>>>29,h=t&e,r+=(h|t&i|s)+a[7]+1518500249,r=r<<5|r>>>27,f=r&t,i+=(f|r&e|h)+a[11]+1518500249,i=i<<9|i>>>23,e+=(i&r|i&t|f)+a[15]+1518500249,e=e<<13|e>>>19,s=e^i,t+=(s^r)+a[0]+1859775393,t=t<<3|t>>>29,r+=(s^t)+a[8]+1859775393,r=r<<9|r>>>23,f=r^t,i+=(f^e)+a[4]+1859775393,i=i<<11|i>>>21,e+=(f^i)+a[12]+1859775393,e=e<<15|e>>>17,s=e^i,t+=(s^r)+a[2]+1859775393,t=t<<3|t>>>29,r+=(s^t)+a[10]+1859775393,r=r<<9|r>>>23,f=r^t,i+=(f^e)+a[6]+1859775393,i=i<<11|i>>>21,e+=(f^i)+a[14]+1859775393,e=e<<15|e>>>17,s=e^i,t+=(s^r)+a[1]+1859775393,t=t<<3|t>>>29,r+=(s^t)+a[9]+1859775393,r=r<<9|r>>>23,f=r^t,i+=(f^e)+a[5]+1859775393,i=i<<11|i>>>21,e+=(f^i)+a[13]+1859775393,e=e<<15|e>>>17,s=e^i,t+=(s^r)+a[3]+1859775393,t=t<<3|t>>>29,r+=(s^t)+a[11]+1859775393,r=r<<9|r>>>23,f=r^t,i+=(f^e)+a[7]+1859775393,i=i<<11|i>>>21,e+=(f^i)+a[15]+1859775393,e=e<<15|e>>>17,this.first?(this.h0=t+1732584193<<0,this.h1=e-271733879<<0,this.h2=i-1732584194<<0,this.h3=r+271733878<<0,this.first=!1):(this.h0=this.h0+t<<0,this.h1=this.h1+e<<0,this.h2=this.h2+i<<0,this.h3=this.h3+r<<0)},t.prototype.hex=function(){this.finalize();var t=this.h0,e=this.h1,i=this.h2,r=this.h3;return f[t>>4&15]+f[15&t]+f[t>>12&15]+f[t>>8&15]+f[t>>20&15]+f[t>>16&15]+f[t>>28&15]+f[t>>24&15]+f[e>>4&15]+f[15&e]+f[e>>12&15]+f[e>>8&15]+f[e>>20&15]+f[e>>16&15]+f[e>>28&15]+f[e>>24&15]+f[i>>4&15]+f[15&i]+f[i>>12&15]+f[i>>8&15]+f[i>>20&15]+f[i>>16&15]+f[i>>28&15]+f[i>>24&15]+f[r>>4&15]+f[15&r]+f[r>>12&15]+f[r>>8&15]+f[r>>20&15]+f[r>>16&15]+f[r>>28&15]+f[r>>24&15]},t.prototype.toString=t.prototype.hex,t.prototype.digest=function(){this.finalize();var t=this.h0,e=this.h1,i=this.h2,r=this.h3;return[255&t,t>>8&255,t>>16&255,t>>24&255,255&e,e>>8&255,e>>16&255,e>>24&255,255&i,i>>8&255,i>>16&255,i>>24&255,255&r,r>>8&255,r>>16&255,r>>24&255]},t.prototype.array=t.prototype.digest,t.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(16),e=new Uint32Array(t);return e[0]=this.h0,e[1]=this.h1,e[2]=this.h2,e[3]=this.h3,t},t.prototype.buffer=t.prototype.arrayBuffer;var b=c();h?module.exports=b:(e.md4=b,s&&define(function(){return b}))}(); \ No newline at end of file diff --git a/doc/Md4_.html b/doc/Md4_.html index 7ff27c0..e0f85f1 100644 --- a/doc/Md4_.html +++ b/doc/Md4_.html @@ -91,7 +91,7 @@
Source:
@@ -198,7 +198,7 @@
Source:
@@ -314,7 +314,7 @@
Source:
@@ -432,7 +432,7 @@
Source:
@@ -548,7 +548,7 @@
Source:
@@ -664,7 +664,7 @@
Source:
@@ -780,7 +780,7 @@
Source:
@@ -954,7 +954,7 @@
Source:
@@ -1031,7 +1031,7 @@
- Documentation generated by JSDoc 3.4.3 on Mon Jan 23 2017 15:12:38 GMT+0800 (CST) + Documentation generated by JSDoc 3.4.3 on Tue Jan 24 2017 15:15:12 GMT+0800 (CST)
diff --git a/doc/global.html b/doc/global.html index 322777b..4be8353 100644 --- a/doc/global.html +++ b/doc/global.html @@ -210,7 +210,7 @@
Source:
@@ -285,7 +285,7 @@
- Documentation generated by JSDoc 3.4.3 on Mon Jan 23 2017 15:12:38 GMT+0800 (CST) + Documentation generated by JSDoc 3.4.3 on Tue Jan 24 2017 15:15:12 GMT+0800 (CST)
diff --git a/doc/index.html b/doc/index.html index bb4b1cd..1962a48 100644 --- a/doc/index.html +++ b/doc/index.html @@ -52,9 +52,14 @@
npm install js-md4

Notice

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

Usage

You could use like this:

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

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

+hash.hex(); + +var hash2 = md4.update('Message to hash'); +hash2.update('Message2 to hash'); +hash2.array();

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

var md4 = require('js-md4');

It supports AMD:

require(['your/path/md4.js'], function (md4) {
 // ...
@@ -93,7 +98,7 @@ md4.arrayBuffer(''); // ArrayBuffer

License

The project i

- Documentation generated by JSDoc 3.4.3 on Mon Jan 23 2017 15:12:38 GMT+0800 (CST) + Documentation generated by JSDoc 3.4.3 on Tue Jan 24 2017 15:15:12 GMT+0800 (CST)
diff --git a/doc/md4.html b/doc/md4.html index 5f08b66..31341b0 100644 --- a/doc/md4.html +++ b/doc/md4.html @@ -47,7 +47,7 @@
Version:
-
  • 0.3.1
+
  • 0.3.2
@@ -1085,7 +1085,7 @@
- Documentation generated by JSDoc 3.4.3 on Mon Jan 23 2017 15:12:38 GMT+0800 (CST) + Documentation generated by JSDoc 3.4.3 on Tue Jan 24 2017 15:15:12 GMT+0800 (CST)
diff --git a/doc/md4.js.html b/doc/md4.js.html index f0a43ff..fb3d916 100644 --- a/doc/md4.js.html +++ b/doc/md4.js.html @@ -30,7 +30,7 @@ * [js-md4]{@link https://github.com/emn178/js-md4} * * @namespace md4 - * @version 0.3.1 + * @version 0.3.2 * @author Yi-Cyuan Chen [emn178@gmail.com] * @copyright Yi-Cyuan Chen 2015-2027 * @license MIT @@ -50,7 +50,7 @@ var HEX_CHARS = '0123456789abcdef'.split(''); var EXTRA = [128, 32768, 8388608, -2147483648]; var SHIFT = [0, 8, 16, 24]; - var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer']; + var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer', 'arrayBuffer']; var blocks = [], buffer8; if (ARRAY_BUFFER) { @@ -125,6 +125,9 @@ */ var createMethod = function () { var method = createOutputMethod('hex'); + if (NODE_JS) { + method = nodeWrap(method); + } method.create = function () { return new Md4(); }; @@ -138,6 +141,21 @@ return method; }; + var nodeWrap = function (method) { + var crypto = require('crypto'); + var Buffer = require('buffer').Buffer; + var nodeMethod = function (message) { + if (typeof message === 'string') { + return crypto.createHash('md4').update(message, 'utf8').digest('hex'); + } else if (ARRAY_BUFFER && message instanceof ArrayBuffer) { + message = new Uint8Array(message); + } else if (message.length === undefined) { + return method(message); + } + return crypto.createHash('md4').update(new Buffer(message)).digest('hex'); + }; + return nodeMethod; + }; /** * Md4 class @@ -181,7 +199,7 @@ return; } var notString = typeof message !== 'string'; - if(notString && ARRAY_BUFFER && message instanceof ArrayBuffer) { + if (notString && ARRAY_BUFFER && message instanceof ArrayBuffer) { message = new Uint8Array(message); } var code, index = 0, i, length = message.length || 0, blocks = this.blocks; @@ -598,7 +616,7 @@
- Documentation generated by JSDoc 3.4.3 on Mon Jan 23 2017 15:12:38 GMT+0800 (CST) + Documentation generated by JSDoc 3.4.3 on Tue Jan 24 2017 15:15:12 GMT+0800 (CST)
diff --git a/package.json b/package.json index f966b3d..3b3502f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-md4", - "version": "0.3.1", + "version": "0.3.2", "description": "A simple MD4 hash function for JavaScript supports UTF-8 encoding.", "main": "src/md4.js", "devDependencies": { diff --git a/src/md4.js b/src/md4.js index 7329c96..1a79427 100644 --- a/src/md4.js +++ b/src/md4.js @@ -2,7 +2,7 @@ * [js-md4]{@link https://github.com/emn178/js-md4} * * @namespace md4 - * @version 0.3.1 + * @version 0.3.2 * @author Yi-Cyuan Chen [emn178@gmail.com] * @copyright Yi-Cyuan Chen 2015-2027 * @license MIT @@ -22,7 +22,7 @@ var HEX_CHARS = '0123456789abcdef'.split(''); var EXTRA = [128, 32768, 8388608, -2147483648]; var SHIFT = [0, 8, 16, 24]; - var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer']; + var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer', 'arrayBuffer']; var blocks = [], buffer8; if (ARRAY_BUFFER) { @@ -97,6 +97,9 @@ */ var createMethod = function () { var method = createOutputMethod('hex'); + if (NODE_JS) { + method = nodeWrap(method); + } method.create = function () { return new Md4(); }; @@ -110,6 +113,21 @@ return method; }; + var nodeWrap = function (method) { + var crypto = require('crypto'); + var Buffer = require('buffer').Buffer; + var nodeMethod = function (message) { + if (typeof message === 'string') { + return crypto.createHash('md4').update(message, 'utf8').digest('hex'); + } else if (ARRAY_BUFFER && message instanceof ArrayBuffer) { + message = new Uint8Array(message); + } else if (message.length === undefined) { + return method(message); + } + return crypto.createHash('md4').update(new Buffer(message)).digest('hex'); + }; + return nodeMethod; + }; /** * Md4 class @@ -153,7 +171,7 @@ return; } var notString = typeof message !== 'string'; - if(notString && ARRAY_BUFFER && message instanceof ArrayBuffer) { + if (notString && ARRAY_BUFFER && message instanceof ArrayBuffer) { message = new Uint8Array(message); } var code, index = 0, i, length = message.length || 0, blocks = this.blocks; diff --git a/tests/test.js b/tests/test.js index 376519d..1270f8d 100644 --- a/tests/test.js +++ b/tests/test.js @@ -4,7 +4,7 @@ var hex = ''; for (var i = 0; i < array.length; ++i) { var c = array[i].toString('16'); - hex += c.length == 1 ? '0' + c : c; + hex += c.length === 1 ? '0' + c : c; } return hex; }; @@ -141,26 +141,26 @@ delete testCases['ArrayBuffer']; } - methods.forEach(function (method) { - describe('#' + method.name, function() { - for (var testCaseName in testCases) { - (function (testCaseName) { - var testCase = testCases[testCaseName]; - context('when ' + testCaseName, function() { - for(var hash in testCase) { - (function (message, hash) { - it('should be equal', function() { - expect(method.call(message)).to.be(hash); - }); - })(testCase[hash], hash); - } - }); - })(testCaseName); - } + describe('Md4', function () { + methods.forEach(function (method) { + describe('#' + method.name, function() { + for (var testCaseName in testCases) { + (function (testCaseName) { + var testCase = testCases[testCaseName]; + context('when ' + testCaseName, function() { + for(var hash in testCase) { + (function (message, hash) { + it('should be equal', function() { + expect(method.call(message)).to.be(hash); + }); + })(testCase[hash], hash); + } + }); + })(testCaseName); + } + }); }); - }); - describe('Md4', function () { classMethods.forEach(function (method) { describe('#' + method.name, function() { for (var testCaseName in testCases) {