From e5c451015bb893e001b55b96088da268ca7c6345 Mon Sep 17 00:00:00 2001 From: Chen Yi-Cyuan Date: Sat, 28 Feb 2015 15:16:53 +0800 Subject: [PATCH] * Remove ascii parameter. * Improve performance. * Add test case. --- .travis.yml | 9 ++- CHANGELOG.md | 6 ++ README.md | 14 ---- bower.json | 2 +- build/md2.min.js | 18 ++++- package.json | 2 +- src/md2.js | 192 +++++++++++++++++++++++---------------------- tests/node-test.js | 8 ++ tests/test.js | 45 ++++++----- 9 files changed, 158 insertions(+), 138 deletions(-) diff --git a/.travis.yml b/.travis.yml index f5cd45c..aabe614 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,10 +3,11 @@ node_js: - "0.11" - "0.10" - "0.8" -install: +before_install: - npm install mocha -g - npm install coveralls -g - npm install mocha-lcov-reporter -g - - npm install -script: - - npm run-script coveralls +script: npm run-script coveralls +branches: + only: + - master diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a37e77..a26e2e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v0.2.0 / 2015-02-28 + +* Remove ascii parameter. +* Improve performance. +* Add test case. + # v0.1.2 / 2015-01-07 * Add bower package. diff --git a/README.md b/README.md index 42cd95b..82861b2 100644 --- a/README.md +++ b/README.md @@ -30,20 +30,6 @@ If you use node.js, you should require the module first: 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 Code ```JavaScript diff --git a/bower.json b/bower.json index 5e5e044..b7693e0 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "js-md2", - "version": "0.1.2", + "version": "0.2.0", "main": ["build/md2.min.js"], "ignore": [ "samples", diff --git a/build/md2.min.js b/build/md2.min.js index a7f97a3..7293498 100644 --- a/build/md2.min.js +++ b/build/md2.min.js @@ -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&& -/[^\x00-\x7F]/.test(k)){d=[];for(a=b=0;ac?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>4)+1<<4;b=[];for(a=0;a> -4,e;for(e=0;eg;++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;ba;++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); +/* + * js-md5 v0.2.0 + * https://github.com/emn178/js-md2 + * + * 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;lb;++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;bd;++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); diff --git a/package.json b/package.json index 4a1fc31..71f7633 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-md2", - "version": "0.1.2", + "version": "0.2.0", "description": "A simple MD2 hash function for JavaScript supports UTF-8 encoding.", "main": "src/md2.js", "devDependencies": { diff --git a/src/md2.js b/src/md2.js index 42b9e0e..1abd29d 100644 --- a/src/md2.js +++ b/src/md2.js @@ -1,5 +1,5 @@ /* - * js-md5 v0.1.2 + * js-md5 v0.2.0 * https://github.com/emn178/js-md2 * * Copyright 2014-2015, emn178@gmail.com @@ -10,6 +10,10 @@ ;(function(root, undefined) { 'use strict'; + var NODE_JS = typeof(module) != 'undefined'; + if(NODE_JS) { + root = global; + } var HEX_CHARS = '0123456789abcdef'.split(''); 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, 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 M, X = [], i, j; - if(!asciiOnly && /[^\x00-\x7F]/.test(message)) { - M = getBlocksFromUtf8(message); - } else { - M = getBlocksFromAscii(message); - } - appendChecksum(M); + var code, i, j, k, t, L = 0, loop = 1, B, + index = 0, start = 0, bytes = 0, length = message.length; - for(i = 0;i < 48;++i) { - X[i] = 0; + for(i = 0;i < 16;++i) { + X[i] = C[i] = 0; } - var length = M.length >> 4, hex = ''; - for(i = 0;i < length;++i) { - for(j = 0;j < 16;++j) { - X[16 + j] = M[(i << 4) + j]; - X[32 + j] = X[16 + j] ^ X[j]; + + M[16] = M[17] = M[18] = 0; + do { + M[0] = M[16]; + M[1] = M[17]; + M[2] = M[18]; + M[16] = M[17] = M[18] = M[3] = + M[4] = M[5] = M[6] = M[7] = + M[8] = M[9] = M[10] = M[11] = + 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); + } } - var t = 0; - for(j = 0;j < 18;++j) { - for(var k = 0;k < 48;++k) { - X[k] = t = X[k] ^ S[t]; + bytes += i - start; + start = i - 16; + + if(index == length && i < 16) { + loop = 2; + t = 16 - (bytes & 15); + for(;i < 16;++i) { + M[i] = t; } - t = (t + j) & 0xFF; } - } - for(i = 0;i < 16;++i) { - hex += HEX_CHARS[(X[i] >> 4) & 0x0F] + HEX_CHARS[X[i] & 0x0F]; - } - return hex; - }; - - 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); + for(i = 0;i < 16;++i) { + C[i] ^= S[M[i] ^ L]; + L = C[i]; } - } - return bytes; - }; - var getBlocksFromAscii = function(message) { - // a block is 8 bits(1 bytes), a chunk is 128 bits(16 bytes) - 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) { - // a block is 8 bits(1 bytes), a chunk is 128 bits(16 bytes) - var bytes = getBytesFromUtf8(message); - var length = bytes.length; - var chunkCount = (length >> 4) + 1; - var blockCount = chunkCount << 4; // chunkCount * 16 - var blocks = [], i; - for(i = 0;i < length;++i) { - blocks[i] = bytes[i]; - } - var ibit = 16 - (length & 15); - for(;i < blockCount;++i) { - blocks[i] = ibit; - } - return blocks; - }; + for(i = 0;i < loop;++i) { + B = i === 0 ? M : C; + + X[16] = B[0]; + X[32] = X[16] ^ X[0]; + X[17] = B[1]; + X[33] = X[17] ^ X[1]; + X[18] = B[2]; + X[34] = X[18] ^ X[2]; + X[19] = B[3]; + X[35] = X[19] ^ X[3]; + X[20] = B[4]; + X[36] = X[20] ^ X[4]; + X[21] = B[5]; + X[37] = X[21] ^ X[5]; + X[22] = B[6]; + X[38] = X[22] ^ X[6]; + 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) { - var checksum = [], L = 0, length = M.length >> 4, i; - for(i = 0;i < length;++i) { - for(var j = 0;j < 16;++j) { - var c = M[(i << 4) + j]; - checksum[j] ^= S[c ^ L]; - L = checksum[j]; + t = 0; + for(j = 0;j < 18;++j) { + for(k = 0;k < 48;++k) { + X[k] = t = X[k] ^ S[t]; + } + t = (t + j) & 0xFF; + } } - } - length = M.length; + } while(loop == 1); + + var hex = ''; 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; } else if(root) { root.md2 = md2; diff --git a/tests/node-test.js b/tests/node-test.js index 935baa7..907e291 100644 --- a/tests/node-test.js +++ b/tests/node-test.js @@ -1,3 +1,11 @@ md2 = require('../src/md2.js'); expect = require('expect.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'); diff --git a/tests/test.js b/tests/test.js index 9d2a949..fbe2fbb 100644 --- a/tests/test.js +++ b/tests/test.js @@ -1,30 +1,33 @@ -describe('ascii', function() { - describe('less than 64 bytes', function() { - it('should be successful', function() { - expect(md2('')).to.be('8350e5a3e24c153df2275c9f80692773'); - 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'); +(function(md2) { + describe('ascii', function() { + describe('less than 64 bytes', function() { + it('should be successful', function() { + expect(md2('')).to.be('8350e5a3e24c153df2275c9f80692773'); + 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() { - 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'); + describe('more than 64 bytes', 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'); + }); }); }); -}); -describe('UTF8', function() { - describe('less than 64 bytes', function() { - it('should be successful', function() { - expect(md2('中文')).to.be('7af93c270b0ec392ca2f0d90a927cf8a'); - expect(md2('aécio')).to.be('628657f2dbd637b6b13500e8567a1c83'); + describe('UTF8', function() { + describe('less than 64 bytes', function() { + it('should be successful', function() { + expect(md2('中文')).to.be('7af93c270b0ec392ca2f0d90a927cf8a'); + expect(md2('aécio')).to.be('628657f2dbd637b6b13500e8567a1c83'); + expect(md2('𠜎')).to.be('434fc70b04f5ce106b1463f2201223a2'); + }); }); - }); - describe('more than 64 bytes', function() { - it('should be successful', function() { - expect(md2('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('4a0cf02bb374a56e9a9a17e426dee995'); + describe('more than 64 bytes', function() { + it('should be successful', function() { + expect(md2('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('4a0cf02bb374a56e9a9a17e426dee995'); + }); }); }); -}); +})(md2);