mirror of https://github.com/emn178/js-md2.git
create project
commit
29f0933cb2
@ -0,0 +1,3 @@
|
|||||||
|
# v0.1.0 / 2014-01-05
|
||||||
|
|
||||||
|
Initial release
|
@ -0,0 +1,20 @@
|
|||||||
|
Copyright 2014 emn178@gmail.com
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -0,0 +1,80 @@
|
|||||||
|
# js-md2
|
||||||
|
This is a simple MD2 hash function for JavaScript supports UTF-8 encoding.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
For node.js, you can use this command to install:
|
||||||
|
|
||||||
|
npm install js-md2
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
If you use node.js, you should require the module first:
|
||||||
|
```JavaScript
|
||||||
|
md2 = require('js-md2');
|
||||||
|
```
|
||||||
|
And you could use like this:
|
||||||
|
```JavaScript
|
||||||
|
md2('Message to hash');
|
||||||
|
```
|
||||||
|
## Example
|
||||||
|
Code
|
||||||
|
```JavaScript
|
||||||
|
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
|
||||||
|
```JavaScript
|
||||||
|
md2('中文');
|
||||||
|
```
|
||||||
|
Output
|
||||||
|
|
||||||
|
7af93c270b0ec392ca2f0d90a927cf8a
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
You can open `tests/index.html` in browser or use node.js to run test
|
||||||
|
|
||||||
|
node tests/node-test.js
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
npm test
|
||||||
|
|
||||||
|
## Extensions
|
||||||
|
### jQuery
|
||||||
|
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
|
||||||
|
The project is released under the [MIT license](http://www.opensource.org/licenses/MIT).
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
The project's website is located at https://github.com/emn178/js-md2
|
||||||
|
Author: emn178@gmail.com
|
@ -0,0 +1,4 @@
|
|||||||
|
(function(h,n){var k={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},l=[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],m=function(d){var a;a:{for(a=d.length;a--;)if(255<d.charCodeAt(a)){a=!0;break a}a=!1}if(a){var e=encodeURIComponent(d);d=[];for(var c=a=0,b=e.length;a<b;++a){var f=e.charCodeAt(a);d[c]=37==f?k[e.charAt(++a)]<<4|k[e.charAt(++a)]:f;++c}e=(c>>4)+1<<4;b=16-(c&15);for(a=c;a<e;++a)d[a]=b}else{b=d.length;a=(b>>4)+1<<4;c=[];for(e=0;e<b;++e)c[e]=d.charCodeAt(e);for(d=16-(b&15);e<a;++e)c[e]=d;d=c}a=[];c=0;e=d.length>>4;for(b=0;b<e;++b)for(f=0;16>f;++f)a[f]^=l[d[(b<<4)+f]^
|
||||||
|
c],c=a[f];e=d.length;for(b=0;16>b;++b)d[e+b]=a[b];a=[];for(c=0;48>c;++c)a[c]=0;e=d.length>>4;for(c=0;c<e;++c){for(b=0;16>b;++b)a[16+b]=d[(c<<4)+b],a[32+b]=a[16+b]^a[b];for(b=f=0;18>b;++b){for(var g=0;48>g;++g)a[g]=f=a[g]^l[f];f=f+b&255}}d="";for(c=0;16>c;++c)d+="0123456789abcdef".charAt(a[c]>>4&15)+"0123456789abcdef".charAt(a[c]&15);return d};"undefined"!=typeof module?module.exports=m:h&&(h.md2=m)})(this);
|
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"name": "js-md2",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"description": "A simple MD2 hash function for JavaScript supports UTF-8 encoding.",
|
||||||
|
"main": "src/md2.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "node tests/node-test.js"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/emn178/js-md2.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"md2",
|
||||||
|
"hash",
|
||||||
|
"encryption",
|
||||||
|
"cryptography",
|
||||||
|
"HMAC"
|
||||||
|
],
|
||||||
|
"license": "MIT",
|
||||||
|
"author": "emn178 <emn178@gmail.com>",
|
||||||
|
"homepage": "https://github.com/emn178/js-md2",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/emn178/js-md2/issues"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,133 @@
|
|||||||
|
/*
|
||||||
|
* js-md5 v0.1.0
|
||||||
|
* https://github.com/emn178/js-md2
|
||||||
|
*
|
||||||
|
* Copyright 2014, emn178@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the MIT license:
|
||||||
|
* http://www.opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
;(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 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,
|
||||||
|
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,
|
||||||
|
0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2, 0x07, 0x3F, 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21,
|
||||||
|
0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E, 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03,
|
||||||
|
0xFF, 0x19, 0x30, 0xB3, 0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56, 0xAA, 0xC6,
|
||||||
|
0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6, 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1,
|
||||||
|
0x45, 0x9D, 0x70, 0x59, 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65, 0xE6, 0x2D, 0xA8, 0x02,
|
||||||
|
0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0, 0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F,
|
||||||
|
0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, 0xC3, 0x5C, 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26,
|
||||||
|
0x2C, 0x53, 0x0D, 0x6E, 0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81, 0x4D, 0x52,
|
||||||
|
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,
|
||||||
|
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 md2 = function(message) {
|
||||||
|
var M = hasUTF8(message) ? UTF8toBlocks(message) : ASCIItoBlocks(message);
|
||||||
|
appendChecksum(M);
|
||||||
|
|
||||||
|
var X = [];
|
||||||
|
for(var i = 0;i < 48;++i)
|
||||||
|
X[i] = 0;
|
||||||
|
var length = M.length >> 4;
|
||||||
|
for(var i = 0;i < length;++i)
|
||||||
|
{
|
||||||
|
for(var j = 0;j < 16;++j)
|
||||||
|
{
|
||||||
|
X[16 + j] = M[(i << 4) + j];
|
||||||
|
X[32 + j] = X[16 + j] ^ X[j];
|
||||||
|
}
|
||||||
|
var t = 0;
|
||||||
|
for(var j = 0;j < 18;++j)
|
||||||
|
{
|
||||||
|
for(var k = 0;k < 48;++k)
|
||||||
|
X[k] = t = X[k] ^ S[t];
|
||||||
|
t = (t + j) & 0xFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var hex = '';
|
||||||
|
for(var i = 0;i < 16;++i)
|
||||||
|
hex += HEX_CHARS.charAt((X[i] >> 4) & 0x0F) + HEX_CHARS.charAt(X[i] & 0x0F);
|
||||||
|
return hex;
|
||||||
|
};
|
||||||
|
|
||||||
|
var hasUTF8 = function(message) {
|
||||||
|
for(var i = message.length;i--;)
|
||||||
|
if(message.charCodeAt(i) > 255)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
var ASCIItoBlocks = 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 = [];
|
||||||
|
var 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 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] = (HEX_TABLE[uri.charAt(++i)] << 4) | HEX_TABLE[uri.charAt(++i)];
|
||||||
|
else
|
||||||
|
blocks[bytes] = c;
|
||||||
|
++bytes;
|
||||||
|
}
|
||||||
|
var chunkCount = (bytes >> 4) + 1;
|
||||||
|
var blockCount = chunkCount << 4; // chunkCount * 4
|
||||||
|
var ibit = 16 - (bytes & 15);
|
||||||
|
for(var i = bytes;i < blockCount;++i)
|
||||||
|
blocks[i] = ibit;
|
||||||
|
return blocks;
|
||||||
|
};
|
||||||
|
|
||||||
|
var appendChecksum = function(M) {
|
||||||
|
var checksum = [];
|
||||||
|
var L = 0;
|
||||||
|
var length = M.length >> 4;
|
||||||
|
for(var 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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
length = M.length;
|
||||||
|
for(var i = 0;i < 16;++i)
|
||||||
|
M[length + i] = checksum[i];
|
||||||
|
};
|
||||||
|
|
||||||
|
if(typeof(module) != 'undefined')
|
||||||
|
module.exports = md2;
|
||||||
|
else if(root)
|
||||||
|
root.md2 = md2;
|
||||||
|
}(this));
|
||||||
|
|
@ -0,0 +1,12 @@
|
|||||||
|
(function(root) {
|
||||||
|
var assert = function (title, expect, actual) {
|
||||||
|
if(expect == actual)
|
||||||
|
console.log(title + ': true');
|
||||||
|
else
|
||||||
|
console.log(title + ': false', 'Except:' + expect, 'Actual: ' + actual);
|
||||||
|
};
|
||||||
|
if(typeof(module) != 'undefined')
|
||||||
|
global.assert = assert;
|
||||||
|
else if(root)
|
||||||
|
root.assert = assert;
|
||||||
|
})(this);
|
@ -0,0 +1,12 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>MD2</title>
|
||||||
|
<script src="../src/md2.js"></script>
|
||||||
|
<script src="debug.js"></script>
|
||||||
|
<script src="test.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,3 @@
|
|||||||
|
md2 = require('../src/md2.js');
|
||||||
|
require('./debug.js');
|
||||||
|
require('./test.js');
|
@ -0,0 +1,4 @@
|
|||||||
|
assert('md2 1', '8350e5a3e24c153df2275c9f80692773', md2(''));
|
||||||
|
assert('md2 2', '03d85a0d629d2c442e987525319fc471', md2('The quick brown fox jumps over the lazy dog'));
|
||||||
|
assert('md2 3', '71eaa7e440b611e41a6f0d97384b342a', md2('The quick brown fox jumps over the lazy dog.'));
|
||||||
|
assert('md2 4', '7af93c270b0ec392ca2f0d90a927cf8a', md2('中文'));
|
Loading…
Reference in New Issue