Skip to content

Commit de88d05

Browse files
committed
Merge pull request #16 from legastero/master
Match core crypto API outputs.
2 parents 44f474d + 256178e commit de88d05

File tree

7 files changed

+49
-58
lines changed

7 files changed

+49
-58
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,36 @@ var md5 = require('./md5')
77
var algorithms = {
88
sha1: {
99
hex: sha.hex_sha1,
10-
binary: sha.b64_sha1,
11-
ascii: sha.str_sha1
10+
base64: sha.b64_sha1,
11+
binary: sha.str_sha1
1212
},
1313
sha256: {
1414
hex: sha256.hex_sha256,
15-
binary: sha256.b64_sha256,
16-
ascii: sha256.str_sha256
15+
base64: sha256.b64_sha256,
16+
binary: sha256.str_sha256
1717
},
1818
md5: {
1919
hex: md5.hex_md5,
20-
binary: md5.b64_md5,
21-
ascii: md5.any_md5
20+
base64: md5.b64_md5,
21+
binary: md5.bin_md5
2222
}
2323
}
2424

2525
var algorithmsHmac = {
2626
sha1: {
2727
hex: sha.hex_hmac_sha1,
28-
binary: sha.b64_hmac_sha1,
29-
ascii: sha.str_hmac_sha1
28+
base64: sha.b64_hmac_sha1,
29+
binary: sha.str_hmac_sha1
3030
},
3131
sha256: {
3232
hex: sha256.hex_hmac_sha256,
33-
binary: sha256.b64_hmac_sha256,
34-
ascii: sha256.str_hmac_sha256
33+
base64: sha256.b64_hmac_sha256,
34+
binary: sha256.str_hmac_sha256
3535
},
3636
md5: {
3737
hex: md5.hex_hmac_md5,
38-
binary: md5.b64_hmac_md5,
39-
ascii: md5.any_hmac_md5
38+
base64: md5.b64_hmac_md5,
39+
binary: md5.bin_hmac_md5
4040
}
4141
}
4242

md5.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212
* the server-side, but the defaults work in most cases.
1313
*/
1414
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
15-
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
15+
var b64pad = "="; /* base-64 pad character. "=" for strict RFC compliance */
1616

1717
/*
1818
* These are the functions you'll usually want to call
1919
* They take string arguments and return either hex or base-64 encoded strings
2020
*/
2121
function hex_md5(s) { return rstr2hex(rstr_md5(str2rstr_utf8(s))); }
2222
function b64_md5(s) { return rstr2b64(rstr_md5(str2rstr_utf8(s))); }
23+
function bin_md5(s) { return rstr_md5(str2rstr_utf8(s)); }
2324
function any_md5(s, e) { return rstr2any(rstr_md5(str2rstr_utf8(s)), e); }
2425
function hex_hmac_md5(k, d)
2526
{ return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
2627
function b64_hmac_md5(k, d)
2728
{ return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
29+
function bin_hmac_md5(k, d)
30+
{ return rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)); }
2831
function any_hmac_md5(k, d, e)
2932
{ return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e); }
3033

@@ -381,7 +384,9 @@ function bit_rol(num, cnt)
381384

382385
exports.hex_md5 = hex_md5;
383386
exports.b64_md5 = b64_md5;
387+
exports.bin_md5 = bin_md5;
384388
exports.any_md5 = any_md5;
385389
exports.hex_hmac_md5 = hex_hmac_md5;
386390
exports.b64_hmac_md5 = b64_hmac_md5;
391+
exports.bin_hmac_md5 = bin_hmac_md5;
387392
exports.any_hmac_md5 = any_hmac_md5;

