Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed a few strict-mode errors, added test cases

  • Loading branch information...
commit 936ae91983f79e6b4bb6f9f00165cc302d8ea3fb 1 parent e231cef
@coolaj86 authored
Showing with 93 additions and 30 deletions.
  1. +46 −30 md5.js → lib/index.js
  2. +18 −0 lib/package.json
  3. +29 −0 tests/test.js
View
76 md5.js → lib/index.js
@@ -1,3 +1,5 @@
+(function () {
+ "use strict";
/**
* md5.js
* Copyright (c) 2011, Yoshinori Kohyama (http://algobit.jp/)
@@ -5,6 +7,23 @@
*/
exports.digest = function (M) {
+ var originalLength
+ , i
+ , j
+ , k
+ , l
+ , A
+ , B
+ , C
+ , D
+ , AA
+ , BB
+ , CC
+ , DD
+ , X
+ , rval
+ ;
+
function F(x, y, z) { return (x & y) | (~x & z); }
function G(x, y, z) { return (x & z) | (y & ~z); }
function H(x, y, z) { return x ^ y ^ z; }
@@ -14,12 +33,12 @@ exports.digest = function (M) {
return [n&0xff, (n>>>8)&0xff, (n>>>16)&0xff, (n>>>24)&0xff];
}
- var originalLength = M.length; // for Step.2
+ originalLength = M.length; // for Step.2
// 3.1 Step 1. Append Padding Bits
M.push(0x80);
- var l = (56 - M.length)&0x3f;
- for (var i = 0; i < l; i++)
+ l = (56 - M.length)&0x3f;
+ for (i = 0; i < l; i++)
M.push(0);
// 3.2 Step 2. Append Length
@@ -27,11 +46,10 @@ exports.digest = function (M) {
[0, 0, 0, 0].forEach(function (e) { M.push(e); });
// 3.3 Step 3. Initialize MD Buffer
- var A = [0x67452301];
- var B = [0xefcdab89];
- var C = [0x98badcfe];
- var D = [0x10325476];
- var AA, BB, CC, DD;
+ A = [0x67452301];
+ B = [0xefcdab89];
+ C = [0x98badcfe];
+ D = [0x10325476];
// 3.4 Step 4. Process Message in 16-Word Blocks
function rounds(a, b, c, d, k, s, t, f) {
@@ -39,10 +57,10 @@ exports.digest = function (M) {
a[0] = ((a[0]<<s)|(a[0]>>>(32 - s)));
a[0] += b[0];
}
- var X;
- for (var i = 0; i < M.length; i += 64) {
+
+ for (i = 0; i < M.length; i += 64) {
X = [];
- for (var j = 0; j < 64; j += 4) {
+ for (j = 0; j < 64; j += 4) {
k = i + j;
X.push(M[k]|(M[k + 1]<<8)|(M[k + 2]<<16)|(M[k + 3]<<24));
}
@@ -129,39 +147,37 @@ exports.digest = function (M) {
D[0] += DD;
}
- var rval = [];
+ rval = [];
to4bytes(A[0]).forEach(function (e) { rval.push(e); });
to4bytes(B[0]).forEach(function (e) { rval.push(e); });
to4bytes(C[0]).forEach(function (e) { rval.push(e); });
to4bytes(D[0]).forEach(function (e) { rval.push(e); });
+
return rval;
}
exports.digest_s = function (s) {
- var M = [];
- for (var i = 0; i < s.length; i++)
+ var M = []
+ , i
+ , d
+ , rstr
+ , s
+ ;
+
+ for (i = 0; i < s.length; i++)
M.push(s.charCodeAt(i));
- var d = exports.digest(M);
- var rstr = '';
+
+ d = exports.digest(M);
+ rstr = '';
+
d.forEach(function (e) {
- var s = e.toString(16);
+ s = e.toString(16);
while (s.length < 2)
s = '0' + s;
rstr += s;
});
+
return rstr;
}
-// to test
-/*
-function test(s) {
- console.log('MD5 ("' + s + '") = ' + exports.digest_s(s));
-}
-test('');
-test('a');
-test('abc');
-test('message digest');
-test('abcdefghijklmnopqrstuvwxyz');
-test('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
-test('12345678901234567890123456789012345678901234567890123456789012345678901234567890');
-*/
+}());
View
18 lib/package.json
@@ -0,0 +1,18 @@
+{
+ "author": "Yoshinori Kohyama (http://jp.linkedin.com/pub/yoshinori-kohyama/36/155/9b8)",
+ "name": "md5",
+ "description": "Kohyama's jsMD5. See http://jsperf.com/md5-shootout. I'll replace this on npm with anything else.",
+ "keywords": ["ender", "md5", "browser"],
+ "version": "1.0.0",
+ "homepage": "https://github.com/coolaj86/jsMD5",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/coolaj86/jsMD5.git"
+ },
+ "main": "index.js",
+ "engines": {
+ "node": "~v0.4.8"
+ },
+ "dependencies": {},
+ "devDependencies": {}
+}
View
29 tests/test.js
@@ -0,0 +1,29 @@
+(function () {
+ "use strict";
+
+ var assert = require('assert')
+ , crypto = require('crypto')
+ , md5 = require('../lib')
+ ;
+
+ function test(data) {
+ var md5sum = crypto.createHash('md5')
+ ;
+
+ md5sum.update(data);
+ assert.strictEqual(md5sum.digest('hex'), md5.digest_s(data));
+ }
+
+ [
+ ''
+ , 'a'
+ , 'abc'
+ , 'message digest'
+ , 'abcdefghijklmnopqrstuvwxyz'
+ , 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
+ , '12345678901234567890123456789012345678901234567890123456789012345678901234567890'
+ , 'The quick brown fox jumped over the lazy dog.'
+ ].forEach(test);
+
+ console.log('All tests pass');
+}());
Please sign in to comment.
Something went wrong with that request. Please try again.