diff --git a/CHANGELOG.md b/CHANGELOG.md index 52e26e4..f8f78ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# v0.4.1 / 2016-03-31 + +* Removed length detection in node.js. +* Deprecated `buffer` and replace by `arrayBuffer`. + # v0.4.0 / 2015-12-28 * Added support for update hash. diff --git a/LICENSE.txt b/LICENSE.txt index aff3297..1598c37 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright 2014-2015 Yi-Cyuan Chen +Copyright 2014-2016 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 705b6af..2b4c023 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ # js-md5 [![Build Status](https://travis-ci.org/emn178/js-md5.svg?branch=master)](https://travis-ci.org/emn178/js-md5) [![Coverage Status](https://coveralls.io/repos/emn178/js-md5/badge.svg?branch=master)](https://coveralls.io/r/emn178/js-md5?branch=master) -[![NPM](https://nodei.co/npm/js-md5.png?stars&downloads)](https://nodei.co/npm/js-md5/) +[![NPM](https://nodei.co/npm/js-md5.png?stars&downloads)](https://nodei.co/npm/js-md5/) + A simple MD5 hash function for JavaScript supports UTF-8 encoding. ## Demo [MD5 Online](http://emn178.github.io/online-tools/md5.html) -[MD5 File Checksum Online](http://emn178.github.io/online-tools/md5_checksum.html) +[MD5 File Checksum Online](http://emn178.github.io/online-tools/md5_checksum.html) ## Download [Compress](https://raw.github.com/emn178/js-md5/master/build/md5.min.js) @@ -21,6 +22,9 @@ 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: ```JavaScript @@ -59,7 +63,8 @@ md5(''); // d41d8cd98f00b204e9800998ecf8427e md5.hex(''); // d41d8cd98f00b204e9800998ecf8427e md5.array(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126] md5.digest(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126] -md5.buffer(''); // ArrayBuffer +md5.arrayBuffer(''); // ArrayBuffer +md5.buffer(''); // ArrayBuffer, deprecated, This maybe confuse with Buffer in node.js. Please use arrayBuffer instead. ``` ## Benchmark @@ -71,4 +76,4 @@ The project is released under the [MIT license](http://www.opensource.org/licens ## Contact The project's website is located at https://github.com/emn178/js-md5 -Author: Yi-Cyuan Chen (emn178@gmail.com) +Author: Chen, Yi-Cyuan (emn178@gmail.com) diff --git a/bower.json b/bower.json index 48b8331..98a50c4 100644 --- a/bower.json +++ b/bower.json @@ -1,9 +1,8 @@ { "name": "js-md5", - "version": "0.4.0", + "version": "0.4.1", "main": ["src/md5.js"], "ignore": [ - "samples", "tests" ] } diff --git a/build/md5.min.js b/build/md5.min.js index 5f7af4c..384e1a6 100644 --- a/build/md5.min.js +++ b/build/md5.min.js @@ -3,8 +3,8 @@ * * @namespace md5 * @version 0.4.0 - * @author Yi-Cyuan Chen [emn178@gmail.com] - * @copyright Yi-Cyuan Chen 2014-2015 + * @author Chen, Yi-Cyuan [emn178@gmail.com] + * @copyright Chen, Yi-Cyuan 2014-2016 * @license MIT */ -!function(t){"use strict";function e(t){if(t)c[0]=c[16]=c[1]=c[2]=c[3]=c[4]=c[5]=c[6]=c[7]=c[8]=c[9]=c[10]=c[11]=c[12]=c[13]=c[14]=c[15]=0,this.blocks=c,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 h="object"==typeof process&&process.versions&&process.versions.node;h&&(t=global);var r,i=!t.JS_MD5_TEST&&"object"==typeof module&&module.exports,s="function"==typeof define&&define.amd,n=!t.JS_MD5_TEST&&"undefined"!=typeof ArrayBuffer,o="0123456789abcdef".split(""),f=[128,32768,8388608,-2147483648],a=[0,8,16,24],u=["hex","array","digest","buffer"],c=[];if(n){var d=new ArrayBuffer(68);r=new Uint8Array(d),c=new Uint32Array(d)}var l=function(t){return function(h){return new e(!0).update(h)[t]()}},y=function(){var t=l("hex");h&&(t=p(t)),t.create=function(){return new e},t.update=function(e){return t.create().update(e)};for(var r=0;rs;){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),h)if(n)for(i=this.start;o>s&&64>i;++s)u[i++]=e[s];else for(i=this.start;o>s&&64>i;++s)f[i>>2]|=e[s]<s&&64>i;++s)r=e.charCodeAt(s),128>r?u[i++]=r:2048>r?(u[i++]=192|r>>6,u[i++]=128|63&r):55296>r||r>=57344?(u[i++]=224|r>>12,u[i++]=128|r>>6&63,u[i++]=128|63&r):(r=65536+((1023&r)<<10|1023&e.charCodeAt(++s)),u[i++]=240|r>>18,u[i++]=128|r>>12&63,u[i++]=128|r>>6&63,u[i++]=128|63&r);else for(i=this.start;o>s&&64>i;++s)r=e.charCodeAt(s),128>r?f[i>>2]|=r<r?(f[i>>2]|=(192|r>>6)<>2]|=(128|63&r)<r||r>=57344?(f[i>>2]|=(224|r>>12)<>2]|=(128|r>>6&63)<>2]|=(128|63&r)<>2]|=(240|r>>18)<>2]|=(128|r>>12&63)<>2]|=(128|r>>6&63)<>2]|=(128|63&r)<=64?(this.start=i-64,this.hash(),this.hashed=!0):this.start=i}return this}},e.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,e=this.lastByteIndex;t[e>>2]|=f[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()}},e.prototype.hash=function(){var t,e,h,r,i,s,n=this.blocks;this.first?(t=n[0]-680876937,t=(t<<7|t>>>25)-271733879<<0,r=(-1732584194^2004318071&t)+n[1]-117830708,r=(r<<12|r>>>20)+t<<0,h=(-271733879^r&(-271733879^t))+n[2]-1126478375,h=(h<<17|h>>>15)+r<<0,e=(t^h&(r^t))+n[3]-1316259209,e=(e<<22|e>>>10)+h<<0):(t=this.h0,e=this.h1,h=this.h2,r=this.h3,t+=(r^e&(h^r))+n[0]-680876936,t=(t<<7|t>>>25)+e<<0,r+=(h^t&(e^h))+n[1]-389564586,r=(r<<12|r>>>20)+t<<0,h+=(e^r&(t^e))+n[2]+606105819,h=(h<<17|h>>>15)+r<<0,e+=(t^h&(r^t))+n[3]-1044525330,e=(e<<22|e>>>10)+h<<0),t+=(r^e&(h^r))+n[4]-176418897,t=(t<<7|t>>>25)+e<<0,r+=(h^t&(e^h))+n[5]+1200080426,r=(r<<12|r>>>20)+t<<0,h+=(e^r&(t^e))+n[6]-1473231341,h=(h<<17|h>>>15)+r<<0,e+=(t^h&(r^t))+n[7]-45705983,e=(e<<22|e>>>10)+h<<0,t+=(r^e&(h^r))+n[8]+1770035416,t=(t<<7|t>>>25)+e<<0,r+=(h^t&(e^h))+n[9]-1958414417,r=(r<<12|r>>>20)+t<<0,h+=(e^r&(t^e))+n[10]-42063,h=(h<<17|h>>>15)+r<<0,e+=(t^h&(r^t))+n[11]-1990404162,e=(e<<22|e>>>10)+h<<0,t+=(r^e&(h^r))+n[12]+1804603682,t=(t<<7|t>>>25)+e<<0,r+=(h^t&(e^h))+n[13]-40341101,r=(r<<12|r>>>20)+t<<0,h+=(e^r&(t^e))+n[14]-1502002290,h=(h<<17|h>>>15)+r<<0,e+=(t^h&(r^t))+n[15]+1236535329,e=(e<<22|e>>>10)+h<<0,t+=(h^r&(e^h))+n[1]-165796510,t=(t<<5|t>>>27)+e<<0,r+=(e^h&(t^e))+n[6]-1069501632,r=(r<<9|r>>>23)+t<<0,h+=(t^e&(r^t))+n[11]+643717713,h=(h<<14|h>>>18)+r<<0,e+=(r^t&(h^r))+n[0]-373897302,e=(e<<20|e>>>12)+h<<0,t+=(h^r&(e^h))+n[5]-701558691,t=(t<<5|t>>>27)+e<<0,r+=(e^h&(t^e))+n[10]+38016083,r=(r<<9|r>>>23)+t<<0,h+=(t^e&(r^t))+n[15]-660478335,h=(h<<14|h>>>18)+r<<0,e+=(r^t&(h^r))+n[4]-405537848,e=(e<<20|e>>>12)+h<<0,t+=(h^r&(e^h))+n[9]+568446438,t=(t<<5|t>>>27)+e<<0,r+=(e^h&(t^e))+n[14]-1019803690,r=(r<<9|r>>>23)+t<<0,h+=(t^e&(r^t))+n[3]-187363961,h=(h<<14|h>>>18)+r<<0,e+=(r^t&(h^r))+n[8]+1163531501,e=(e<<20|e>>>12)+h<<0,t+=(h^r&(e^h))+n[13]-1444681467,t=(t<<5|t>>>27)+e<<0,r+=(e^h&(t^e))+n[2]-51403784,r=(r<<9|r>>>23)+t<<0,h+=(t^e&(r^t))+n[7]+1735328473,h=(h<<14|h>>>18)+r<<0,e+=(r^t&(h^r))+n[12]-1926607734,e=(e<<20|e>>>12)+h<<0,i=e^h,t+=(i^r)+n[5]-378558,t=(t<<4|t>>>28)+e<<0,r+=(i^t)+n[8]-2022574463,r=(r<<11|r>>>21)+t<<0,s=r^t,h+=(s^e)+n[11]+1839030562,h=(h<<16|h>>>16)+r<<0,e+=(s^h)+n[14]-35309556,e=(e<<23|e>>>9)+h<<0,i=e^h,t+=(i^r)+n[1]-1530992060,t=(t<<4|t>>>28)+e<<0,r+=(i^t)+n[4]+1272893353,r=(r<<11|r>>>21)+t<<0,s=r^t,h+=(s^e)+n[7]-155497632,h=(h<<16|h>>>16)+r<<0,e+=(s^h)+n[10]-1094730640,e=(e<<23|e>>>9)+h<<0,i=e^h,t+=(i^r)+n[13]+681279174,t=(t<<4|t>>>28)+e<<0,r+=(i^t)+n[0]-358537222,r=(r<<11|r>>>21)+t<<0,s=r^t,h+=(s^e)+n[3]-722521979,h=(h<<16|h>>>16)+r<<0,e+=(s^h)+n[6]+76029189,e=(e<<23|e>>>9)+h<<0,i=e^h,t+=(i^r)+n[9]-640364487,t=(t<<4|t>>>28)+e<<0,r+=(i^t)+n[12]-421815835,r=(r<<11|r>>>21)+t<<0,s=r^t,h+=(s^e)+n[15]+530742520,h=(h<<16|h>>>16)+r<<0,e+=(s^h)+n[2]-995338651,e=(e<<23|e>>>9)+h<<0,t+=(h^(e|~r))+n[0]-198630844,t=(t<<6|t>>>26)+e<<0,r+=(e^(t|~h))+n[7]+1126891415,r=(r<<10|r>>>22)+t<<0,h+=(t^(r|~e))+n[14]-1416354905,h=(h<<15|h>>>17)+r<<0,e+=(r^(h|~t))+n[5]-57434055,e=(e<<21|e>>>11)+h<<0,t+=(h^(e|~r))+n[12]+1700485571,t=(t<<6|t>>>26)+e<<0,r+=(e^(t|~h))+n[3]-1894986606,r=(r<<10|r>>>22)+t<<0,h+=(t^(r|~e))+n[10]-1051523,h=(h<<15|h>>>17)+r<<0,e+=(r^(h|~t))+n[1]-2054922799,e=(e<<21|e>>>11)+h<<0,t+=(h^(e|~r))+n[8]+1873313359,t=(t<<6|t>>>26)+e<<0,r+=(e^(t|~h))+n[15]-30611744,r=(r<<10|r>>>22)+t<<0,h+=(t^(r|~e))+n[6]-1560198380,h=(h<<15|h>>>17)+r<<0,e+=(r^(h|~t))+n[13]+1309151649,e=(e<<21|e>>>11)+h<<0,t+=(h^(e|~r))+n[4]-145523070,t=(t<<6|t>>>26)+e<<0,r+=(e^(t|~h))+n[11]-1120210379,r=(r<<10|r>>>22)+t<<0,h+=(t^(r|~e))+n[2]+718787259,h=(h<<15|h>>>17)+r<<0,e+=(r^(h|~t))+n[9]-343485551,e=(e<<21|e>>>11)+h<<0,this.first?(this.h0=t+1732584193<<0,this.h1=e-271733879<<0,this.h2=h-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+h<<0,this.h3=this.h3+r<<0)},e.prototype.hex=function(){this.finalize();var t=this.h0,e=this.h1,h=this.h2,r=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[e>>4&15]+o[15&e]+o[e>>12&15]+o[e>>8&15]+o[e>>20&15]+o[e>>16&15]+o[e>>28&15]+o[e>>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[r>>4&15]+o[15&r]+o[r>>12&15]+o[r>>8&15]+o[r>>20&15]+o[r>>16&15]+o[r>>28&15]+o[r>>24&15]},e.prototype.toString=e.prototype.hex,e.prototype.digest=function(){this.finalize();var t=this.h0,e=this.h1,h=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&h,h>>8&255,h>>16&255,h>>24&255,255&r,r>>8&255,r>>16&255,r>>24&255]},e.prototype.array=e.prototype.digest,e.prototype.buffer=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};var b=y();i?module.exports=b:(t.md5=b,s&&define(function(){return b}))}(this); \ No newline at end of file +!function(t){"use strict";function r(t){if(t)c[0]=c[16]=c[1]=c[2]=c[3]=c[4]=c[5]=c[6]=c[7]=c[8]=c[9]=c[10]=c[11]=c[12]=c[13]=c[14]=c[15]=0,this.blocks=c,this.buffer8=i;else if(n){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=0,this.finalized=this.hashed=!1,this.first=!0}var e="object"==typeof process&&process.versions&&process.versions.node;e&&(t=global);var i,h=!t.JS_MD5_TEST&&"object"==typeof module&&module.exports,s="function"==typeof define&&define.amd,n=!t.JS_MD5_TEST&&"undefined"!=typeof ArrayBuffer,f="0123456789abcdef".split(""),a=[128,32768,8388608,-2147483648],o=[0,8,16,24],u=["hex","array","digest","buffer","arrayBuffer"],c=[];if(n){var p=new ArrayBuffer(68);i=new Uint8Array(p),c=new Uint32Array(p)}var y=function(t){return function(e){return new r(!0).update(e)[t]()}},d=function(){var t=y("hex");e&&(t=l(t)),t.create=function(){return new r},t.update=function(r){return t.create().update(r)};for(var i=0;is;){if(this.hashed&&(this.hashed=!1,a[0]=a[16],a[16]=a[1]=a[2]=a[3]=a[4]=a[5]=a[6]=a[7]=a[8]=a[9]=a[10]=a[11]=a[12]=a[13]=a[14]=a[15]=0),e)if(n)for(h=this.start;f>s&&64>h;++s)u[h++]=r[s];else for(h=this.start;f>s&&64>h;++s)a[h>>2]|=r[s]<s&&64>h;++s)i=r.charCodeAt(s),128>i?u[h++]=i:2048>i?(u[h++]=192|i>>6,u[h++]=128|63&i):55296>i||i>=57344?(u[h++]=224|i>>12,u[h++]=128|i>>6&63,u[h++]=128|63&i):(i=65536+((1023&i)<<10|1023&r.charCodeAt(++s)),u[h++]=240|i>>18,u[h++]=128|i>>12&63,u[h++]=128|i>>6&63,u[h++]=128|63&i);else for(h=this.start;f>s&&64>h;++s)i=r.charCodeAt(s),128>i?a[h>>2]|=i<i?(a[h>>2]|=(192|i>>6)<>2]|=(128|63&i)<i||i>=57344?(a[h>>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=h-64,this.hash(),this.hashed=!0):this.start=h}return this}},r.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,r=this.lastByteIndex;t[r>>2]|=a[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,this.hash()}},r.prototype.hash=function(){var t,r,e,i,h,s,n=this.blocks;this.first?(t=n[0]-680876937,t=(t<<7|t>>>25)-271733879<<0,i=(-1732584194^2004318071&t)+n[1]-117830708,i=(i<<12|i>>>20)+t<<0,e=(-271733879^i&(-271733879^t))+n[2]-1126478375,e=(e<<17|e>>>15)+i<<0,r=(t^e&(i^t))+n[3]-1316259209,r=(r<<22|r>>>10)+e<<0):(t=this.h0,r=this.h1,e=this.h2,i=this.h3,t+=(i^r&(e^i))+n[0]-680876936,t=(t<<7|t>>>25)+r<<0,i+=(e^t&(r^e))+n[1]-389564586,i=(i<<12|i>>>20)+t<<0,e+=(r^i&(t^r))+n[2]+606105819,e=(e<<17|e>>>15)+i<<0,r+=(t^e&(i^t))+n[3]-1044525330,r=(r<<22|r>>>10)+e<<0),t+=(i^r&(e^i))+n[4]-176418897,t=(t<<7|t>>>25)+r<<0,i+=(e^t&(r^e))+n[5]+1200080426,i=(i<<12|i>>>20)+t<<0,e+=(r^i&(t^r))+n[6]-1473231341,e=(e<<17|e>>>15)+i<<0,r+=(t^e&(i^t))+n[7]-45705983,r=(r<<22|r>>>10)+e<<0,t+=(i^r&(e^i))+n[8]+1770035416,t=(t<<7|t>>>25)+r<<0,i+=(e^t&(r^e))+n[9]-1958414417,i=(i<<12|i>>>20)+t<<0,e+=(r^i&(t^r))+n[10]-42063,e=(e<<17|e>>>15)+i<<0,r+=(t^e&(i^t))+n[11]-1990404162,r=(r<<22|r>>>10)+e<<0,t+=(i^r&(e^i))+n[12]+1804603682,t=(t<<7|t>>>25)+r<<0,i+=(e^t&(r^e))+n[13]-40341101,i=(i<<12|i>>>20)+t<<0,e+=(r^i&(t^r))+n[14]-1502002290,e=(e<<17|e>>>15)+i<<0,r+=(t^e&(i^t))+n[15]+1236535329,r=(r<<22|r>>>10)+e<<0,t+=(e^i&(r^e))+n[1]-165796510,t=(t<<5|t>>>27)+r<<0,i+=(r^e&(t^r))+n[6]-1069501632,i=(i<<9|i>>>23)+t<<0,e+=(t^r&(i^t))+n[11]+643717713,e=(e<<14|e>>>18)+i<<0,r+=(i^t&(e^i))+n[0]-373897302,r=(r<<20|r>>>12)+e<<0,t+=(e^i&(r^e))+n[5]-701558691,t=(t<<5|t>>>27)+r<<0,i+=(r^e&(t^r))+n[10]+38016083,i=(i<<9|i>>>23)+t<<0,e+=(t^r&(i^t))+n[15]-660478335,e=(e<<14|e>>>18)+i<<0,r+=(i^t&(e^i))+n[4]-405537848,r=(r<<20|r>>>12)+e<<0,t+=(e^i&(r^e))+n[9]+568446438,t=(t<<5|t>>>27)+r<<0,i+=(r^e&(t^r))+n[14]-1019803690,i=(i<<9|i>>>23)+t<<0,e+=(t^r&(i^t))+n[3]-187363961,e=(e<<14|e>>>18)+i<<0,r+=(i^t&(e^i))+n[8]+1163531501,r=(r<<20|r>>>12)+e<<0,t+=(e^i&(r^e))+n[13]-1444681467,t=(t<<5|t>>>27)+r<<0,i+=(r^e&(t^r))+n[2]-51403784,i=(i<<9|i>>>23)+t<<0,e+=(t^r&(i^t))+n[7]+1735328473,e=(e<<14|e>>>18)+i<<0,r+=(i^t&(e^i))+n[12]-1926607734,r=(r<<20|r>>>12)+e<<0,h=r^e,t+=(h^i)+n[5]-378558,t=(t<<4|t>>>28)+r<<0,i+=(h^t)+n[8]-2022574463,i=(i<<11|i>>>21)+t<<0,s=i^t,e+=(s^r)+n[11]+1839030562,e=(e<<16|e>>>16)+i<<0,r+=(s^e)+n[14]-35309556,r=(r<<23|r>>>9)+e<<0,h=r^e,t+=(h^i)+n[1]-1530992060,t=(t<<4|t>>>28)+r<<0,i+=(h^t)+n[4]+1272893353,i=(i<<11|i>>>21)+t<<0,s=i^t,e+=(s^r)+n[7]-155497632,e=(e<<16|e>>>16)+i<<0,r+=(s^e)+n[10]-1094730640,r=(r<<23|r>>>9)+e<<0,h=r^e,t+=(h^i)+n[13]+681279174,t=(t<<4|t>>>28)+r<<0,i+=(h^t)+n[0]-358537222,i=(i<<11|i>>>21)+t<<0,s=i^t,e+=(s^r)+n[3]-722521979,e=(e<<16|e>>>16)+i<<0,r+=(s^e)+n[6]+76029189,r=(r<<23|r>>>9)+e<<0,h=r^e,t+=(h^i)+n[9]-640364487,t=(t<<4|t>>>28)+r<<0,i+=(h^t)+n[12]-421815835,i=(i<<11|i>>>21)+t<<0,s=i^t,e+=(s^r)+n[15]+530742520,e=(e<<16|e>>>16)+i<<0,r+=(s^e)+n[2]-995338651,r=(r<<23|r>>>9)+e<<0,t+=(e^(r|~i))+n[0]-198630844,t=(t<<6|t>>>26)+r<<0,i+=(r^(t|~e))+n[7]+1126891415,i=(i<<10|i>>>22)+t<<0,e+=(t^(i|~r))+n[14]-1416354905,e=(e<<15|e>>>17)+i<<0,r+=(i^(e|~t))+n[5]-57434055,r=(r<<21|r>>>11)+e<<0,t+=(e^(r|~i))+n[12]+1700485571,t=(t<<6|t>>>26)+r<<0,i+=(r^(t|~e))+n[3]-1894986606,i=(i<<10|i>>>22)+t<<0,e+=(t^(i|~r))+n[10]-1051523,e=(e<<15|e>>>17)+i<<0,r+=(i^(e|~t))+n[1]-2054922799,r=(r<<21|r>>>11)+e<<0,t+=(e^(r|~i))+n[8]+1873313359,t=(t<<6|t>>>26)+r<<0,i+=(r^(t|~e))+n[15]-30611744,i=(i<<10|i>>>22)+t<<0,e+=(t^(i|~r))+n[6]-1560198380,e=(e<<15|e>>>17)+i<<0,r+=(i^(e|~t))+n[13]+1309151649,r=(r<<21|r>>>11)+e<<0,t+=(e^(r|~i))+n[4]-145523070,t=(t<<6|t>>>26)+r<<0,i+=(r^(t|~e))+n[11]-1120210379,i=(i<<10|i>>>22)+t<<0,e+=(t^(i|~r))+n[2]+718787259,e=(e<<15|e>>>17)+i<<0,r+=(i^(e|~t))+n[9]-343485551,r=(r<<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)},r.prototype.hex=function(){this.finalize();var t=this.h0,r=this.h1,e=this.h2,i=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[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]+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]},r.prototype.toString=r.prototype.hex,r.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]},r.prototype.array=r.prototype.digest,r.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},r.prototype.buffer=r.prototype.arrayBuffer;var v=d();h?module.exports=v:(t.md5=v,s&&define(function(){return v}))}(this); \ No newline at end of file diff --git a/doc/Md5_.html b/doc/Md5_.html index 5d6223d..852f389 100644 --- a/doc/Md5_.html +++ b/doc/Md5_.html @@ -91,7 +91,7 @@
Source:
@@ -198,7 +198,7 @@
Source:
@@ -263,6 +263,122 @@ +

