* Remove ascii parameter.

* Improve performance.
* Add test case.
pull/1/merge v0.2.0
Chen Yi-Cyuan 11 years ago
parent 32c00d7646
commit 4e7cf2c709

@ -3,10 +3,11 @@ node_js:
- "0.11" - "0.11"
- "0.10" - "0.10"
- "0.8" - "0.8"
install: before_install:
- npm install mocha -g - npm install mocha -g
- npm install coveralls -g - npm install coveralls -g
- npm install mocha-lcov-reporter -g - npm install mocha-lcov-reporter -g
- npm install script: npm run-script coveralls
script: branches:
- npm run-script coveralls only:
- master

@ -1,3 +1,9 @@
# v0.2.0 / 2015-02-28
* Remove ascii parameter.
* Improve performance.
* Add test case.
# v0.1.2 / 2015-01-07 # v0.1.2 / 2015-01-07
* Add bower package. * Add bower package.

@ -30,20 +30,6 @@ If you use node.js, you should require the module first:
md2 = require('js-md2'); md2 = require('js-md2');
``` ```
### Methods
#### md2(str, asciiOnly)
Hash string to md2, 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 ## Example
Code Code
```JavaScript ```JavaScript

@ -1,6 +1,6 @@
{ {
"name": "js-md2", "name": "js-md2",
"version": "0.1.2", "version": "0.2.0",
"main": ["build/md2.min.js"], "main": ["build/md2.min.js"],
"ignore": [ "ignore": [
"samples", "samples",

18
build/md2.min.js vendored

@ -1,4 +1,14 @@
(function(l,q){var m="0123456789abcdef".split(""),n=[41,46,67,201,162,216,124,1,61,54,84,161,236,240,6,19,98,167,5,243,192,199,115,140,152,147,43,217,188,76,130,202,30,155,87,60,253,212,224,22,103,66,111,24,138,23,229,18,190,78,196,214,218,158,222,73,160,251,245,142,187,47,238,122,169,104,121,145,21,178,7,63,148,194,16,137,11,34,95,33,128,127,93,154,90,144,50,39,53,62,204,231,191,247,151,3,255,25,48,179,72,165,181,209,215,94,146,42,172,86,170,198,79,184,56,210,150,164,125,182,118,252,107,226,156, /*
116,4,241,69,157,112,89,100,113,135,32,134,91,207,101,230,45,168,2,27,96,37,173,174,176,185,246,28,70,97,105,52,64,126,15,85,71,163,35,221,81,175,58,195,92,249,206,186,197,234,38,44,83,13,110,133,40,132,9,211,223,205,244,65,129,77,82,106,220,55,200,108,193,171,250,36,225,123,8,12,189,177,74,120,136,149,139,227,99,232,109,233,203,213,254,59,0,29,57,242,239,183,14,102,88,208,228,166,119,114,248,235,117,75,10,49,68,80,180,143,237,31,26,219,153,141,51,159,17,131,20],p=function(k,l){var d,h=[],b,a;if(!l&& * js-md5 v0.2.0
/[^\x00-\x7F]/.test(k)){d=[];for(a=b=0;a<k.length;a++){var c=k.charCodeAt(a);128>c?d[b++]=c:(2048>c?d[b++]=192|c>>6:(55296>c||57344<=c?d[b++]=224|c>>12:(c=65536+((c&1023)<<10|k.charCodeAt(++a)&1023),d[b++]=240|c>>18,d[b++]=128|c>>12&63),d[b++]=128|c>>6&63),d[b++]=128|c&63)}var f=d.length;b=(f>>4)+1<<4;a=[];for(c=0;c<f;++c)a[c]=d[c];for(d=16-(f&15);c<b;++c)a[c]=d;d=a}else{c=k.length;d=(c>>4)+1<<4;b=[];for(a=0;a<c;++a)b[a]=k.charCodeAt(a);for(c=16-(c&15);a<d;++a)b[a]=c;d=b}b=d;a=[];var c=0,f=b.length>> * https://github.com/emn178/js-md2
4,e;for(e=0;e<f;++e)for(var g=0;16>g;++g)a[g]^=n[b[(e<<4)+g]^c],c=a[g];f=b.length;for(e=0;16>e;++e)b[f+e]=a[e];for(b=0;48>b;++b)h[b]=0;c=d.length>>4;f="";for(b=0;b<c;++b){for(a=0;16>a;++a)h[16+a]=d[(b<<4)+a],h[32+a]=h[16+a]^h[a];for(a=e=0;18>a;++a){for(g=0;48>g;++g)h[g]=e=h[g]^n[e];e=e+a&255}}for(b=0;16>b;++b)f+=m[h[b]>>4&15]+m[h[b]&15];return f};"undefined"!=typeof module?module.exports=p:l&&(l.md2=p)})(this); *
* Copyright 2014-2015, emn178@gmail.com
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*/
(function(e,v){var n="undefined"!=typeof module;n&&(e=global);var t="0123456789abcdef".split(""),u=[41,46,67,201,162,216,124,1,61,54,84,161,236,240,6,19,98,167,5,243,192,199,115,140,152,147,43,217,188,76,130,202,30,155,87,60,253,212,224,22,103,66,111,24,138,23,229,18,190,78,196,214,218,158,222,73,160,251,245,142,187,47,238,122,169,104,121,145,21,178,7,63,148,194,16,137,11,34,95,33,128,127,93,154,90,144,50,39,53,62,204,231,191,247,151,3,255,25,48,179,72,165,181,209,215,94,146,42,172,86,170,198,79,
184,56,210,150,164,125,182,118,252,107,226,156,116,4,241,69,157,112,89,100,113,135,32,134,91,207,101,230,45,168,2,27,96,37,173,174,176,185,246,28,70,97,105,52,64,126,15,85,71,163,35,221,81,175,58,195,92,249,206,186,197,234,38,44,83,13,110,133,40,132,9,211,223,205,244,65,129,77,82,106,220,55,200,108,193,171,250,36,225,123,8,12,189,177,74,120,136,149,139,227,99,232,109,233,203,213,254,59,0,29,57,242,239,183,14,102,88,208,228,166,119,114,248,235,117,75,10,49,68,80,180,143,237,31,26,219,153,141,51,159,
17,131,20],c=[],a=[],h=[],m=function(e,n){var d,b,k,f,g=0,p=1,l=0,q=0,r=0,m=e.length;for(b=0;16>b;++b)a[b]=h[b]=0;c[16]=c[17]=c[18]=0;do{c[0]=c[16];c[1]=c[17];c[2]=c[18];c[16]=c[17]=c[18]=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;for(b=q;l<m&&16>b;++l)d=e.charCodeAt(l),128>d?c[b++]=d:(2048>d?c[b++]=192|d>>6:(55296>d||57344<=d?c[b++]=224|d>>12:(d=65536+((d&1023)<<10|e.charCodeAt(++l)&1023),c[b++]=240|d>>18,c[b++]=128|d>>12&63),c[b++]=128|d>>6&63),c[b++]=128|d&63);r+=
b-q;q=b-16;if(l==m&&16>b)for(p=2,f=16-(r&15);16>b;++b)c[b]=f;for(b=0;16>b;++b)h[b]^=u[c[b]^g],g=h[b];for(b=0;b<p;++b)for(d=0===b?c:h,a[16]=d[0],a[32]=a[16]^a[0],a[17]=d[1],a[33]=a[17]^a[1],a[18]=d[2],a[34]=a[18]^a[2],a[19]=d[3],a[35]=a[19]^a[3],a[20]=d[4],a[36]=a[20]^a[4],a[21]=d[5],a[37]=a[21]^a[5],a[22]=d[6],a[38]=a[22]^a[6],a[23]=d[7],a[39]=a[23]^a[7],a[24]=d[8],a[40]=a[24]^a[8],a[25]=d[9],a[41]=a[25]^a[9],a[26]=d[10],a[42]=a[26]^a[10],a[27]=d[11],a[43]=a[27]^a[11],a[28]=d[12],a[44]=a[28]^a[12],
a[29]=d[13],a[45]=a[29]^a[13],a[30]=d[14],a[46]=a[30]^a[14],a[31]=d[15],a[47]=a[31]^a[15],d=f=0;18>d;++d){for(k=0;48>k;++k)a[k]=f=a[k]^u[f];f=f+d&255}}while(1==p);g="";for(b=0;16>b;++b)g+=t[a[b]>>4&15]+t[a[b]&15];return g};!e.JS_MD2_TEST&&n?module.exports=m:e&&(e.md2=m)})(this);

@ -1,6 +1,6 @@
{ {
"name": "js-md2", "name": "js-md2",
"version": "0.1.2", "version": "0.2.0",
"description": "A simple MD2 hash function for JavaScript supports UTF-8 encoding.", "description": "A simple MD2 hash function for JavaScript supports UTF-8 encoding.",
"main": "src/md2.js", "main": "src/md2.js",
"devDependencies": { "devDependencies": {

@ -1,5 +1,5 @@
/* /*
* js-md5 v0.1.2 * js-md5 v0.2.0
* https://github.com/emn178/js-md2 * https://github.com/emn178/js-md2
* *
* Copyright 2014-2015, emn178@gmail.com * Copyright 2014-2015, emn178@gmail.com
@ -10,6 +10,10 @@
;(function(root, undefined) { ;(function(root, undefined) {
'use strict'; 'use strict';
var NODE_JS = typeof(module) != 'undefined';
if(NODE_JS) {
root = global;
}
var HEX_CHARS = '0123456789abcdef'.split(''); var HEX_CHARS = '0123456789abcdef'.split('');
var S = [ 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36, 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13, var S = [ 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36, 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13,
@ -29,112 +33,114 @@
0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58, 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A, 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58, 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A,
0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99, 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14]; 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99, 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14];
var M = [], X = [], C = [];
var md2 = function(message, asciiOnly) { var md2 = function(message, asciiOnly) {
var M, X = [], i, j; var code, i, j, k, t, L = 0, loop = 1, B,
if(!asciiOnly && /[^\x00-\x7F]/.test(message)) { index = 0, start = 0, bytes = 0, length = message.length;
M = getBlocksFromUtf8(message);
} else {
M = getBlocksFromAscii(message);
}
appendChecksum(M);
for(i = 0;i < 48;++i) { for(i = 0;i < 16;++i) {
X[i] = 0; X[i] = C[i] = 0;
} }
var length = M.length >> 4, hex = '';
for(i = 0;i < length;++i) { M[16] = M[17] = M[18] = 0;
for(j = 0;j < 16;++j) { do {
X[16 + j] = M[(i << 4) + j]; M[0] = M[16];
X[32 + j] = X[16 + j] ^ X[j]; M[1] = M[17];
} M[2] = M[18];
var t = 0; M[16] = M[17] = M[18] = M[3] =
for(j = 0;j < 18;++j) { M[4] = M[5] = M[6] = M[7] =
for(var k = 0;k < 48;++k) { M[8] = M[9] = M[10] = M[11] =
X[k] = t = X[k] ^ S[t]; M[12] = M[13] = M[14] = M[15] = 0;
for (i = start;index < length && i < 16; ++index) {
code = message.charCodeAt(index);
if (code < 0x80) {
M[i++] = code;
} else if (code < 0x800) {
M[i++] = 0xc0 | (code >> 6);
M[i++] = 0x80 | (code & 0x3f);
} else if (code < 0xd800 || code >= 0xe000) {
M[i++] = 0xe0 | (code >> 12);
M[i++] = 0x80 | ((code >> 6) & 0x3f);
M[i++] = 0x80 | (code & 0x3f);
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
M[i++] = 0xf0 | (code >> 18);
M[i++] = 0x80 | ((code >> 12) & 0x3f);
M[i++] = 0x80 | ((code >> 6) & 0x3f);
M[i++] = 0x80 | (code & 0x3f);
} }
t = (t + j) & 0xFF;
} }
} bytes += i - start;
start = i - 16;
for(i = 0;i < 16;++i) { if(index == length && i < 16) {
hex += HEX_CHARS[(X[i] >> 4) & 0x0F] + HEX_CHARS[X[i] & 0x0F]; loop = 2;
} t = 16 - (bytes & 15);
return hex; for(;i < 16;++i) {
}; M[i] = t;
}
}
var getBytesFromUtf8 = function(str) { for(i = 0;i < 16;++i) {
var bytes = [], index = 0; C[i] ^= S[M[i] ^ L];
for (var i = 0;i < str.length; i++) { L = C[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 getBlocksFromAscii = function(message) { for(i = 0;i < loop;++i) {
// a block is 8 bits(1 bytes), a chunk is 128 bits(16 bytes) B = i === 0 ? M : C;
var length = message.length;
var chunkCount = (length >> 4) + 1;
var blockCount = chunkCount << 4; // chunkCount * 16
var blocks = [], i;
for(i = 0;i < length;++i) {
blocks[i] = message.charCodeAt(i);
}
var ibit = 16 - (length & 15);
for(;i < blockCount;++i) {
blocks[i] = ibit;
}
return blocks;
};
var getBlocksFromUtf8 = function(message) { X[16] = B[0];
// a block is 8 bits(1 bytes), a chunk is 128 bits(16 bytes) X[32] = X[16] ^ X[0];
var bytes = getBytesFromUtf8(message); X[17] = B[1];
var length = bytes.length; X[33] = X[17] ^ X[1];
var chunkCount = (length >> 4) + 1; X[18] = B[2];
var blockCount = chunkCount << 4; // chunkCount * 16 X[34] = X[18] ^ X[2];
var blocks = [], i; X[19] = B[3];
for(i = 0;i < length;++i) { X[35] = X[19] ^ X[3];
blocks[i] = bytes[i]; X[20] = B[4];
} X[36] = X[20] ^ X[4];
var ibit = 16 - (length & 15); X[21] = B[5];
for(;i < blockCount;++i) { X[37] = X[21] ^ X[5];
blocks[i] = ibit; X[22] = B[6];
} X[38] = X[22] ^ X[6];
return blocks; X[23] = B[7];
}; X[39] = X[23] ^ X[7];
X[24] = B[8];
X[40] = X[24] ^ X[8];
X[25] = B[9];
X[41] = X[25] ^ X[9];
X[26] = B[10];
X[42] = X[26] ^ X[10];
X[27] = B[11];
X[43] = X[27] ^ X[11];
X[28] = B[12];
X[44] = X[28] ^ X[12];
X[29] = B[13];
X[45] = X[29] ^ X[13];
X[30] = B[14];
X[46] = X[30] ^ X[14];
X[31] = B[15];
X[47] = X[31] ^ X[15];
var appendChecksum = function(M) { t = 0;
var checksum = [], L = 0, length = M.length >> 4, i; for(j = 0;j < 18;++j) {
for(i = 0;i < length;++i) { for(k = 0;k < 48;++k) {
for(var j = 0;j < 16;++j) { X[k] = t = X[k] ^ S[t];
var c = M[(i << 4) + j]; }
checksum[j] ^= S[c ^ L]; t = (t + j) & 0xFF;
L = checksum[j]; }
} }
} } while(loop == 1);
length = M.length;
var hex = '';
for(i = 0;i < 16;++i) { for(i = 0;i < 16;++i) {
M[length + i] = checksum[i]; hex += HEX_CHARS[(X[i] >> 4) & 0x0F] + HEX_CHARS[X[i] & 0x0F];
} }
return hex;
}; };
if(typeof(module) != 'undefined') { if(!root.JS_MD2_TEST && NODE_JS) {
module.exports = md2; module.exports = md2;
} else if(root) { } else if(root) {
root.md2 = md2; root.md2 = md2;

@ -1,3 +1,11 @@
md2 = require('../src/md2.js'); md2 = require('../src/md2.js');
expect = require('expect.js'); expect = require('expect.js');
require('./test.js'); require('./test.js');
delete require.cache[require.resolve('../src/md2.js')]
delete require.cache[require.resolve('./test.js')]
md2 = null
JS_MD2_TEST = true;
require('../src/md2.js');
require('./test.js');

@ -1,30 +1,33 @@
describe('ascii', function() { (function(md2) {
describe('less than 64 bytes', function() { describe('ascii', function() {
it('should be successful', function() { describe('less than 64 bytes', function() {
expect(md2('')).to.be('8350e5a3e24c153df2275c9f80692773'); it('should be successful', function() {
expect(md2('The quick brown fox jumps over the lazy dog')).to.be('03d85a0d629d2c442e987525319fc471'); expect(md2('')).to.be('8350e5a3e24c153df2275c9f80692773');
expect(md2('The quick brown fox jumps over the lazy dog.')).to.be('71eaa7e440b611e41a6f0d97384b342a'); expect(md2('The quick brown fox jumps over the lazy dog')).to.be('03d85a0d629d2c442e987525319fc471');
expect(md2('The quick brown fox jumps over the lazy dog.')).to.be('71eaa7e440b611e41a6f0d97384b342a');
});
}); });
});
describe('more than 64 bytes', function() { describe('more than 64 bytes', function() {
it('should be successful', function() { it('should be successful', function() {
expect(md2('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('3658f72434eecc8bdb99047f9710c263'); expect(md2('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('3658f72434eecc8bdb99047f9710c263');
});
}); });
}); });
});
describe('UTF8', function() { describe('UTF8', function() {
describe('less than 64 bytes', function() { describe('less than 64 bytes', function() {
it('should be successful', function() { it('should be successful', function() {
expect(md2('中文')).to.be('7af93c270b0ec392ca2f0d90a927cf8a'); expect(md2('中文')).to.be('7af93c270b0ec392ca2f0d90a927cf8a');
expect(md2('aécio')).to.be('628657f2dbd637b6b13500e8567a1c83'); expect(md2('aécio')).to.be('628657f2dbd637b6b13500e8567a1c83');
expect(md2('𠜎')).to.be('434fc70b04f5ce106b1463f2201223a2');
});
}); });
});
describe('more than 64 bytes', function() { describe('more than 64 bytes', function() {
it('should be successful', function() { it('should be successful', function() {
expect(md2('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。')).to.be('4a0cf02bb374a56e9a9a17e426dee995'); expect(md2('訊息摘要演算法第五版英語Message-Digest Algorithm 5縮寫為MD5是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一又譯雜湊演算法、摘要演算法等主流程式語言普遍已有MD5的實作。')).to.be('4a0cf02bb374a56e9a9a17e426dee995');
});
}); });
}); });
}); })(md2);

Loading…
Cancel
Save