Skip to content

Commit 5086f43

Browse files
committed
Merge pull request #134 from crypto-browserify/standard
Adds Standard
2 parents 7ac9b42 + d2704b2 commit 5086f43

File tree

15 files changed

+348
-328
lines changed

15 files changed

+348
-328
lines changed

.travis.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
sudo: false
12
language: node_js
23
node_js:
34
- "0.10"
45
- "0.11"
56
- "0.12"
6-
- "iojs"
7+
- "iojs"
8+
env:
9+
- TEST_SUITE=standard
10+
- TEST_SUITE=unit
11+
script: "npm run-script $TEST_SUITE"

readme.markdown renamed to README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
A port of node's `crypto` module to the browser.
44

55
[![travis](https://img.shields.io/travis/crypto-browserify/crypto-browserify/master.svg?style=flat)](https://travis-ci.org/crypto-browserify/crypto-browserify)
6+
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
67

78
The goal of this module is to reimplement node's crypto module,
89
in pure javascript so that it can run in the browser.

example/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var crypto = require('crypto')
22
var abc = crypto.createHash('sha1').update('abc').digest('hex')
33
console.log(abc)
4-
//require('hello').inlineCall().call2()
4+
// require('hello').inlineCall().call2()

index.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
'use strict';
1+
'use strict'
22

33
exports.randomBytes = exports.rng = exports.pseudoRandomBytes = exports.prng = require('randombytes')
4-
54
exports.createHash = exports.Hash = require('create-hash')
6-
75
exports.createHmac = exports.Hmac = require('create-hmac')
86

97
var hashes = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160'].concat(Object.keys(require('browserify-sign/algos')))
108
exports.getHashes = function () {
11-
return hashes;
9+
return hashes
1210
}
1311

1412
var p = require('pbkdf2')
1513
exports.pbkdf2 = p.pbkdf2
1614
exports.pbkdf2Sync = p.pbkdf2Sync
1715

18-
var aes = require('browserify-aes');
19-
[
16+
var aes = require('browserify-aes')
17+
;[
2018
'Cipher',
2119
'createCipher',
2220
'Cipheriv',
@@ -28,41 +26,41 @@ var aes = require('browserify-aes');
2826
'getCiphers',
2927
'listCiphers'
3028
].forEach(function (key) {
31-
exports[key] = aes[key];
29+
exports[key] = aes[key]
3230
})
3331

34-
var dh = require('diffie-hellman');
35-
[
32+
var dh = require('diffie-hellman')
33+
;[
3634
'DiffieHellmanGroup',
3735
'createDiffieHellmanGroup',
3836
'getDiffieHellman',
3937
'createDiffieHellman',
4038
'DiffieHellman'
4139
].forEach(function (key) {
42-
exports[key] = dh[key];
40+
exports[key] = dh[key]
4341
})
4442

45-
var sign = require('browserify-sign');
46-
[
43+
var sign = require('browserify-sign')
44+
;[
4745
'createSign',
4846
'Sign',
4947
'createVerify',
5048
'Verify'
5149
].forEach(function (key) {
52-
exports[key] = sign[key];
50+
exports[key] = sign[key]
5351
})
5452

5553
exports.createECDH = require('create-ecdh')
5654

57-
var publicEncrypt = require('public-encrypt');
55+
var publicEncrypt = require('public-encrypt')
5856

59-
[
57+
;[
6058
'publicEncrypt',
6159
'privateEncrypt',
6260
'publicDecrypt',
6361
'privateDecrypt'
6462
].forEach(function (key) {
65-
exports[key] = publicEncrypt[key];
63+
exports[key] = publicEncrypt[key]
6664
})
6765

6866
// the least I can do is make error messages for the rest of the node.js/crypto api.
@@ -74,6 +72,6 @@ var publicEncrypt = require('public-encrypt');
7472
'sorry, ' + name + ' is not implemented yet',
7573
'we accept pull requests',
7674
'https://github.com/crypto-browserify/crypto-browserify'
77-
].join('\n'));
75+
].join('\n'))
7876
}
7977
})

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
"url": "git://github.com/crypto-browserify/crypto-browserify.git"
1010
},
1111
"scripts": {
12-
"test": "set -e; for t in test/node/*.js test/*.js; do node $t; done",
13-
"prepublish": "npm ls && npm test"
12+
"prepublish": "npm ls && npm test",
13+
"standard": "standard",
14+
"test": "npm run standard && npm run unit",
15+
"unit": "set -e; for t in test/node/*.js test/*.js; do node $t; done"
1416
},
1517
"engines": {
1618
"node": "*"
@@ -28,8 +30,9 @@
2830
"randombytes": "^2.0.0"
2931
},
3032
"devDependencies": {
31-
"tape": "~2.3.2",
32-
"hash-test-vectors": "~1.3.2"
33+
"hash-test-vectors": "~1.3.2",
34+
"standard": "^5.0.2",
35+
"tape": "~2.3.2"
3336
},
3437
"optionalDependencies": {},
3538
"browser": {

test/aes.js

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
1-
var test = require('tape');
2-
var crypto = require('browserify-aes/browser');
3-
var randomBytes = require('randombytes');
1+
var test = require('tape')
2+
var crypto = require('browserify-aes/browser')
3+
var randomBytes = require('randombytes')
4+
45
test('ciphers', function (t) {
56
crypto.listCiphers().forEach(function (cipher) {
67
t.test(cipher, function (t) {
7-
t.plan(1);
8-
var data = randomBytes(562);
9-
var password = randomBytes(20);
10-
var crypter = crypto.createCipher(cipher, password);
11-
var decrypter = crypto.createDecipher(cipher, password);
12-
var out = [];
13-
out.push(decrypter.update(crypter.update(data)));
14-
out.push(decrypter.update(crypter.final()));
8+
t.plan(1)
9+
var data = randomBytes(562)
10+
var password = randomBytes(20)
11+
var crypter = crypto.createCipher(cipher, password)
12+
var decrypter = crypto.createDecipher(cipher, password)
13+
var out = []
14+
out.push(decrypter.update(crypter.update(data)))
15+
out.push(decrypter.update(crypter.final()))
1516
if (cipher.indexOf('gcm') > -1) {
16-
decrypter.setAuthTag(crypter.getAuthTag());
17+
decrypter.setAuthTag(crypter.getAuthTag())
1718
}
18-
out.push(decrypter.final());
19-
t.equals(data.toString('hex'), Buffer.concat(out).toString('hex'));
20-
});
21-
});
22-
});
19+
out.push(decrypter.final())
20+
t.equals(data.toString('hex'), Buffer.concat(out).toString('hex'))
21+
})
22+
})
23+
})
24+
2325
test('getCiphers', function (t) {
24-
t.plan(1);
25-
t.ok(crypto.getCiphers().length, 'get ciphers returns an array');
26-
});
26+
t.plan(1)
27+
t.ok(crypto.getCiphers().length, 'get ciphers returns an array')
28+
})
29+
2730
test('through crypto browserify works', function (t) {
28-
t.plan(2);
29-
var crypto = require('../');
30-
var cipher = 'aes-128-ctr';
31-
var data = randomBytes(562);
32-
var password = randomBytes(20);
33-
var crypter = crypto.createCipher(cipher, password);
34-
var decrypter = crypto.createDecipher(cipher, password);
35-
var out = [];
36-
out.push(decrypter.update(crypter.update(data)));
37-
out.push(decrypter.update(crypter.final()));
38-
out.push(decrypter.final());
39-
t.equals(data.toString('hex'), Buffer.concat(out).toString('hex'));
40-
t.ok(crypto.getCiphers().length, 'get ciphers returns an array');
41-
});
31+
t.plan(2)
32+
var crypto = require('../')
33+
var cipher = 'aes-128-ctr'
34+
var data = randomBytes(562)
35+
var password = randomBytes(20)
36+
var crypter = crypto.createCipher(cipher, password)
37+
var decrypter = crypto.createDecipher(cipher, password)
38+
var out = []
39+
out.push(decrypter.update(crypter.update(data)))
40+
out.push(decrypter.update(crypter.final()))
41+
out.push(decrypter.final())
42+
t.equals(data.toString('hex'), Buffer.concat(out).toString('hex'))
43+
t.ok(crypto.getCiphers().length, 'get ciphers returns an array')
44+
})