arrayBuffer() → {ArrayBuffer}

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

buffer() → {ArrayBuffer}

@@ -302,6 +418,8 @@ +
Deprecated:
  • This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
+ @@ -314,7 +432,7 @@
Source:
@@ -430,7 +548,7 @@
Source:
@@ -546,7 +664,7 @@
Source:
@@ -662,7 +780,7 @@
Source:
@@ -836,7 +954,7 @@
Source:
@@ -913,7 +1031,7 @@
- Documentation generated by JSDoc 3.4.0 on Mon Dec 28 2015 15:48:48 GMT+0800 (CST) + Documentation generated by JSDoc 3.4.0 on Thu Mar 31 2016 16:24:21 GMT+0800 (CST)
diff --git a/doc/global.html b/doc/global.html index c7ecd73..c4a5a52 100644 --- a/doc/global.html +++ b/doc/global.html @@ -210,7 +210,7 @@
Source:
@@ -259,16 +259,7 @@
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
-
-// It also supports UTF-8 encoding
-md5('中文'); // a7bac2239fcdcb3a067903d8077c4a07
-
-// It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
-md5([]); // d41d8cd98f00b204e9800998ecf8427e
-md5(new Uint8Array([])); // d41d8cd98f00b204e9800998ecf8427e
+
md5(''); // d41d8cd98f00b204e9800998ecf8427e
md5('The quick brown fox jumps over the lazy dog'); // 9e107d9d372bb6826bd81d3542a419d6
md5('The quick brown fox jumps over the lazy dog.'); // e4d909c290d0fb1ca068ffaddf22cbd0

