Compare commits

..

3 Commits

Author SHA1 Message Date
Chen, Yi-Cyuan e7beb9f012 Fixed `root` is undefined in some special environment. 9 years ago
Chen Yi-Cyuan d125d5a40a Support AMD. 10 years ago
Chen Yi-Cyuan e5c451015b * Remove ascii parameter.
* Improve performance.
* Add test case.
11 years ago

@ -1 +1,2 @@
/node_modules/
/tests/ /tests/

2
.gitignore vendored

@ -0,0 +1,2 @@
/node_modules/
/covreporter/

@ -1,12 +1,12 @@
language: node_js language: node_js
node_js: node_js:
- "0.11" - "0.12.15"
- "0.10" - "4.5"
- "0.8" - "6.5.0"
install: before_install:
- npm install mocha -g - npm install coveralls
- npm install coveralls -g - npm install mocha-lcov-reporter
- npm install mocha-lcov-reporter -g script: npm run-script coveralls
- npm install branches:
script: only:
- npm run-script coveralls - master

@ -1,14 +1,36 @@
# v0.1.2 / 2015-01-07 # Change Log
* Add bower package. ## v0.2.2 / 2017-02-08
* Fixed JSHint warnings. ### Fixed
* Add travis. - `root` is undefined in some special environment.
* Add coveralls.
# v0.1.1 / 2014-07-27 ## v0.2.1 / 2015-04-03
### Added
- support AMD.
Fixed accents bug ## v0.2.0 / 2015-02-28
### Added
- test cases.
# v0.1.0 / 2014-01-05 ### Removed
- ascii parameter.
Initial release ### Improved
- performance.
## v0.1.2 / 2015-01-07
### Added
- bower package.
- travis.
- coveralls.
### Fixed
- JSHint warnings.
## v0.1.1 / 2014-07-27
### Fixed
- accents bug
## v0.1.0 / 2014-01-05
### Added
- initial release.

@ -1,4 +1,4 @@
Copyright 2014-2015 emn178@gmail.com Copyright 2014-2017 Chen, Yi-Cyuan
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the

