Skip to content

Commit ad419cc

Browse files
committed
Add failing tests to demonstrate error in md5 hashing
MD5 hashing fails on arbitrary binary data.
1 parent 71bd120 commit ad419cc

File tree

2 files changed

+77
-36
lines changed

2 files changed

+77
-36
lines changed

test/browser.js

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
var test = require('tape')
2-
var Buffer = require('buffer').Buffer
1+
var test = require('tape');
2+
var Buffer = require('buffer').Buffer;
33

4-
var crypto = require('../')
4+
var crypto = require('../');
55

66
var algorithms = ['sha1', 'sha256', 'md5'];
77
var encodings = ['binary', 'hex', 'base64'];
@@ -37,6 +37,15 @@ EXPECTED['md5-hmac-binary'] = atob('ut5jhjxh7QsxZYBuzWrO/A==');
3737
EXPECTED['md5-hmac-hex'] = 'bade63863c61ed0b3165806ecd6acefc';
3838
EXPECTED['md5-hmac-base64'] = 'ut5jhjxh7QsxZYBuzWrO/A==';
3939

40+
EXPECTED['md5-with-binary'] = '27549c8ff29ca52f7957f89c328dbb6d';
41+
EXPECTED['sha1-with-binary'] = '4fa10dda29053b237b5d9703151c852c61e6d8d7';
42+
EXPECTED['sha256-with-binary'] = '424ff84246aabc1560a2881b9664108dfe26784c762d930c4ff396c085f4183b';
43+
44+
EXPECTED['md5-empty-string'] = 'd41d8cd98f00b204e9800998ecf8427e';
45+
EXPECTED['sha1-empty-string'] = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
46+
EXPECTED['sha256-empty-string'] = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
47+
48+
4049
algorithms.forEach(function (algorithm) {
4150
encodings.forEach(function (encoding) {
4251
test(algorithm + ' hash using ' + encoding, function (t) {
@@ -55,16 +64,37 @@ algorithms.forEach(function (algorithm) {
5564
t.end();
5665
});
5766
});
67+
68+
test(algorithm + ' with empty string', function (t) {
69+
t.plan(1);
70+
var actual = crypto.createHash(algorithm).update('', 'utf-8').digest('hex');
71+
var expected = EXPECTED[algorithm + '-empty-string'];
72+
t.equal(actual, expected);
73+
t.end();
74+
});
75+
76+
test(algorithm + ' with raw binary', function (t) {
77+
t.plan(1);
78+
var seed = 'hello';
79+
for (var i = 0; i < 1000; i++) {
80+
seed = crypto.createHash(algorithm).update(seed).digest('binary');
81+
}
82+
var actual = crypto.createHash(algorithm).update(seed).digest('hex');
83+
var expected = EXPECTED[algorithm + '-with-binary'];
84+
t.equal(actual, expected);
85+
t.end();
86+
});
5887
});
5988

89+
6090
test('randomBytes', function (t) {
61-
t.plan(5)
62-
t.equal(crypto.randomBytes(10).length, 10)
63-
t.ok(crypto.randomBytes(10) instanceof Buffer)
64-
crypto.randomBytes(10, function(ex, bytes) {
65-
t.error(ex)
66-
t.equal(bytes.length, 10)
67-
t.ok(bytes instanceof Buffer)
68-
t.end()
69-
})
70-
})
91+
t.plan(5);
92+
t.equal(crypto.randomBytes(10).length, 10);
93+
t.ok(crypto.randomBytes(10) instanceof Buffer);
94+
crypto.randomBytes(10, function(ex, bytes) {
95+
t.error(ex);
96+
t.equal(bytes.length, 10);
97+
t.ok(bytes instanceof Buffer);
98+
t.end();
99+
});
100+
});

test/node.js

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
var test = require("tape")
1+
var test = require('tape');
22

3-
var crypto = require('crypto')
4-
var cryptoB = require('../')
3+
var crypto = require('crypto');
4+
var cryptoB = require('../');
55

66
function assertSame(name, fn) {
7-
test(name, function (t) {
8-
t.plan(1)
9-
fn(crypto, function (err, expected) {
10-
fn(cryptoB, function (err, actual) {
11-
t.equal(actual, expected)
12-
t.end()
13-
})
14-
})
15-
})
7+
test(name, function (t) {
8+
t.plan(1);
9+
fn(crypto, function (err, expected) {
10+
fn(cryptoB, function (err, actual) {
11+
t.equal(actual, expected);
12+
t.end();
13+
});
14+
});
15+
});
1616
}
1717

1818
var algorithms = ['sha1', 'sha256', 'md5'];
@@ -21,26 +21,37 @@ var encodings = ['binary', 'hex', 'base64'];
2121

2222
algorithms.forEach(function (algorithm) {
2323
encodings.forEach(function (encoding) {
24-
2524
assertSame(algorithm + ' hash using ' + encoding, function (crypto, cb) {
2625
cb(null, crypto.createHash(algorithm).update('hello', 'utf-8').digest(encoding));
2726
})
2827

2928
assertSame(algorithm + ' hmac using ' + encoding, function (crypto, cb) {
3029
cb(null, crypto.createHmac(algorithm, 'secret').update('hello', 'utf-8').digest(encoding))
3130
})
31+
});
3232

33+
assertSame(algorithm + ' with raw binary', function (crypto, cb) {
34+
var seed = 'hello';
35+
for (var i = 0; i < 1000; i++) {
36+
seed = crypto.createHash(algorithm).update(seed).digest('binary');
37+
}
38+
cb(null, crypto.createHash(algorithm).update(seed).digest('hex'));
39+
});
40+
41+
assertSame(algorithm + ' empty string', function (crypto, cb) {
42+
cb(null, crypto.createHash(algorithm).update('').digest('hex'));
3343
});
3444
});
3545

46+
3647
test('randomBytes', function (t) {
37-
t.plan(5)
38-
t.equal(cryptoB.randomBytes(10).length, 10)
39-
t.ok(cryptoB.randomBytes(10) instanceof Buffer)
40-
cryptoB.randomBytes(10, function(ex, bytes) {
41-
t.error(ex)
42-
t.equal(bytes.length, 10)
43-
t.ok(bytes instanceof Buffer)
44-
t.end()
45-
})
46-
})
48+
t.plan(5);
49+
t.equal(cryptoB.randomBytes(10).length, 10);
50+
t.ok(cryptoB.randomBytes(10) instanceof Buffer);
51+
cryptoB.randomBytes(10, function(ex, bytes) {
52+
t.error(ex);
53+
t.equal(bytes.length, 10);
54+
t.ok(bytes instanceof Buffer);
55+
t.end();
56+
});
57+
});

0 commit comments

Comments
 (0)