test/create-hash.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1-
var fs = require('fs')
21
var test = require('tape')
32

43
var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
5-
var encodings = [/*'binary',*/ 'hex', 'base64'];
4+
var encodings = ['hex', 'base64'] // FIXME: test binary
65
var vectors = require('hash-test-vectors')
7-
testLib('createHash in crypto-browserify',require('../').createHash);
8-
testLib('create-hash/browser',require('create-hash/browser'));
9-
function testLib(name, createHash) {
10-
test(name, function (t){
6+
7+
testLib('createHash in crypto-browserify', require('../').createHash)
8+
testLib('create-hash/browser', require('create-hash/browser'))
9+
10+
function testLib (name, createHash) {
11+
test(name, function (t) {
1112
algorithms.forEach(function (algorithm) {
1213
t.test('test ' + algorithm + ' against test vectors', function (t) {
1314
vectors.forEach(function (obj, i) {
1415
var input = new Buffer(obj.input, 'base64')
1516
var node = obj[algorithm]
1617
var js = createHash(algorithm).update(input).digest('hex')
17-
t.equal(js, node, algorithm + '(testVector['+i+']) == ' + node)
18+
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
1819
})
1920

2021
encodings.forEach(function (encoding) {
21-
vectors.forEach(function (obj, i) {
22-
var input = new Buffer(obj.input, 'base64').toString(encoding)
23-
var node = obj[algorithm]
24-
var js = createHash(algorithm).update(input, encoding).digest('hex')
25-
t.equal(js, node, algorithm + '(testVector['+i+'], '+encoding+') == ' + node)
26-
})
27-
});
22+
vectors.forEach(function (obj, i) {
23+
var input = new Buffer(obj.input, 'base64').toString(encoding)
24+
var node = obj[algorithm]
25+
var js = createHash(algorithm).update(input, encoding).digest('hex')
26+
t.equal(js, node, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + node)
27+
})
28+
})
2829
vectors.forEach(function (obj, i) {
2930
var input = new Buffer(obj.input, 'base64')
3031
var node = obj[algorithm]
31-
var hash = createHash(algorithm);
32+
var hash = createHash(algorithm)
3233
hash.end(input)
3334
var js = hash.read().toString('hex')
34-
t.equal(js, node, algorithm + '(testVector['+i+']) == ' + node)
35+
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
3536
})
3637
t.end()
3738
})
38-
});
39+
})
3940

40-
});
41-
}
41+
})
42+
}

