From 38c2c6f816b6cefcf703bd77e20ed3667b476e87 Mon Sep 17 00:00:00 2001 From: Chen Yi-Cyuan Date: Sat, 4 Jan 2014 16:06:50 +0800 Subject: [PATCH] Support node.js --- README.md | 17 +++++++++++++++++ package.json | 23 +++++++++++++++++++++++ src/md5.js | 13 ++++++++++--- tests/debug.js | 35 ++++++----------------------------- tests/node-test.js | 3 +-- 5 files changed, 57 insertions(+), 34 deletions(-) create mode 100644 package.json diff --git a/README.md b/README.md index 6655043..129288a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json new file mode 100644 index 0000000..9e1540f --- /dev/null +++ b/package.json @@ -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 ", + "homepage": "https://github.com/emn178/js-md5", + "bugs": { + "url": "https://github.com/emn178/js-md5/issues" + } +} diff --git a/src/md5.js b/src/md5.js index 1f1cdd7..8592ffa 100644 --- a/src/md5.js +++ b/src/md5.js @@ -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)); diff --git a/tests/debug.js b/tests/debug.js index a9460b7..ceceea8 100644 --- a/tests/debug.js +++ b/tests/debug.js @@ -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); diff --git a/tests/node-test.js b/tests/node-test.js index ec3c6b4..c1c6447 100644 --- a/tests/node-test.js +++ b/tests/node-test.js @@ -1,4 +1,3 @@ -window = global; -require('../src/md5.js'); +global.md5 = require('../src/md5.js'); require('./debug.js'); require('./test.js');