Support node.js

pull/3/head
Chen Yi-Cyuan 12 years ago
parent e7294029a3
commit 38c2c6f816

@ -1,7 +1,18 @@
# js-md5 # js-md5
This is a simple MD5 hash function for JavaScript supports UTF-8 encoding. This is a simple MD5 hash function for JavaScript supports UTF-8 encoding.
## Install
For node.js, you can use this command to install:
npm install js-md5
## Usage ## Usage
If you use node.js, you should require the module first:
var md5 = require('js-md5');
And you could use like this:
md5('Message to hash'); md5('Message to hash');
## Example ## Example
@ -28,6 +39,8 @@ Output
## Run Tests ## Run Tests
You can open `tests/index.html` in browser or use node.js to run `node tests/node-test.js` for test. You can open `tests/index.html` in browser or use node.js to run `node tests/node-test.js` for test.
You also could use `npm test` instance of `node tests/node-test.js`.
## Extensions ## Extensions
### jQuery ### jQuery
If you prefer jQuery style, you can add following code to add a jQuery extension. If you prefer jQuery style, you can add following code to add a jQuery extension.
@ -49,3 +62,7 @@ Code
And then you could use like this: And then you could use like this:
'message'.md5(); 'message'.md5();
## Contact
The project's website is located at https://github.com/emn178/js-md5
Author: emn178@gmail.com

@ -0,0 +1,23 @@
{
"name": "js-md5",
"version": "0.1.0",
"description": "A simple MD5 hash function for JavaScript supports UTF-8 encoding.",
"main": "src/md5.js",
"scripts": {
"test": "node tests/node-test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/emn178/js-md5.git"
},
"keywords": [
"md5",
"hash",
"encryption"
],
"author": "emn178 <emn178@gmail.com>",
"homepage": "https://github.com/emn178/js-md5",
"bugs": {
"url": "https://github.com/emn178/js-md5/issues"
}
}

@ -1,4 +1,6 @@
(function(window, undefined){ (function(root, undefined){
'use strict';
var HEX_CHARS = "0123456789abcdef"; var HEX_CHARS = "0123456789abcdef";
var HEX_TABLE = { var HEX_TABLE = {
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
@ -28,7 +30,7 @@
0X6FA87E4F, 0XFE2CE6E0, 0XA3014314, 0X4E0811A1, 0X6FA87E4F, 0XFE2CE6E0, 0XA3014314, 0X4E0811A1,
0XF7537E82, 0XBD3AF235, 0X2AD7D2BB, 0XEB86D391]; 0XF7537E82, 0XBD3AF235, 0X2AD7D2BB, 0XEB86D391];
window.md5 = function(message) { var md5 = function(message) {
var blocks = hasUTF8(message) ? UTF8toBlocks(message) : ASCIItoBlocks(message); var blocks = hasUTF8(message) ? UTF8toBlocks(message) : ASCIItoBlocks(message);
var h0 = 0x67452301; var h0 = 0x67452301;
var h1 = 0xEFCDAB89; var h1 = 0xEFCDAB89;
@ -141,4 +143,9 @@
blocks[blockCount - 2] = bytes << 3; // bytes * 8 blocks[blockCount - 2] = bytes << 3; // bytes * 8
return blocks; return blocks;
}; };
}(window));
if(typeof(module) != 'undefined')
module.exports = md5;
else if(root)
root.md5 = md5;
}(this));

@ -1,35 +1,12 @@
(function(window){ (function(root){
//console.time implementation for IE
if(window.console && typeof(window.console.time) == "undefined") {
console.time = function(name, reset){
if(!name) { return; }
var time = new Date().getTime();
if(!console.timeCounters) { console.timeCounters = {} };
var key = "KEY" + name.toString();
if(!reset && console.timeCounters[key]) { return; }
console.timeCounters[key] = time;
};
console.timeEnd = function(name){
var time = new Date().getTime();
if(!console.timeCounters) { return; }
var key = "KEY" + name.toString();
var timeCounter = console.timeCounters[key];
if(timeCounter) {
var diff = time - timeCounter;
var label = name + ": " + diff + "ms";
console.info(label);
delete console.timeCounters[key];
}
return diff;
};
}
var assert = function (title, expect, actual) { var assert = function (title, expect, actual) {
if(expect == actual) if(expect == actual)
console.log(title + ': true'); console.log(title + ': true');
else else
console.log(title + ': false', 'Except:' + expect, 'Actual: ' + actual); console.log(title + ': false', 'Except:' + expect, 'Actual: ' + actual);
}; };
window.assert = assert; if(typeof(module) != 'undefined')
})(window); global.assert = assert;
else if(root)
root.assert = assert;
})(this);

@ -1,4 +1,3 @@
window = global; global.md5 = require('../src/md5.js');
require('../src/md5.js');
require('./debug.js'); require('./debug.js');
require('./test.js'); require('./test.js');

Loading…
Cancel
Save