package.json

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,19 @@
1616
},
1717
"dependencies": {},
1818
"devDependencies": {
19-
"tape": "~0.1.5"
19+
"tape": "~1.0.4"
2020
},
2121
"testling": {
2222
"files": "test/*.js",
23-
"browsers": {
24-
"ie": [
25-
8,
26-
9
27-
],
28-
"firefox": [
29-
13
30-
],
31-
"chrome": [
32-
20
33-
],
34-
"safari": [
35-
5.1
36-
],
37-
"opera": [
38-
12
39-
]
40-
}
23+
"browsers": [
24+
"ie/8..latest",
25+
"chrome/20..latest",
26+
"firefox/10..latest",
27+
"safari/latest",
28+
"opera/11.0..latest",
29+
"iphone/6",
30+
"ipad/6"
31+
]
4132
},
4233
"optionalDependencies": {}
4334
}

sha.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ exports.str_hmac_sha1 = str_hmac_sha1;
1818
* Configurable variables. You may need to tweak these to be compatible with
1919
* the server-side, but the defaults work in most cases.
2020
*/
21-
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
22-
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
23-
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
21+
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
22+
var b64pad = "="; /* base-64 pad character. "=" for strict RFC compliance */
23+
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
2424

2525
/*
2626
* These are the functions you'll usually want to call

sha256.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ exports.str_hmac_sha256 = str_hmac_sha256;
1818
* Configurable variables. You may need to tweak these to be compatible with
1919
* the server-side, but the defaults work in most cases.
2020
*/
21-
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
22-
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
23-
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
21+
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
22+
var b64pad = "="; /* base-64 pad character. "=" for strict RFC compliance */
23+
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
2424

2525
/*
2626
* These are the functions you'll usually want to call

test/simple.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ var test = require("tape")
33
var crypto = require('crypto')
44
var cryptoB = require('../')
55

6-
function assertSame (fn) {
7-
test(fn.name, function (t) {
6+
function assertSame(name, fn) {
7+
test(name, function (t) {
88
t.plan(1)
99
fn(crypto, function (err, expected) {
1010
fn(cryptoB, function (err, actual) {
@@ -15,29 +15,23 @@ function assertSame (fn) {
1515
})
1616
}
1717

18-
assertSame(function sha1 (crypto, cb) {
19-
cb(null, crypto.createHash('sha1').update('hello', 'utf-8').digest('hex'))
20-
})
18+
var algorithms = ['sha1', 'sha256', 'md5'];
19+
var encodings = ['binary', 'hex', 'base64'];
2120

22-
assertSame(function md5 (crypto, cb) {
23-
cb(null, crypto.createHash('md5').update('hello', 'utf-8').digest('hex'))
24-
})
2521

26-
assertSame(function sha256 (crypto, cb) {
27-
cb(null, crypto.createHash('sha256').update('hello', 'utf-8').digest('hex'))
28-
})
22+
algorithms.forEach(function (algorithm) {
23+
encodings.forEach(function (encoding) {
2924

30-
assertSame(function sha1hmac (crypto, cb) {
31-
cb(null, crypto.createHmac('sha1', 'secret').update('hello', 'utf-8').digest('hex'))
32-
})
25+
assertSame(algorithm + ' hash using ' + encoding, function (crypto, cb) {
26+
cb(null, crypto.createHash(algorithm).update('hello', 'utf-8').digest(encoding));
27+
})
3328

34-
assertSame(function md5hmac (crypto, cb) {
35-
cb(null, crypto.createHmac('md5', 'secret').update('hello', 'utf-8').digest('hex'))
36-
})
29+
assertSame(algorithm + ' hmac using ' + encoding, function (crypto, cb) {
30+
cb(null, crypto.createHmac(algorithm, 'secret').update('hello', 'utf-8').digest(encoding))
31+
})
3732

38-
assertSame(function sha256hmac (crypto, cb) {
39-
cb(null, crypto.createHmac('sha256', 'secret').update('hello', 'utf-8').digest('hex'))
40-
})
33+
});
34+
});
4135

4236
test('randomBytes', function (t) {
4337
t.plan(5)

0 commit comments

Comments
 (0)