@ -27,74 +27,28 @@ md2('Message to hash');
``` ```
If you use node.js, you should require the module first: If you use node.js, you should require the module first:
```JavaScript ```JavaScript
md2 = require('js-md2'); var md2 = require('js-md2');
``` ```
If you use require.js, you should require the module first:
### 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 ```JavaScript
md2(''); require(['md2.js'], function (md2) {
md2('The quick brown fox jumps over the lazy dog'); // ...
md2('The quick brown fox jumps over the lazy dog.'); });
``` ```
Output
8350e5a3e24c153df2275c9f80692773
03d85a0d629d2c442e987525319fc471
71eaa7e440b611e41a6f0d97384b342a
It also supports UTF-8 encoding:
Code ## Example
```JavaScript ```JavaScript
md2('中文'); md2(''); // 8350e5a3e24c153df2275c9f80692773
``` md2('The quick brown fox jumps over the lazy dog'); // 03d85a0d629d2c442e987525319fc471
Output md2('The quick brown fox jumps over the lazy dog.'); // 71eaa7e440b611e41a6f0d97384b342a
7af93c270b0ec392ca2f0d90a927cf8a
## Extensions // It also supports UTF-8 encoding
### jQuery md2('中文'); // 7af93c270b0ec392ca2f0d90a927cf8a
If you prefer jQuery style, you can add following code to add a jQuery extension.
Code
```JavaScript
jQuery.md2 = md2
``` ```
And then you could use like this:
```JavaScript
$.md2('message');
```
### Prototype
If you prefer prototype style, you can add following code to add a prototype extension.
Code
```JavaScript
String.prototype.md2 = function() {
return md2(this);
};
```
And then you could use like this:
```JavaScript
'message'.md2();
```
## License ## License
The project is released under the [MIT license](http://www.opensource.org/licenses/MIT). The project is released under the [MIT license](http://www.opensource.org/licenses/MIT).
## Contact ## Contact
The project's website is located at https://github.com/emn178/js-md2 The project's website is located at https://github.com/emn178/js-md2
Author: emn178@gmail.com Author: Chen, Yi-Cyuan (emn178@gmail.com)

@ -1,7 +1,7 @@
{ {
"name": "js-md2", "name": "js-md2",
"version": "0.1.2", "version": "0.2.2",
"main": ["build/md2.min.js"], "main": ["src/md2.js"],
"ignore": [ "ignore": [
"samples", "samples",
"tests" "tests"

14
build/md2.min.js vendored

@ -1,4 +1,10 @@
(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-md2]{@link https://github.com/emn178/js-md2}
/[^\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>> *
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); * @namespace md2
* @version 0.2.2
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2017
* @license MIT
*/
!function(){"use strict";var o="object"==typeof window?window:{},e=!o.JS_MD2_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node;e&&(o=global);var r=!o.JS_MD2_NO_COMMON_JS&&"object"==typeof module&&module.exports,f="function"==typeof define&&define.amd,t="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],d=[],i=[],s=[],c=function(o){var e,r,f,c,p,u,a=0,l=1,_=0,v=0,b=0,m=o.length;for(r=0;16>r;++r)i[r]=s[r]=0;d[16]=d[17]=d[18]=0;do{for(d[0]=d[16],d[1]=d[17],d[2]=d[18],d[16]=d[17]=d[18]=d[3]=d[4]=d[5]=d[6]=d[7]=d[8]=d[9]=d[10]=d[11]=d[12]=d[13]=d[14]=d[15]=0,r=v;m>_&&16>r;++_)e=o.charCodeAt(_),128>e?d[r++]=e:2048>e?(d[r++]=192|e>>6,d[r++]=128|63&e):55296>e||e>=57344?(d[r++]=224|e>>12,d[r++]=128|e>>6&63,d[r++]=128|63&e):(e=65536+((1023&e)<<10|1023&o.charCodeAt(++_)),d[r++]=240|e>>18,d[r++]=128|e>>12&63,d[r++]=128|e>>6&63,d[r++]=128|63&e);if(b+=r-v,v=r-16,_===m&&16>r)for(l=2,p=16-(15&b);16>r;++r)d[r]=p;for(r=0;16>r;++r)s[r]^=n[d[r]^a],a=s[r];for(r=0;l>r;++r)for(u=0===r?d:s,i[16]=u[0],i[32]=i[16]^i[0],i[17]=u[1],i[33]=i[17]^i[1],i[18]=u[2],i[34]=i[18]^i[2],i[19]=u[3],i[35]=i[19]^i[3],i[20]=u[4],i[36]=i[20]^i[4],i[21]=u[5],i[37]=i[21]^i[5],i[22]=u[6],i[38]=i[22]^i[6],i[23]=u[7],i[39]=i[23]^i[7],i[24]=u[8],i[40]=i[24]^i[8],i[25]=u[9],i[41]=i[25]^i[9],i[26]=u[10],i[42]=i[26]^i[10],i[27]=u[11],i[43]=i[27]^i[11],i[28]=u[12],i[44]=i[28]^i[12],i[29]=u[13],i[45]=i[29]^i[13],i[30]=u[14],i[46]=i[30]^i[14],i[31]=u[15],i[47]=i[31]^i[15],p=0,f=0;18>f;++f){for(c=0;48>c;++c)i[c]=p=i[c]^n[p];p=p+f&255}}while(1===l);var w="";for(r=0;16>r;++r)w+=t[i[r]>>4&15]+t[15&i[r]];return w};r?module.exports=c:(o.md2=c,f&&define(function(){return c}))}();

@ -1,15 +1,19 @@
{ {
"name": "js-md2", "name": "js-md2",
"version": "0.1.2", "version": "0.2.2",
"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": {
"expect.js": "~0.3.1", "expect.js": "~0.3.1",
"jscoverage": "~0.5.9" "jscoverage": "~0.5.9",
"mocha": "~2.3.4",
"uglifyjs": "~2.4.10"
}, },
"scripts": { "scripts": {
"test": "mocha tests/node-test.js -r jscoverage", "test": "mocha tests/node-test.js -r jscoverage",
"coveralls": "mocha tests/node-test.js -R mocha-lcov-reporter -r jscoverage | coveralls" "report": "mocha tests/node-test.js -r jscoverage --covout=html",
"coveralls": "mocha tests/node-test.js -R mocha-lcov-reporter -r jscoverage | coveralls",
"build": "uglifyjs src/md2.js --compress --mangle --comments --output build/md2.min.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -23,7 +27,7 @@
"HMAC" "HMAC"
], ],
"license": "MIT", "license": "MIT",
"author": "emn178 <emn178@gmail.com>", "author": "Chen, Yi-Cyuan <emn178@gmail.com>",
"homepage": "https://github.com/emn178/js-md2", "homepage": "https://github.com/emn178/js-md2",
"bugs": { "bugs": {
"url": "https://github.com/emn178/js-md2/issues" "url": "https://github.com/emn178/js-md2/issues"

@ -1,18 +1,27 @@
/* /**
* js-md5 v0.1.2 * [js-md2]{@link https://github.com/emn178/js-md2}
* https://github.com/emn178/js-md2
* *
* Copyright 2014-2015, emn178@gmail.com * @namespace md2
* * @version 0.2.2
* Licensed under the MIT license: * @author Chen, Yi-Cyuan [emn178@gmail.com]
* http://www.opensource.org/licenses/MIT * @copyright Chen, Yi-Cyuan 2014-2017
* @license MIT
*/ */
;(function(root, undefined) { /*jslint bitwise: true */
(function () {
'use strict'; 'use strict';
var root = typeof window === 'object' ? window : {};
var NODE_JS = !root.JS_MD2_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
if (NODE_JS) {
root = global;
}
var COMMON_JS = !root.JS_MD2_NO_COMMON_JS && typeof module === 'object' && module.exports;
var AMD = typeof define === 'function' && define.amd;
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,
0x62, 0xA7, 0x05, 0xF3, 0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C, 0x82, 0xCA, 0x62, 0xA7, 0x05, 0xF3, 0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C, 0x82, 0xCA,
0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16, 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16, 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12,
0xBE, 0x4E, 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E, 0xBB, 0x2F, 0xEE, 0x7A, 0xBE, 0x4E, 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E, 0xBB, 0x2F, 0xEE, 0x7A,
@ -27,116 +36,124 @@
0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA, 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A, 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA, 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A,
0x78, 0x88, 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE, 0x3B, 0x00, 0x1D, 0x39, 0x78, 0x88, 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE, 0x3B, 0x00, 0x1D, 0x39,
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 md2 = function(message, asciiOnly) { var M = [], X = [], C = [];
var M, X = [], i, j;
if(!asciiOnly && /[^\x00-\x7F]/.test(message)) {
M = getBlocksFromUtf8(message);
} else {
M = getBlocksFromAscii(message);
}
appendChecksum(M);
for(i = 0;i < 48;++i) { var md2 = function (message) {
X[i] = 0; var code, i, j, k, t, L = 0, loop = 1, B,
} index = 0, start = 0, bytes = 0, length = message.length;
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];
}
var t = 0;
for(j = 0;j < 18;++j) {
for(var k = 0;k < 48;++k) {
X[k] = t = X[k] ^ S[t];
}
t = (t + j) & 0xFF;
}
}
for(i = 0;i < 16;++i) { for (i = 0; i < 16; ++i) {
hex += HEX_CHARS[(X[i] >> 4) & 0x0F] + HEX_CHARS[X[i] & 0x0F]; X[i] = C[i] = 0;
} }
return hex;
};
var getBytesFromUtf8 = function(str) { M[16] = M[17] = M[18] = 0;
var bytes = [], index = 0; do {
for (var i = 0;i < str.length; i++) { M[0] = M[16];
var c = str.charCodeAt(i); M[1] = M[17];
if (c < 0x80) { M[2] = M[18];
bytes[index++] = c; M[16] = M[17] = M[18] = M[3] =
} else if (c < 0x800) { M[4] = M[5] = M[6] = M[7] =
bytes[index++] = 0xc0 | (c >> 6); M[8] = M[9] = M[10] = M[11] =
bytes[index++] = 0x80 | (c & 0x3f); M[12] = M[13] = M[14] = M[15] = 0;
} else if (c < 0xd800 || c >= 0xe000) { for (i = start; index < length && i < 16; ++index) {
bytes[index++] = 0xe0 | (c >> 12); code = message.charCodeAt(index);
bytes[index++] = 0x80 | ((c >> 6) & 0x3f); if (code < 0x80) {
bytes[index++] = 0x80 | (c & 0x3f); 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 { } else {
c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff)); code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
bytes[index++] = 0xf0 | (c >> 18); M[i++] = 0xf0 | (code >> 18);
bytes[index++] = 0x80 | ((c >> 12) & 0x3f); M[i++] = 0x80 | ((code >> 12) & 0x3f);
bytes[index++] = 0x80 | ((c >> 6) & 0x3f); M[i++] = 0x80 | ((code >> 6) & 0x3f);
bytes[index++] = 0x80 | (c & 0x3f); M[i++] = 0x80 | (code & 0x3f);
} }
} }
return bytes; bytes += i - start;
}; start = i - 16;
var getBlocksFromAscii = function(message) { if (index === length && i < 16) {
// a block is 8 bits(1 bytes), a chunk is 128 bits(16 bytes) loop = 2;
var length = message.length; t = 16 - (bytes & 15);
var chunkCount = (length >> 4) + 1; for (; i < 16; ++i) {
var blockCount = chunkCount << 4; // chunkCount * 16 M[i] = t;
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) { for (i = 0; i < 16; ++i) {
// a block is 8 bits(1 bytes), a chunk is 128 bits(16 bytes) C[i] ^= S[M[i] ^ L];
var bytes = getBytesFromUtf8(message); L = C[i];
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;
};
var appendChecksum = function(M) { for (i = 0; i < loop; ++i) {
var checksum = [], L = 0, length = M.length >> 4, i; B = i === 0 ? M : C;
for(i = 0;i < length;++i) {
for(var j = 0;j < 16;++j) { X[16] = B[0];
var c = M[(i << 4) + j]; X[32] = X[16] ^ X[0];
checksum[j] ^= S[c ^ L]; X[17] = B[1];
L = checksum[j]; 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];
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);
for(i = 0;i < 16;++i) {
M[length + i] = checksum[i]; var hex = '';
for (i = 0; i < 16; ++i) {
hex += HEX_CHARS[(X[i] >> 4) & 0x0F] + HEX_CHARS[X[i] & 0x0F];
} }
return hex;
}; };
if(typeof(module) != 'undefined') { if (COMMON_JS) {
module.exports = md2; module.exports = md2;
} else if(root) { } else {
root.md2 = md2; root.md2 = md2;
if (AMD) {
define(function() {
return md2;
});
}
} }
}(this)); })();

@ -1,3 +1,50 @@
md2 = require('../src/md2.js'); // Node.js env
expect = require('expect.js'); expect = require('expect.js');
md2 = require('../src/md2.js');
require('./test.js');
delete require.cache[require.resolve('../src/md2.js')]
delete require.cache[require.resolve('./test.js')]
md2 = null
// Webpack browser env
JS_MD2_NO_NODE_JS = true;
window = global;
md2 = require('../src/md2.js');
require('./test.js');
delete require.cache[require.resolve('../src/md2.js')]
delete require.cache[require.resolve('./test.js')]
md2 = null
// browser env
JS_MD2_NO_NODE_JS = true;
JS_MD2_NO_COMMON_JS = true;
window = global;
require('../src/md2.js');
require('./test.js'); require('./test.js');
delete require.cache[require.resolve('../src/md2.js')];
delete require.cache[require.resolve('./test.js')];
md2 = null;
// browser AMD
JS_MD2_NO_NODE_JS = true;
JS_MD2_NO_COMMON_JS = true;
window = global;
define = function (func) {
md2 = func();
require('./test.js');
};
define.amd = true;
require('../src/md2.js');
// define = function(func) {
// md2 = func();
// };
// define.amd = true;
// require('../src/md2.js');
// require('./test.js');

@ -0,0 +1,24 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MD2</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.1.0/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect.js/0.2.0/expect.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.17/require.min.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
mocha.setup('bdd');
require(['../src/md2.js'], function (md2) {
window.md2 = md2;
require(['test.js'], function () {
mocha.checkLeaks();
mocha.run();
})
});
</script>
</body>
</html>

@ -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 () {
it('should be successful', function () {
expect(md2('')).to.be('8350e5a3e24c153df2275c9f80692773'); 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('03d85a0d629d2c442e987525319fc471');
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('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