Comparing version 0.1.2 to 0.1.3
@@ -1,5 +0,10 @@ | ||
(function(r,w){var t={0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,a:10,b:11,c:12,d:13,e:14,f:15,A:10,B:11,C:12,D:13,E:14,F:15},u=[7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21],v=[3614090360,3905402710,606105819,3250441966,4118548399,1200080426,2821735955,4249261313,1770035416,2336552879,4294925233,2304563134,1804603682,4254626195,2792965006,1236535329,4129170786,3225465664,643717713, | ||
3921069994,3593408605,38016083,3634488961,3889429448,568446438,3275163606,4107603335,1163531501,2850285829,4243563512,1735328473,2368359562,4294588738,2272392833,1839030562,4259657740,2763975236,1272893353,4139469664,3200236656,681279174,3936430074,3572445317,76029189,3654602809,3873151461,530742520,3299628645,4096336452,1126891415,2878612391,4237533241,1700485571,2399980690,4293915773,2240044497,1873313359,4264355552,2734768916,1309151649,4149444226,3174756917,718787259,3951481745],q=function(e){var a; | ||
a:{for(a=e.length;a--;)if(127<e.charCodeAt(a)){a=!0;break a}a=!1}if(a){var b=encodeURIComponent(e);e=[];var c=0;a=0;for(var d=b.length;c<d;++c){var m=b.charCodeAt(c);e[a>>2]=37==m?e[a>>2]|(t[b.charAt(++c)]<<4|t[b.charAt(++c)])<<(a%4<<3):e[a>>2]|m<<(a%4<<3);++a}b=(a+8>>6)+1<<4;c=a>>2;e[c]|=128<<(a%4<<3);for(c+=1;c<b;++c)e[c]=0;e[b-2]=a<<3}else{a=e.length;c=(a+8>>6)+1<<4;b=[];for(d=0;d<c;++d)b[d]=0;for(d=0;d<a;++d)b[d>>2]|=e.charCodeAt(d)<<(d%4<<3);b[d>>2]|=128<<(d%4<<3);b[c-2]=a<<3;e=b}a=1732584193; | ||
for(var c=4023233417,b=2562383102,d=271733878,m=0,r=e.length;m<r;m+=16){for(var n=a,g=c,h=b,k=d,l,p,q,f=0;64>f;++f)16>f?(l=k^g&(h^k),p=f):32>f?(l=h^k&(g^h),p=(5*f+1)%16):48>f?(l=g^h^k,p=(3*f+5)%16):(l=h^(g|~k),p=7*f%16),q=k,k=h,h=g,n=n+l+v[f]+e[m+p],l=u[f],g+=n<<l|n>>>32-l,n=q;a=a+n|0;c=c+g|0;b=b+h|0;d=d+k|0}return s(a)+s(c)+s(b)+s(d)},s=function(e){for(var a="",b=0;4>b;b++)var c=b<<3,a=a+("0123456789abcdef".charAt(e>>c+4&15)+"0123456789abcdef".charAt(e>>c&15));return a};"undefined"!=typeof module? | ||
module.exports=q:r&&(r.md5=q)})(this); | ||
(function(m,v){var t="0123456789abcdef".split(""),u=function(h,l){var e,k=1732584193,n=4023233417,p=2562383102,q=271733878,b,d,a,c,g;if(!l&&/[^\x00-\x7F]/.test(h)){e=[];for(d=b=0;d<h.length;d++)a=h.charCodeAt(d),128>a?e[b++]=a:(2048>a?e[b++]=192|a>>6:(55296>a||57344<=a?e[b++]=224|a>>12:(a=65536+((a&1023)<<10|h.charCodeAt(++d)&1023),e[b++]=240|a>>18,e[b++]=128|a>>12&63),e[b++]=128|a>>6&63),e[b++]=128|a&63);b=e.length;d=(b+8>>6)+1<<4;a=[];for(c=0;c<d;++c)a[c]=0;for(c=0;c<b;++c)a[c>>2]|=e[c]<<((c&3)<< | ||
3);a[c>>2]|=128<<((c&3)<<3);a[d-2]=b<<3;e=a}else{e=h.length;b=(e+8>>6)+1<<4;d=[];for(a=0;a<b;++a)d[a]=0;for(a=0;a<e;++a)d[a>>2]|=h.charCodeAt(a)<<((a&3)<<3);d[a>>2]|=128<<((a&3)<<3);d[b-2]=e<<3;e=d}for(var f=0,m=e.length;f<m;f+=16)0===f?(b=e[f+0]-680876937,b=(b<<7|b>>>25)-271733879<<0,c=e[f+1]-117830708+(2004318071&b^-1732584194),c=(c<<12|c>>>20)+b<<0,a=e[f+2]-1126478375+((b^-271733879)&c^-271733879),a=(a<<17|a>>>15)+c<<0,d=e[f+3]-1316259209+((c^b)&a^b)):(b=k,d=n,a=p,c=q,b+=(c^d&(a^c))+e[f+0]-680876936, | ||
b=(b<<7|b>>>25)+d<<0,c+=(a^b&(d^a))+e[f+1]-389564586,c=(c<<12|c>>>20)+b<<0,a+=(d^c&(b^d))+e[f+2]+606105819,a=(a<<17|a>>>15)+c<<0,d+=(b^a&(c^b))+e[f+3]-1044525330),d=(d<<22|d>>>10)+a<<0,b+=(c^d&(a^c))+e[f+4]-176418897,b=(b<<7|b>>>25)+d<<0,c+=(a^b&(d^a))+e[f+5]+1200080426,c=(c<<12|c>>>20)+b<<0,a+=(d^c&(b^d))+e[f+6]-1473231341,a=(a<<17|a>>>15)+c<<0,d+=(b^a&(c^b))+e[f+7]-45705983,d=(d<<22|d>>>10)+a<<0,b+=(c^d&(a^c))+e[f+8]+1770035416,b=(b<<7|b>>>25)+d<<0,c+=(a^b&(d^a))+e[f+9]-1958414417,c=(c<<12|c>>> | ||
20)+b<<0,a+=(d^c&(b^d))+e[f+10]-42063,a=(a<<17|a>>>15)+c<<0,d+=(b^a&(c^b))+e[f+11]-1990404162,d=(d<<22|d>>>10)+a<<0,b+=(c^d&(a^c))+e[f+12]+1804603682,b=(b<<7|b>>>25)+d<<0,c+=(a^b&(d^a))+e[f+13]-40341101,c=(c<<12|c>>>20)+b<<0,a+=(d^c&(b^d))+e[f+14]-1502002290,a=(a<<17|a>>>15)+c<<0,d+=(b^a&(c^b))+e[f+15]+1236535329,d=(d<<22|d>>>10)+a<<0,b+=(a^c&(d^a))+e[f+1]-165796510,b=(b<<5|b>>>27)+d<<0,c+=(d^a&(b^d))+e[f+6]-1069501632,c=(c<<9|c>>>23)+b<<0,a+=(b^d&(c^b))+e[f+11]+643717713,a=(a<<14|a>>>18)+c<<0,d+= | ||
(c^b&(a^c))+e[f+0]-373897302,d=(d<<20|d>>>12)+a<<0,b+=(a^c&(d^a))+e[f+5]-701558691,b=(b<<5|b>>>27)+d<<0,c+=(d^a&(b^d))+e[f+10]+38016083,c=(c<<9|c>>>23)+b<<0,a+=(b^d&(c^b))+e[f+15]-660478335,a=(a<<14|a>>>18)+c<<0,d+=(c^b&(a^c))+e[f+4]-405537848,d=(d<<20|d>>>12)+a<<0,b+=(a^c&(d^a))+e[f+9]+568446438,b=(b<<5|b>>>27)+d<<0,c+=(d^a&(b^d))+e[f+14]-1019803690,c=(c<<9|c>>>23)+b<<0,a+=(b^d&(c^b))+e[f+3]-187363961,a=(a<<14|a>>>18)+c<<0,d+=(c^b&(a^c))+e[f+8]+1163531501,d=(d<<20|d>>>12)+a<<0,b+=(a^c&(d^a))+e[f+ | ||
13]-1444681467,b=(b<<5|b>>>27)+d<<0,c+=(d^a&(b^d))+e[f+2]-51403784,c=(c<<9|c>>>23)+b<<0,a+=(b^d&(c^b))+e[f+7]+1735328473,a=(a<<14|a>>>18)+c<<0,d+=(c^b&(a^c))+e[f+12]-1926607734,d=(d<<20|d>>>12)+a<<0,g=d^a,b+=(g^c)+e[f+5]-378558,b=(b<<4|b>>>28)+d<<0,g=d^a,c+=(g^b)+e[f+8]-2022574463,c=(c<<11|c>>>21)+b<<0,g=c^b,a+=(g^d)+e[f+11]+1839030562,a=(a<<16|a>>>16)+c<<0,g=c^b,d+=(g^a)+e[f+14]-35309556,d=(d<<23|d>>>9)+a<<0,g=d^a,b+=(g^c)+e[f+1]-1530992060,b=(b<<4|b>>>28)+d<<0,g=d^a,c+=(g^b)+e[f+4]+1272893353,c= | ||
(c<<11|c>>>21)+b<<0,g=c^b,a+=(g^d)+e[f+7]-155497632,a=(a<<16|a>>>16)+c<<0,g=c^b,d+=(g^a)+e[f+10]-1094730640,d=(d<<23|d>>>9)+a<<0,g=d^a,b+=(g^c)+e[f+13]+681279174,b=(b<<4|b>>>28)+d<<0,g=d^a,c+=(g^b)+e[f+0]-358537222,c=(c<<11|c>>>21)+b<<0,g=c^b,a+=(g^d)+e[f+3]-722521979,a=(a<<16|a>>>16)+c<<0,g=c^b,d+=(g^a)+e[f+6]+76029189,d=(d<<23|d>>>9)+a<<0,g=d^a,b+=(g^c)+e[f+9]-640364487,b=(b<<4|b>>>28)+d<<0,g=d^a,c+=(g^b)+e[f+12]-421815835,c=(c<<11|c>>>21)+b<<0,g=c^b,a+=(g^d)+e[f+15]+530742520,a=(a<<16|a>>>16)+ | ||
c<<0,g=c^b,d+=(g^a)+e[f+2]-995338651,d=(d<<23|d>>>9)+a<<0,b+=(a^(d|~c))+e[f+0]-198630844,b=(b<<6|b>>>26)+d<<0,c+=(d^(b|~a))+e[f+7]+1126891415,c=(c<<10|c>>>22)+b<<0,a+=(b^(c|~d))+e[f+14]-1416354905,a=(a<<15|a>>>17)+c<<0,d+=(c^(a|~b))+e[f+5]-57434055,d=(d<<21|d>>>11)+a<<0,b+=(a^(d|~c))+e[f+12]+1700485571,b=(b<<6|b>>>26)+d<<0,c+=(d^(b|~a))+e[f+3]-1894986606,c=(c<<10|c>>>22)+b<<0,a+=(b^(c|~d))+e[f+10]-1051523,a=(a<<15|a>>>17)+c<<0,d+=(c^(a|~b))+e[f+1]-2054922799,d=(d<<21|d>>>11)+a<<0,b+=(a^(d|~c))+e[f+ | ||
8]+1873313359,b=(b<<6|b>>>26)+d<<0,c+=(d^(b|~a))+e[f+15]-30611744,c=(c<<10|c>>>22)+b<<0,a+=(b^(c|~d))+e[f+6]-1560198380,a=(a<<15|a>>>17)+c<<0,d+=(c^(a|~b))+e[f+13]+1309151649,d=(d<<21|d>>>11)+a<<0,b+=(a^(d|~c))+e[f+4]-145523070,b=(b<<6|b>>>26)+d<<0,c+=(d^(b|~a))+e[f+11]-1120210379,c=(c<<10|c>>>22)+b<<0,a+=(b^(c|~d))+e[f+2]+718787259,a=(a<<15|a>>>17)+c<<0,d+=(c^(a|~b))+e[f+9]-343485551,d=(d<<21|d>>>11)+a<<0,k=k+b<<0,n=n+d<<0,p=p+a<<0,q=q+c<<0;return r(k)+r(n)+r(p)+r(q)},r=function(h){for(var l="", | ||
e=0;4>e;e++)var k=e<<3,l=l+(t[h>>k+4&15]+t[h>>k&15]);return l};"undefined"!=typeof module?module.exports=u:m&&(m.md5=u)})(this); |
@@ -0,1 +1,9 @@ | ||
# v0.1.3 / 2015-01-05 | ||
* Improve performance. | ||
* Add bower package. | ||
* Fixed JSHint warnings. | ||
* Add travis. | ||
* Add coveralls. | ||
# v0.1.2 / 2014-07-27 | ||
@@ -2,0 +10,0 @@ |
@@ -1,2 +0,2 @@ | ||
Copyright 2014 emn178@gmail.com | ||
Copyright 2014-2015 emn178@gmail.com | ||
@@ -3,0 +3,0 @@ Permission is hereby granted, free of charge, to any person obtaining |
{ | ||
"name": "js-md5", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "A simple MD5 hash function for JavaScript supports UTF-8 encoding.", | ||
"main": "src/md5.js", | ||
"devDependencies": { | ||
"expect.js": "~0.3.1", | ||
"jscoverage": "~0.5.9" | ||
}, | ||
"scripts": { | ||
"test": "node tests/node-test.js" | ||
"test": "mocha tests/node-test.js -r jscoverage", | ||
"coveralls": "mocha tests/node-test.js -R mocha-lcov-reporter -r jscoverage | coveralls" | ||
}, | ||
@@ -9,0 +14,0 @@ "repository": { |
# js-md5 | ||
[![Build Status](https://api.travis-ci.org/emn178/js-md5.png)](https://travis-ci.org/emn178/js-md5) | ||
[![Build Status](https://coveralls.io/repos/emn178/js-md5/badge.png?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/) | ||
A simple MD5 hash function for JavaScript supports UTF-8 encoding. | ||
## Demo | ||
[MD5 Online](http://emn178.github.io/online-tools/md5.html) | ||
## Download | ||
[Compress](https://raw.github.com/emn178/js-md5/master/build/md5.min.js) | ||
[Uncompress](https://raw.github.com/emn178/js-md5/master/src/md5.js) | ||
## Install | ||
You can also install js-md5 by using Bower. | ||
bower install js-md5 | ||
For node.js, you can use this command to install: | ||
@@ -10,2 +24,6 @@ | ||
## Usage | ||
You could use like this: | ||
```JavaScript | ||
md5('Message to hash'); | ||
``` | ||
If you use node.js, you should require the module first: | ||
@@ -15,6 +33,17 @@ ```JavaScript | ||
``` | ||
And you could use like this: | ||
```JavaScript | ||
md5('Message to hash'); | ||
``` | ||
### Methods | ||
#### md5(str, asciiOnly) | ||
Hash string to md5, set asciiOnly to true for better performace if you ensure input is ascii. | ||
##### *str: `String`* | ||
String to hash. | ||
##### *asciiOnly: `Boolean` (default: `false`)* | ||
Specify the string encoding is ASCII. | ||
## Example | ||
@@ -24,4 +53,4 @@ Code | ||
md5(''); | ||
md5('The quick brown fox jumps over the lazy dog'); | ||
md5('The quick brown fox jumps over the lazy dog.'); | ||
md5('The quick brown fox jumps over the lazy dog', true); | ||
md5('The quick brown fox jumps over the lazy dog.', true); | ||
``` | ||
@@ -44,11 +73,6 @@ Output | ||
## Tests | ||
You can open `tests/index.html` in browser or use node.js to run test | ||
## Benchmark | ||
[ASCII](http://jsperf.com/md5-ascii/4) | ||
[UTF8](http://jsperf.com/md5-utf8/3) | ||
node tests/node-test.js | ||
or | ||
npm test | ||
## Extensions | ||
@@ -55,0 +79,0 @@ ### jQuery |
345
src/md5.js
/* | ||
* js-md5 v0.1.2 | ||
* js-md5 v0.1.3 | ||
* https://github.com/emn178/js-md5 | ||
* | ||
* Copyright 2014, emn178@gmail.com | ||
* Copyright 2014-2015, emn178@gmail.com | ||
* | ||
@@ -10,90 +10,183 @@ * Licensed under the MIT license: | ||
*/ | ||
(function(root, undefined){ | ||
;(function(root, undefined) { | ||
'use strict'; | ||
var HEX_CHARS = "0123456789abcdef"; | ||
var HEX_TABLE = { | ||
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, | ||
'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15, | ||
'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15 | ||
}; | ||
var HEX_CHARS = '0123456789abcdef'.split(''); | ||
var R = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, | ||
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, | ||
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, | ||
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]; | ||
var md5 = function(message, asciiOnly) { | ||
var blocks, h0 = 0x67452301, h1 = 0xEFCDAB89, h2 = 0x98BADCFE, h3 = 0x10325476, | ||
a, b, c, d, bc, da; | ||
if(!asciiOnly && /[^\x00-\x7F]/.test(message)) { | ||
blocks = getBlocksFromUtf8(message); | ||
} else { | ||
blocks = getBlocksFromAscii(message); | ||
} | ||
for(var i = 0, length = blocks.length;i < length;i += 16) { | ||
if(i === 0) { | ||
a = blocks[i + 0] - 680876937; | ||
a = ((a << 7 | a >>> 25) - 271733879) << 0; | ||
d = blocks[i + 1] - 117830708 + ((2004318071 & a) ^ -1732584194); | ||
d = ((d << 12 | d >>> 20) + a) << 0; | ||
c = blocks[i + 2] - 1126478375 + (((a ^ -271733879) & d) ^ -271733879); | ||
c = ((c << 17 | c >>> 15) + d) << 0; | ||
b = blocks[i + 3] - 1316259209 + (((d ^ a) & c) ^ a); | ||
b = ((b << 22 | b >>> 10) + c) << 0; | ||
} else { | ||
a = h0; | ||
b = h1; | ||
c = h2; | ||
d = h3; | ||
a += (d ^ (b & (c ^ d))) + blocks[i + 0] - 680876936; | ||
a = ((a << 7 | a >>> 25) + b) << 0; | ||
d += (c ^ (a & (b ^ c))) + blocks[i + 1] - 389564586; | ||
d = ((d << 12 | d >>> 20) + a) << 0; | ||
c += (b ^ (d & (a ^ b))) + blocks[i + 2] + 606105819; | ||
c = ((c << 17 | c >>> 15) + d) << 0; | ||
b += (a ^ (c & (d ^ a))) + blocks[i + 3] - 1044525330; | ||
b = ((b << 22 | b >>> 10) + c) << 0; | ||
} | ||
var K = [0XD76AA478, 0XE8C7B756, 0X242070DB, 0XC1BDCEEE, | ||
0XF57C0FAF, 0X4787C62A, 0XA8304613, 0XFD469501, | ||
0X698098D8, 0X8B44F7AF, 0XFFFF5BB1, 0X895CD7BE, | ||
0X6B901122, 0XFD987193, 0XA679438E, 0X49B40821, | ||
0XF61E2562, 0XC040B340, 0X265E5A51, 0XE9B6C7AA, | ||
0XD62F105D, 0X02441453, 0XD8A1E681, 0XE7D3FBC8, | ||
0X21E1CDE6, 0XC33707D6, 0XF4D50D87, 0X455A14ED, | ||
0XA9E3E905, 0XFCEFA3F8, 0X676F02D9, 0X8D2A4C8A, | ||
0XFFFA3942, 0X8771F681, 0X6D9D6122, 0XFDE5380C, | ||
0XA4BEEA44, 0X4BDECFA9, 0XF6BB4B60, 0XBEBFBC70, | ||
0X289B7EC6, 0XEAA127FA, 0XD4EF3085, 0X04881D05, | ||
0XD9D4D039, 0XE6DB99E5, 0X1FA27CF8, 0XC4AC5665, | ||
0XF4292244, 0X432AFF97, 0XAB9423A7, 0XFC93A039, | ||
0X655B59C3, 0X8F0CCC92, 0XFFEFF47D, 0X85845DD1, | ||
0X6FA87E4F, 0XFE2CE6E0, 0XA3014314, 0X4E0811A1, | ||
0XF7537E82, 0XBD3AF235, 0X2AD7D2BB, 0XEB86D391]; | ||
a += (d ^ (b & (c ^ d))) + blocks[i + 4] - 176418897; | ||
a = ((a << 7 | a >>> 25) + b) << 0; | ||
d += (c ^ (a & (b ^ c))) + blocks[i + 5] + 1200080426; | ||
d = ((d << 12 | d >>> 20) + a) << 0; | ||
c += (b ^ (d & (a ^ b))) + blocks[i + 6] - 1473231341; | ||
c = ((c << 17 | c >>> 15) + d) << 0; | ||
b += (a ^ (c & (d ^ a))) + blocks[i + 7] - 45705983; | ||
b = ((b << 22 | b >>> 10) + c) << 0; | ||
a += (d ^ (b & (c ^ d))) + blocks[i + 8] + 1770035416; | ||
a = ((a << 7 | a >>> 25) + b) << 0; | ||
d += (c ^ (a & (b ^ c))) + blocks[i + 9] - 1958414417; | ||
d = ((d << 12 | d >>> 20) + a) << 0; | ||
c += (b ^ (d & (a ^ b))) + blocks[i + 10] - 42063; | ||
c = ((c << 17 | c >>> 15) + d) << 0; | ||
b += (a ^ (c & (d ^ a))) + blocks[i + 11] - 1990404162; | ||
b = ((b << 22 | b >>> 10) + c) << 0; | ||
a += (d ^ (b & (c ^ d))) + blocks[i + 12] + 1804603682; | ||
a = ((a << 7 | a >>> 25) + b) << 0; | ||
d += (c ^ (a & (b ^ c))) + blocks[i + 13] - 40341101; | ||
d = ((d << 12 | d >>> 20) + a) << 0; | ||
c += (b ^ (d & (a ^ b))) + blocks[i + 14] - 1502002290; | ||
c = ((c << 17 | c >>> 15) + d) << 0; | ||
b += (a ^ (c & (d ^ a))) + blocks[i + 15] + 1236535329; | ||
b = ((b << 22 | b >>> 10) + c) << 0; | ||
a += (c ^ (d & (b ^ c))) + blocks[i + 1] - 165796510; | ||
a = ((a << 5 | a >>> 27) + b) << 0; | ||
d += (b ^ (c & (a ^ b))) + blocks[i + 6] - 1069501632; | ||
d = ((d << 9 | d >>> 23) + a) << 0; | ||
c += (a ^ (b & (d ^ a))) + blocks[i + 11] + 643717713; | ||
c = ((c << 14 | c >>> 18) + d) << 0; | ||
b += (d ^ (a & (c ^ d))) + blocks[i + 0] - 373897302; | ||
b = ((b << 20 | b >>> 12) + c) << 0; | ||
a += (c ^ (d & (b ^ c))) + blocks[i + 5] - 701558691; | ||
a = ((a << 5 | a >>> 27) + b) << 0; | ||
d += (b ^ (c & (a ^ b))) + blocks[i + 10] + 38016083; | ||
d = ((d << 9 | d >>> 23) + a) << 0; | ||
c += (a ^ (b & (d ^ a))) + blocks[i + 15] - 660478335; | ||
c = ((c << 14 | c >>> 18) + d) << 0; | ||
b += (d ^ (a & (c ^ d))) + blocks[i + 4] - 405537848; | ||
b = ((b << 20 | b >>> 12) + c) << 0; | ||
a += (c ^ (d & (b ^ c))) + blocks[i + 9] + 568446438; | ||
a = ((a << 5 | a >>> 27) + b) << 0; | ||
d += (b ^ (c & (a ^ b))) + blocks[i + 14] - 1019803690; | ||
d = ((d << 9 | d >>> 23) + a) << 0; | ||
c += (a ^ (b & (d ^ a))) + blocks[i + 3] - 187363961; | ||
c = ((c << 14 | c >>> 18) + d) << 0; | ||
b += (d ^ (a & (c ^ d))) + blocks[i + 8] + 1163531501; | ||
b = ((b << 20 | b >>> 12) + c) << 0; | ||
a += (c ^ (d & (b ^ c))) + blocks[i + 13] - 1444681467; | ||
a = ((a << 5 | a >>> 27) + b) << 0; | ||
d += (b ^ (c & (a ^ b))) + blocks[i + 2] - 51403784; | ||
d = ((d << 9 | d >>> 23) + a) << 0; | ||
c += (a ^ (b & (d ^ a))) + blocks[i + 7] + 1735328473; | ||
c = ((c << 14 | c >>> 18) + d) << 0; | ||
b += (d ^ (a & (c ^ d))) + blocks[i + 12] - 1926607734; | ||
b = ((b << 20 | b >>> 12) + c) << 0; | ||
bc = b ^ c; | ||
a += (bc ^ d) + blocks[i + 5] - 378558; | ||
a = ((a << 4 | a >>> 28) + b) << 0; | ||
bc = b ^ c; | ||
d += (bc ^ a) + blocks[i + 8] - 2022574463; | ||
d = ((d << 11 | d >>> 21) + a) << 0; | ||
da = d ^ a; | ||
c += (da ^ b) + blocks[i + 11] + 1839030562; | ||
c = ((c << 16 | c >>> 16) + d) << 0; | ||
da = d ^ a; | ||
b += (da ^ c) + blocks[i + 14] - 35309556; | ||
b = ((b << 23 | b >>> 9) + c) << 0; | ||
bc = b ^ c; | ||
a += (bc ^ d) + blocks[i + 1] - 1530992060; | ||
a = ((a << 4 | a >>> 28) + b) << 0; | ||
bc = b ^ c; | ||
d += (bc ^ a) + blocks[i + 4] + 1272893353; | ||
d = ((d << 11 | d >>> 21) + a) << 0; | ||
da = d ^ a; | ||
c += (da ^ b) + blocks[i + 7] - 155497632; | ||
c = ((c << 16 | c >>> 16) + d) << 0; | ||
da = d ^ a; | ||
b += (da ^ c) + blocks[i + 10] - 1094730640; | ||
b = ((b << 23 | b >>> 9) + c) << 0; | ||
bc = b ^ c; | ||
a += (bc ^ d) + blocks[i + 13] + 681279174; | ||
a = ((a << 4 | a >>> 28) + b) << 0; | ||
bc = b ^ c; | ||
d += (bc ^ a) + blocks[i + 0] - 358537222; | ||
d = ((d << 11 | d >>> 21) + a) << 0; | ||
da = d ^ a; | ||
c += (da ^ b) + blocks[i + 3] - 722521979; | ||
c = ((c << 16 | c >>> 16) + d) << 0; | ||
da = d ^ a; | ||
b += (da ^ c) + blocks[i + 6] + 76029189; | ||
b = ((b << 23 | b >>> 9) + c) << 0; | ||
bc = b ^ c; | ||
a += (bc ^ d) + blocks[i + 9] - 640364487; | ||
a = ((a << 4 | a >>> 28) + b) << 0; | ||
bc = b ^ c; | ||
d += (bc ^ a) + blocks[i + 12] - 421815835; | ||
d = ((d << 11 | d >>> 21) + a) << 0; | ||
da = d ^ a; | ||
c += (da ^ b) + blocks[i + 15] + 530742520; | ||
c = ((c << 16 | c >>> 16) + d) << 0; | ||
da = d ^ a; | ||
b += (da ^ c) + blocks[i + 2] - 995338651; | ||
b = ((b << 23 | b >>> 9) + c) << 0; | ||
a += (c ^ (b | ~d)) + blocks[i + 0] - 198630844; | ||
a = ((a << 6 | a >>> 26) + b) << 0; | ||
d += (b ^ (a | ~c)) + blocks[i + 7] + 1126891415; | ||
d = ((d << 10 | d >>> 22) + a) << 0; | ||
c += (a ^ (d | ~b)) + blocks[i + 14] - 1416354905; | ||
c = ((c << 15 | c >>> 17) + d) << 0; | ||
b += (d ^ (c | ~a)) + blocks[i + 5] - 57434055; | ||
b = ((b << 21 | b >>> 11) + c) << 0; | ||
a += (c ^ (b | ~d)) + blocks[i + 12] + 1700485571; | ||
a = ((a << 6 | a >>> 26) + b) << 0; | ||
d += (b ^ (a | ~c)) + blocks[i + 3] - 1894986606; | ||
d = ((d << 10 | d >>> 22) + a) << 0; | ||
c += (a ^ (d | ~b)) + blocks[i + 10] - 1051523; | ||
c = ((c << 15 | c >>> 17) + d) << 0; | ||
b += (d ^ (c | ~a)) + blocks[i + 1] - 2054922799; | ||
b = ((b << 21 | b >>> 11) + c) << 0; | ||
a += (c ^ (b | ~d)) + blocks[i + 8] + 1873313359; | ||
a = ((a << 6 | a >>> 26) + b) << 0; | ||
d += (b ^ (a | ~c)) + blocks[i + 15] - 30611744; | ||
d = ((d << 10 | d >>> 22) + a) << 0; | ||
c += (a ^ (d | ~b)) + blocks[i + 6] - 1560198380; | ||
c = ((c << 15 | c >>> 17) + d) << 0; | ||
b += (d ^ (c | ~a)) + blocks[i + 13] + 1309151649; | ||
b = ((b << 21 | b >>> 11) + c) << 0; | ||
a += (c ^ (b | ~d)) + blocks[i + 4] - 145523070; | ||
a = ((a << 6 | a >>> 26) + b) << 0; | ||
d += (b ^ (a | ~c)) + blocks[i + 11] - 1120210379; | ||
d = ((d << 10 | d >>> 22) + a) << 0; | ||
c += (a ^ (d | ~b)) + blocks[i + 2] + 718787259; | ||
c = ((c << 15 | c >>> 17) + d) << 0; | ||
b += (d ^ (c | ~a)) + blocks[i + 9] - 343485551; | ||
b = ((b << 21 | b >>> 11) + c) << 0; | ||
var md5 = function(message) { | ||
var blocks = hasUTF8(message) ? UTF8toBlocks(message) : ASCIItoBlocks(message); | ||
var h0 = 0x67452301; | ||
var h1 = 0xEFCDAB89; | ||
var h2 = 0x98BADCFE; | ||
var h3 = 0x10325476; | ||
h0 = (h0 + a) << 0; | ||
h1 = (h1 + b) << 0; | ||
h2 = (h2 + c) << 0; | ||
h3 = (h3 + d) << 0; | ||
} | ||
for(var i = 0, length = blocks.length;i < length;i += 16) | ||
{ | ||
var a = h0; | ||
var b = h1; | ||
var c = h2; | ||
var d = h3; | ||
var f, g, tmp, x, y; | ||
for(var j = 0;j < 64;++j) | ||
{ | ||
if(j < 16) | ||
{ | ||
// f = (b & c) | ((~b) & d); | ||
f = d ^ (b & (c ^ d)); | ||
g = j; | ||
} | ||
else if(j < 32) | ||
{ | ||
// f = (d & b) | ((~d) & c); | ||
f = c ^ (d & (b ^ c)); | ||
g = (5 * j + 1) % 16; | ||
} | ||
else if(j < 48) | ||
{ | ||
f = b ^ c ^ d; | ||
g = (3 * j + 5) % 16; | ||
} | ||
else | ||
{ | ||
f = c ^ (b | (~d)); | ||
g = (7 * j) % 16; | ||
} | ||
tmp = d; | ||
d = c | ||
c = b | ||
// leftrotate | ||
x = (a + f + K[j] + blocks[i + g]); | ||
y = R[j]; | ||
b += (x << y) | (x >>> (32 - y)); | ||
a = tmp; | ||
} | ||
h0 = (h0 + a) | 0; | ||
h1 = (h1 + b) | 0; | ||
h2 = (h2 + c) | 0; | ||
h3 = (h3 + d) | 0; | ||
} | ||
return toHexString(h0) + toHexString(h1) + toHexString(h2) + toHexString(h3); | ||
@@ -103,7 +196,6 @@ }; | ||
var toHexString = function(num) { | ||
var hex = ""; | ||
for(var i = 0; i < 4; i++) | ||
{ | ||
var hex = ''; | ||
for(var i = 0; i < 4; i++) { | ||
var offset = i << 3; | ||
hex += HEX_CHARS.charAt((num >> (offset + 4)) & 0x0F) + HEX_CHARS.charAt((num >> offset) & 0x0F); | ||
hex += HEX_CHARS[(num >> (offset + 4)) & 0x0F] + HEX_CHARS[(num >> offset) & 0x0F]; | ||
} | ||
@@ -113,11 +205,27 @@ return hex; | ||
var hasUTF8 = function(message) { | ||
var i = message.length; | ||
while(i--) | ||
if(message.charCodeAt(i) > 127) | ||
return true; | ||
return false; | ||
var getBytesFromUtf8 = function(str) { | ||
var bytes = [], index = 0; | ||
for (var i = 0;i < str.length; i++) { | ||
var c = str.charCodeAt(i); | ||
if (c < 0x80) { | ||
bytes[index++] = c; | ||
} else if (c < 0x800) { | ||
bytes[index++] = 0xc0 | (c >> 6); | ||
bytes[index++] = 0x80 | (c & 0x3f); | ||
} else if (c < 0xd800 || c >= 0xe000) { | ||
bytes[index++] = 0xe0 | (c >> 12); | ||
bytes[index++] = 0x80 | ((c >> 6) & 0x3f); | ||
bytes[index++] = 0x80 | (c & 0x3f); | ||
} else { | ||
c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff)); | ||
bytes[index++] = 0xf0 | (c >> 18); | ||
bytes[index++] = 0x80 | ((c >> 12) & 0x3f); | ||
bytes[index++] = 0x80 | ((c >> 6) & 0x3f); | ||
bytes[index++] = 0x80 | (c & 0x3f); | ||
} | ||
} | ||
return bytes; | ||
}; | ||
var ASCIItoBlocks = function(message) { | ||
var getBlocksFromAscii = function(message) { | ||
// a block is 32 bits(4 bytes), a chunk is 512 bits(64 bytes) | ||
@@ -127,9 +235,10 @@ var length = message.length; | ||
var blockCount = chunkCount << 4; // chunkCount * 16 | ||
var blocks = []; | ||
var i; | ||
for(i = 0;i < blockCount;++i) | ||
var blocks = [], i; | ||
for(i = 0;i < blockCount;++i) { | ||
blocks[i] = 0; | ||
for(i = 0;i < length;++i) | ||
blocks[i >> 2] |= message.charCodeAt(i) << ((i % 4) << 3); | ||
blocks[i >> 2] |= 0x80 << ((i % 4) << 3); | ||
} | ||
for(i = 0;i < length;++i) { | ||
blocks[i >> 2] |= message.charCodeAt(i) << ((i & 3) << 3); | ||
} | ||
blocks[i >> 2] |= 0x80 << ((i & 3) << 3); | ||
blocks[blockCount - 2] = length << 3; // length * 8 | ||
@@ -139,28 +248,24 @@ return blocks; | ||
var UTF8toBlocks = function(message) { | ||
var uri = encodeURIComponent(message); | ||
var blocks = []; | ||
for(var i = 0, bytes = 0, length = uri.length;i < length;++i) | ||
{ | ||
var c = uri.charCodeAt(i); | ||
if(c == 37) // % | ||
blocks[bytes >> 2] |= ((HEX_TABLE[uri.charAt(++i)] << 4) | HEX_TABLE[uri.charAt(++i)]) << ((bytes % 4) << 3); | ||
else | ||
blocks[bytes >> 2] |= c << ((bytes % 4) << 3); | ||
++bytes; | ||
} | ||
var chunkCount = ((bytes + 8) >> 6) + 1; | ||
var getBlocksFromUtf8 = function(message) { | ||
var bytes = getBytesFromUtf8(message); | ||
var length = bytes.length; | ||
var chunkCount = ((length + 8) >> 6) + 1; | ||
var blockCount = chunkCount << 4; // chunkCount * 16 | ||
var index = bytes >> 2; | ||
blocks[index] |= 0x80 << ((bytes % 4) << 3); | ||
for(var i = index + 1;i < blockCount;++i) | ||
var blocks = [], i; | ||
for(i = 0;i < blockCount;++i) { | ||
blocks[i] = 0; | ||
blocks[blockCount - 2] = bytes << 3; // bytes * 8 | ||
} | ||
for(i = 0;i < length;++i) { | ||
blocks[i >> 2] |= bytes[i] << ((i & 3) << 3); | ||
} | ||
blocks[i >> 2] |= 0x80 << ((i & 3) << 3); | ||
blocks[blockCount - 2] = length << 3; // length * 8 | ||
return blocks; | ||
}; | ||
if(typeof(module) != 'undefined') | ||
if(typeof(module) != 'undefined') { | ||
module.exports = md5; | ||
else if(root) | ||
} else if(root) { | ||
root.md5 = md5; | ||
} | ||
}(this)); |
md5 = require('../src/md5.js'); | ||
require('./debug.js'); | ||
expect = require('expect.js'); | ||
require('./test.js'); |
@@ -1,5 +0,30 @@ | ||
assert('md5 1', 'd41d8cd98f00b204e9800998ecf8427e', md5('')); | ||
assert('md5 2', '9e107d9d372bb6826bd81d3542a419d6', md5('The quick brown fox jumps over the lazy dog')); | ||
assert('md5 3', 'e4d909c290d0fb1ca068ffaddf22cbd0', md5('The quick brown fox jumps over the lazy dog.')); | ||
assert('md5 4', 'a7bac2239fcdcb3a067903d8077c4a07', md5('中文')); | ||
assert('md5 5', 'ec3edbf3b05a449fc206a0138c739c3b', md5('aécio')); | ||
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'); | ||
}); | ||
}); | ||
describe('more than 64 bytes', function() { | ||
it('should be successful', function() { | ||
expect(md5('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('ad36c9ab669a0ba9ce46d3ce9134de34'); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27858
14
489
105
2
1