// It also supports UTF-8 encoding
md5('中文'); // a7bac2239fcdcb3a067903d8077c4a07

// It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
md5([]); // d41d8cd98f00b204e9800998ecf8427e
md5(new Uint8Array([])); // d41d8cd98f00b204e9800998ecf8427e
@@ -294,7 +285,7 @@ md5(new Uint8Array([])); // d41d8cd98f00b204e9800998ecf8427e
- Documentation generated by JSDoc 3.4.0 on Mon Dec 28 2015 15:48:48 GMT+0800 (CST) + Documentation generated by JSDoc 3.4.0 on Thu Mar 31 2016 16:24:21 GMT+0800 (CST)
diff --git a/doc/index.html b/doc/index.html index 4a7e156..237a007 100644 --- a/doc/index.html +++ b/doc/index.html @@ -44,12 +44,14 @@

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

+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

Usage

You could use like this:

+
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');
@@ -74,9 +76,10 @@ md5(''); // d41d8cd98f00b204e9800998ecf8427e
 md5.hex(''); // d41d8cd98f00b204e9800998ecf8427e
 md5.array(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126]
 md5.digest(''); // [212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126]
-md5.buffer(''); // ArrayBuffer

Benchmark

UTF8
ASCII

+md5.arrayBuffer(''); // ArrayBuffer +md5.buffer(''); // ArrayBuffer, deprecated, This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.