test/create-hmac.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
21
var test = require('tape')
32

43
var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
54
var vectors = require('hash-test-vectors/hmac')
6-
testLib('createHmac in crypto-browserify',require('../').createHmac);
7-
testLib('create-hmac/browser',require('create-hmac/browser'));
8-
function testLib(name, createHmac) {
9-
test(name, function (t){
10-
algorithms.forEach(function (alg) {
5+
testLib('createHmac in crypto-browserify', require('../').createHmac)
6+
testLib('create-hmac/browser', require('create-hmac/browser'))
117

12-
t.test('hmac('+alg+')', function (t) {
8+
function testLib (name, createHmac) {
9+
test(name, function (t) {
10+
algorithms.forEach(function (alg) {
11+
t.test('hmac(' + alg + ')', function (t) {
1312
vectors.forEach(function (input, i) {
1413
var output = createHmac(alg, new Buffer(input.key, 'hex'))
15-
.update(input.data, 'hex').digest()
14+
.update(input.data, 'hex').digest()
1615

1716
output = input.truncate ? output.slice(0, input.truncate) : output
1817
t.equal(output.toString('hex'), input[alg])
1918
})
2019
t.end()
2120
})
2221

23-
t.test('hmac('+alg+')', function (t) {
22+
t.test('hmac(' + alg + ')', function (t) {
2423
vectors.forEach(function (input, i) {
2524
var hmac = createHmac(alg, new Buffer(input.key, 'hex'))
2625

@@ -35,5 +34,4 @@ function testLib(name, createHmac) {
3534

3635
})
3736
})
38-
3937
}

0 commit comments

Comments
 (0)