mirror of
https://github.com/emn178/js-md5.git
synced 2026-08-12 20:31:43 +08:00
Support node.js
This commit is contained in:
@@ -1,7 +1,18 @@
|
||||
# js-md5
|
||||
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
|
||||
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');
|
||||
|
||||
## Example
|
||||
@@ -28,6 +39,8 @@ Output
|
||||
## Run Tests
|
||||
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
|
||||
### jQuery
|
||||
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:
|
||||
|
||||
'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"
|
||||
}
|
||||
}
|
||||
+10
-3
@@ -1,4 +1,6 @@
|
||||
(function(window, undefined){
|
||||
(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,
|
||||
@@ -28,7 +30,7 @@
|
||||
0X6FA87E4F, 0XFE2CE6E0, 0XA3014314, 0X4E0811A1,
|
||||
0XF7537E82, 0XBD3AF235, 0X2AD7D2BB, 0XEB86D391];
|
||||
|
||||
window.md5 = function(message) {
|
||||
var md5 = function(message) {
|
||||
var blocks = hasUTF8(message) ? UTF8toBlocks(message) : ASCIItoBlocks(message);
|
||||
var h0 = 0x67452301;
|
||||
var h1 = 0xEFCDAB89;
|
||||
@@ -141,4 +143,9 @@
|
||||
blocks[blockCount - 2] = bytes << 3; // bytes * 8
|
||||
return blocks;
|
||||
};
|
||||
}(window));
|
||||
|
||||
if(typeof(module) != 'undefined')
|
||||
module.exports = md5;
|
||||
else if(root)
|
||||
root.md5 = md5;
|
||||
}(this));
|
||||
|
||||
+6
-29
@@ -1,35 +1,12 @@
|
||||
(function(window){
|
||||
//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;
|
||||
};
|
||||
}
|
||||
|
||||
(function(root){
|
||||
var assert = function (title, expect, actual) {
|
||||
if(expect == actual)
|
||||
console.log(title + ': true');
|
||||
else
|
||||
console.log(title + ': false', 'Except:' + expect, 'Actual: ' + actual);
|
||||
};
|
||||
window.assert = assert;
|
||||
})(window);
|
||||
if(typeof(module) != 'undefined')
|
||||
global.assert = assert;
|
||||
else if(root)
|
||||
root.assert = assert;
|
||||
})(this);
|
||||
|
||||
+1
-2
@@ -1,4 +1,3 @@
|
||||
window = global;
|
||||
require('../src/md5.js');
|
||||
global.md5 = require('../src/md5.js');
|
||||
require('./debug.js');
|
||||
require('./test.js');
|
||||
|
||||
Reference in New Issue
Block a user