Benchmark

UTF8
ASCII

License

The project is released under the MIT license.

-

Contact

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

+

Contact

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

@@ -93,7 +96,7 @@ md5.buffer(''); // ArrayBuffer

Benchmark

diff --git a/doc/md5.html b/doc/md5.html index c691300..58b92fa 100644 --- a/doc/md5.html +++ b/doc/md5.html @@ -68,14 +68,14 @@
Author:
    -
  • Yi-Cyuan Chen [emn178@gmail.com]
  • +
  • Chen, Yi-Cyuan [emn178@gmail.com]
- + @@ -115,107 +115,90 @@ -

Members

- - - -

(static) asciiThreshold :Number

- - - - -
- To use node.js md5 if ascii string length is greater than this value. -
- + +

Methods

-
Type:
-
    -
  • + -Number - - -
  • -
- - - - - -
- +

(static) array(message) → {Array}

- - - +
+ Output hash as bytes array +
- - - - - - -
Default Value:
-
    -
  • 60
  • -
- - -
Source:
-
- - +
Parameters:
- -
+ + + + + + + + + + + + + + - + -

(static) bytesThreshold :Number

+ + + + + -
Type:
-
    -
  • -Number +
+ + + + +
NameTypeDescription
message + + +String +| +Array +| +Uint8Array +| +ArrayBuffer -
- To use node.js md5 if bytes length is greater than this value. -
+ +
message to hash
- - @@ -245,17 +228,12 @@ - -
Default Value:
-
    -
  • 245
  • -
Source:
@@ -271,102 +249,56 @@ - - -

(static) utf8Threshold :Number

-
- To use node.js md5 if UTF-8 string length is greater than this value. -
-
Type:
-
    -
  • - -Number - - -
  • -
- - - - - -
- - - - - - - - - - - - - - +
Returns:
- + +
+ Bytes array +
- - - +
+
+ Type +
+
+ +Array - -
Default Value:
-
    -
  • 18
  • -
- - -
Source:
-
- + +
- +
Example
-
- - - - - +
md5.array('The quick brown fox jumps over the lazy dog');
- - - -

Methods

-

(static) array(message) → {Array}

+

(static) arrayBuffer(message) → {ArrayBuffer}

- Output hash as bytes array + Output hash as ArrayBuffer
@@ -468,7 +400,7 @@
Source:
@@ -495,7 +427,7 @@
- Bytes array + ArrayBuffer
@@ -506,7 +438,7 @@
-Array +ArrayBuffer
@@ -517,7 +449,7 @@
Example
-
md5.array('The quick brown fox jumps over the lazy dog');
+
md5.arrayBuffer('The quick brown fox jumps over the lazy dog');
@@ -623,6 +555,8 @@ +
Deprecated:
  • This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
+ @@ -635,7 +569,7 @@
Source:
@@ -744,7 +678,7 @@
Source:
@@ -1127,9 +1061,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');
+
md5.hex('The quick brown fox jumps over the lazy dog');
// equal to
md5('The quick brown fox jumps over the lazy dog');
@@ -1247,7 +1179,7 @@ md5('The quick brown fox jumps over the lazy dog');
Source:
@@ -1296,10 +1228,7 @@ 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');
+
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');
@@ -1325,7 +1254,7 @@ hash.update('The quick brown fox jumps over the lazy dog');
- Documentation generated by JSDoc 3.4.0 on Mon Dec 28 2015 15:48:48 GMT+0800 (CST) + Documentation generated by JSDoc 3.4.0 on Thu Mar 31 2016 16:24:21 GMT+0800 (CST)
diff --git a/doc/md5.js.html b/doc/md5.js.html index 3c1e34d..749ddd6 100644 --- a/doc/md5.js.html +++ b/doc/md5.js.html @@ -31,8 +31,8 @@ * * @namespace md5 * @version 0.4.0 - * @author Yi-Cyuan Chen [emn178@gmail.com] - * @copyright Yi-Cyuan Chen 2014-2015 + * @author Chen, Yi-Cyuan [emn178@gmail.com] + * @copyright Chen, Yi-Cyuan 2014-2016 * @license MIT */ (function (root) { @@ -48,7 +48,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) { @@ -86,8 +86,18 @@ * @example * md5.array('The quick brown fox jumps over the lazy dog'); */ + /** + * @method arrayBuffer + * @memberof md5 + * @description Output hash as ArrayBuffer + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {ArrayBuffer} ArrayBuffer + * @example + * md5.arrayBuffer('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 * @description Output hash as ArrayBuffer * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash @@ -96,9 +106,9 @@ * md5.buffer('The quick brown fox jumps over the lazy dog'); */ var createOutputMethod = function (outputType) { - return function(message) { + return function (message) { return new Md5(true).update(message)[outputType](); - } + }; }; /** @@ -153,45 +163,16 @@ } var nodeMethod = function (message) { if (typeof message == 'string') { - if (message.length <= nodeMethod.utf8Threshold) { - return method(message); - } else if (message.length <= nodeMethod.asciiThreshold && !/[^\x00-\x7F]/.test(message)) { - return method(message); - } return crypto.createHash('md5').update(message, 'utf8').digest('hex'); } else if (message.constructor == ArrayBuffer) { message = new Uint8Array(message); - } else if (message.length === undefined || message.length <= nodeMethod.bytesThreshold) { + } else if (message.length === undefined) { return method(message); } return crypto.createHash('md5').update(new Buffer(message)).digest('hex'); }; - - /** - * @member {Number} utf8Threshold - * @default 18 - * @description To use node.js md5 if UTF-8 string length is greater than this value. - * @memberof md5 - */ - nodeMethod.utf8Threshold = 18; - - /** - * @member {Number} asciiThreshold - * @default 60 - * @description To use node.js md5 if ascii string length is greater than this value. - * @memberof md5 - */ - nodeMethod.asciiThreshold = 60; - - /** - * @member {Number} bytesThreshold - * @default 245 - * @description To use node.js md5 if bytes length is greater than this value. - * @memberof md5 - */ - nodeMethod.bytesThreshold = 245; return nodeMethod; - } + }; /** * Md5 class @@ -235,7 +216,7 @@ return; } var notString = typeof(message) != 'string'; - if(notString && message.constructor == root.ArrayBuffer) { + if (notString && message.constructor == root.ArrayBuffer) { message = new Uint8Array(message); } var code, index = 0, i, length = message.length || 0, blocks = this.blocks; @@ -341,7 +322,7 @@ Md5.prototype.hash = function () { var a, b, c, d, bc, da, blocks = this.blocks; - if(this.first) { + if (this.first) { a = blocks[0] - 680876937; a = (a << 7 | a >>> 25) - 271733879 << 0; d = (-1732584194 ^ a & 2004318071) + blocks[1] - 117830708; @@ -494,7 +475,7 @@ b += (d ^ (c | ~a)) + blocks[9] - 343485551; b = (b << 21 | b >>> 11) + c << 0; - if(this.first) { + if (this.first) { this.h0 = a + 1732584193 << 0; this.h1 = b - 271733879 << 0; this.h2 = c - 1732584194 << 0; @@ -563,7 +544,7 @@ * @example * hash.digest(); */ - Md5.prototype.digest = function() { + Md5.prototype.digest = function () { this.finalize(); var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3; @@ -588,16 +569,16 @@ Md5.prototype.array = Md5.prototype.digest; /** - * @method buffer + * @method arrayBuffer * @memberof Md5 * @instance * @description Output hash as ArrayBuffer * @returns {ArrayBuffer} ArrayBuffer - * @see {@link md5.buffer} + * @see {@link md5.arrayBuffer} * @example - * hash.buffer(); + * hash.arrayBuffer(); */ - Md5.prototype.buffer = function() { + Md5.prototype.arrayBuffer = function () { this.finalize(); var buffer = new ArrayBuffer(16); @@ -609,6 +590,19 @@ return buffer; }; + /** + * @method buffer + * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead. + * @memberof Md5 + * @instance + * @description Output hash as ArrayBuffer + * @returns {ArrayBuffer} ArrayBuffer + * @see {@link md5.buffer} + * @example + * hash.buffer(); + */ + Md5.prototype.buffer = Md5.prototype.arrayBuffer; + var exports = createMethod(); if (COMMON_JS) { @@ -655,7 +649,7 @@
- Documentation generated by JSDoc 3.4.0 on Mon Dec 28 2015 15:48:47 GMT+0800 (CST) + Documentation generated by JSDoc 3.4.0 on Thu Mar 31 2016 16:24:20 GMT+0800 (CST)
diff --git a/package.json b/package.json index 90ed918..f626599 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-md5", - "version": "0.4.0", + "version": "0.4.1", "description": "A simple MD5 hash function for JavaScript supports UTF-8 encoding.", "main": "src/md5.js", "devDependencies": { @@ -31,7 +31,7 @@ "HMAC" ], "license": "MIT", - "author": "emn178 ", + "author": "Chen, Yi-Cyuan ", "homepage": "https://github.com/emn178/js-md5", "bugs": { "url": "https://github.com/emn178/js-md5/issues" diff --git a/src/md5.js b/src/md5.js index e9717e6..9433745 100644 --- a/src/md5.js +++ b/src/md5.js @@ -3,8 +3,8 @@ * * @namespace md5 * @version 0.4.0 - * @author Yi-Cyuan Chen [emn178@gmail.com] - * @copyright Yi-Cyuan Chen 2014-2015 + * @author Chen, Yi-Cyuan [emn178@gmail.com] + * @copyright Chen, Yi-Cyuan 2014-2016 * @license MIT */ (function (root) { @@ -20,7 +20,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) { @@ -58,8 +58,18 @@ * @example * md5.array('The quick brown fox jumps over the lazy dog'); */ + /** + * @method arrayBuffer + * @memberof md5 + * @description Output hash as ArrayBuffer + * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash + * @returns {ArrayBuffer} ArrayBuffer + * @example + * md5.arrayBuffer('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 * @description Output hash as ArrayBuffer * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash @@ -68,7 +78,7 @@ * md5.buffer('The quick brown fox jumps over the lazy dog'); */ var createOutputMethod = function (outputType) { - return function(message) { + return function (message) { return new Md5(true).update(message)[outputType](); }; }; @@ -125,43 +135,14 @@ } var nodeMethod = function (message) { if (typeof message == 'string') { - if (message.length <= nodeMethod.utf8Threshold) { - return method(message); - } else if (message.length <= nodeMethod.asciiThreshold && !/[^\x00-\x7F]/.test(message)) { - return method(message); - } return crypto.createHash('md5').update(message, 'utf8').digest('hex'); } else if (message.constructor == ArrayBuffer) { message = new Uint8Array(message); - } else if (message.length === undefined || message.length <= nodeMethod.bytesThreshold) { + } else if (message.length === undefined) { return method(message); } return crypto.createHash('md5').update(new Buffer(message)).digest('hex'); }; - - /** - * @member {Number} utf8Threshold - * @default 18 - * @description To use node.js md5 if UTF-8 string length is greater than this value. - * @memberof md5 - */ - nodeMethod.utf8Threshold = 18; - - /** - * @member {Number} asciiThreshold - * @default 60 - * @description To use node.js md5 if ascii string length is greater than this value. - * @memberof md5 - */ - nodeMethod.asciiThreshold = 60; - - /** - * @member {Number} bytesThreshold - * @default 245 - * @description To use node.js md5 if bytes length is greater than this value. - * @memberof md5 - */ - nodeMethod.bytesThreshold = 245; return nodeMethod; }; @@ -207,7 +188,7 @@ return; } var notString = typeof(message) != 'string'; - if(notString && message.constructor == root.ArrayBuffer) { + if (notString && message.constructor == root.ArrayBuffer) { message = new Uint8Array(message); } var code, index = 0, i, length = message.length || 0, blocks = this.blocks; @@ -313,7 +294,7 @@ Md5.prototype.hash = function () { var a, b, c, d, bc, da, blocks = this.blocks; - if(this.first) { + if (this.first) { a = blocks[0] - 680876937; a = (a << 7 | a >>> 25) - 271733879 << 0; d = (-1732584194 ^ a & 2004318071) + blocks[1] - 117830708; @@ -466,7 +447,7 @@ b += (d ^ (c | ~a)) + blocks[9] - 343485551; b = (b << 21 | b >>> 11) + c << 0; - if(this.first) { + if (this.first) { this.h0 = a + 1732584193 << 0; this.h1 = b - 271733879 << 0; this.h2 = c - 1732584194 << 0; @@ -535,7 +516,7 @@ * @example * hash.digest(); */ - Md5.prototype.digest = function() { + Md5.prototype.digest = function () { this.finalize(); var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3; @@ -560,16 +541,16 @@ Md5.prototype.array = Md5.prototype.digest; /** - * @method buffer + * @method arrayBuffer * @memberof Md5 * @instance * @description Output hash as ArrayBuffer * @returns {ArrayBuffer} ArrayBuffer - * @see {@link md5.buffer} + * @see {@link md5.arrayBuffer} * @example - * hash.buffer(); + * hash.arrayBuffer(); */ - Md5.prototype.buffer = function() { + Md5.prototype.arrayBuffer = function () { this.finalize(); var buffer = new ArrayBuffer(16); @@ -581,6 +562,19 @@ return buffer; }; + /** + * @method buffer + * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead. + * @memberof Md5 + * @instance + * @description Output hash as ArrayBuffer + * @returns {ArrayBuffer} ArrayBuffer + * @see {@link md5.buffer} + * @example + * hash.buffer(); + */ + Md5.prototype.buffer = Md5.prototype.arrayBuffer; + var exports = createMethod(); if (COMMON_JS) { diff --git a/tests/node-test.js b/tests/node-test.js index 8f53fff..fef2577 100644 --- a/tests/node-test.js +++ b/tests/node-test.js @@ -14,7 +14,7 @@ delete require.cache[require.resolve('../src/md5.js')]; delete require.cache[require.resolve('./test.js')]; md5 = null; -define = function(func) { +define = function (func) { md5 = func(); require('./test.js'); }; diff --git a/tests/requirejs.html b/tests/requirejs.html index 03a1faa..d0694ef 100644 --- a/tests/requirejs.html +++ b/tests/requirejs.html @@ -12,12 +12,12 @@
diff --git a/tests/test.js b/tests/test.js index 3d5b937..503aae8 100644 --- a/tests/test.js +++ b/tests/test.js @@ -58,6 +58,13 @@ } }; + if (typeof process == 'object') { + testCases['Buffer'] = { + 'd41d8cd98f00b204e9800998ecf8427e': new Buffer(0), + '9e107d9d372bb6826bd81d3542a419d6': new Buffer(new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])) + } + } + var methods = [ { name: 'md5', @@ -79,6 +86,12 @@ return md5.digest(message).toHexString(); } }, + { + name: 'md5.arrayBuffer', + call: function (message) { + return md5.arrayBuffer(message).toHexString(); + } + }, { name: 'md5.buffer', call: function (message) { @@ -118,6 +131,12 @@ return md5.update(message).digest().toHexString(); } }, + { + name: 'arrayBuffer', + call: function (message) { + return md5.update(message).arrayBuffer().toHexString(); + } + }, { name: 'buffer', call: function (message) { @@ -174,73 +193,4 @@ }); }); }); - // describe('md5', function() { - // describe('ascii', function() { - // describe('less than 64 bytes', function() { - // it('should be successful', function() { - // expect(md5('')).to.be('d41d8cd98f00b204e9800998ecf8427e'); - // expect(md5('The quick brown fox jumps over the lazy dog')).to.be('9e107d9d372bb6826bd81d3542a419d6'); - // expect(md5('The quick brown fox jumps over the lazy dog.')).to.be('e4d909c290d0fb1ca068ffaddf22cbd0'); - // }); - // }); - - // describe('more than 64 bytes', function() { - // it('should be successful', function() { - // expect(md5('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('f63872ef7bc97a8a8eadba6f0881de53'); - // }); - // }); - // }); - - // describe('UTF8', function() { - // describe('less than 64 bytes', function() { - // it('should be successful', function() { - // expect(md5('中文')).to.be('a7bac2239fcdcb3a067903d8077c4a07'); - // expect(md5('aécio')).to.be('ec3edbf3b05a449fc206a0138c739c3b'); - // expect(md5('𠜎')).to.be('b90869aaf121210f6c563973fa855650'); - // }); - // }); - - // describe('more than 64 bytes', function() { - // it('should be successful', function() { - // expect(md5('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('edce615b179e6e29be23145b77ebbd61'); - // expect(md5('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('ad36c9ab669a0ba9ce46d3ce9134de34'); - // }); - // }); - // }); - - // describe('special length', function() { - // it('should be successful', function() { - // expect(md5('0123456780123456780123456780123456780123456780123456780')).to.be('a119de63e4b2398427da06dd780263b3'); - // expect(md5('01234567801234567801234567801234567801234567801234567801')).to.be('ddafd84ebe63aebc4626b037a569d78b'); - // expect(md5('0123456780123456780123456780123456780123456780123456780123456780')).to.be('9ea04d743618797ce464445b5785a630'); - // expect(md5('01234567801234567801234567801234567801234567801234567801234567801234567')).to.be('658d914ae42c4938874b2e786ccda479'); - // expect(md5('012345678012345678012345678012345678012345678012345678012345678012345678')).to.be('a083a3710d685793f1f17988bfe3c175'); - // expect(md5('012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678012345678')).to.be('2b21a843cfc31c8026a0d835bc91bc98'); - // }); - // }); - // }); - - // describe('Array', function() { - // describe('Array', function() { - // it('should be successful', function() { - // expect(md5([])).to.be('d41d8cd98f00b204e9800998ecf8427e'); - // expect(md5([0])).to.be('93b885adfe0da089cdf634904fd59f71'); - // expect(md5([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])).to.be('9e107d9d372bb6826bd81d3542a419d6'); - // }); - // }); - - // describe('Uint8Array', function() { - // it('should be successful', function() { - // expect(md5(new Uint8Array([]))).to.be('d41d8cd98f00b204e9800998ecf8427e'); - // expect(md5(new Uint8Array(371))).to.be('58f494c2a0fb65332110fb62ae5c4a74'); - // }); - // }); - - // describe('ArrayBuffer', function() { - // it('should be successful', function() { - // expect(md5(new ArrayBuffer(0))).to.be('d41d8cd98f00b204e9800998ecf8427e'); - // expect(md5(new ArrayBuffer(1))).to.be('93b885adfe0da089cdf634904fd59f71'); - // }); - // }); - // }); })(md5);