Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dignified.js #4

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

lib
dist
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: node_js
node_js:
- 4
- 5

# Make sure we have new NPM.
before_install:
- npm install -g npm

script:
- npm run lint
- npm test

addons:
firefox: 'latest'

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
1 change: 0 additions & 1 deletion .zuul.yml

This file was deleted.

31 changes: 18 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
"name": "webcrypto",
"version": "0.1.0",
"description": "Use the Node.js crypto module API without having to worry if it is being run in browser or Node.js",
"main": "src/index.js",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
"scripts": {
"test:node": "node test",
"lint": "./node_modules/.bin/standard",
"test:browser": "./node_modules/.bin/zuul --browser-version $BROWSER_VERSION --browser-name $BROWSER_NAME -- test/index.js",
"test:browser:q": "BROWSER_VERSION=46 BROWSER_NAME=chrome npm run test:browser"
"lint": "dignified-lint",
"test": "dignified-test",
"test:node": "dignified-test node",
"test:browser": "dignified-test browser",
"build": "dignified-build",
"release": "dignified-release"
},
"repository": {
"type": "git",
"url": "git+https://github.com/diasdavid/webcrypto.git"
},
"pre-commit": [
"lint",
"test"
],
"keywords": [
"crypto"
],
Expand All @@ -23,16 +30,14 @@
},
"homepage": "https://github.com/diasdavid/webcrypto#readme",
"dependencies": {
"crypto-browserify": "^3.10.0"
"crypto-browserify": "^3.10.0",
"detect-node": "^2.0.3"
},
"devDependencies": {
"chai": "^3.5.0",
"dignified.js": "^1.0.0",
"hash-test-vectors": "^1.3.2",
"pseudorandombytes": "^2.0.0",
"standard": "^5.3.1",
"tape": "^4.2.2",
"zuul": "^3.7.2"
},
"browser": {
"crypto": false
"pre-commit": "^1.1.2",
"pseudorandombytes": "^2.0.0"
}
}
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var isNode = !global.window
'use strict'

const isNode = require('detect-node')

if (isNode) {
module.exports = require('crypto')
Expand Down
53 changes: 29 additions & 24 deletions test/aes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
var test = require('tape')
var crypto = require('../src')
/* eslint-env mocha */
'use strict'

var expect = require('chai').expect
var randomBytes = require('pseudorandombytes')
var crypto = require('../src')

function runIt (i) {
/*
crypto.listCiphers().forEach(function (cipher) {
test('run: ' + i, function (t) {
t.test('ciphers: ' + cipher, function (t) {
t.plan(1)
describe('run: ' + i, function () {
it('ciphers: ' + cipher, function () {
var data = randomBytes(562)
var password = randomBytes(20)
var crypter = crypto.createCipher(cipher, password)
Expand All @@ -19,7 +21,7 @@ function runIt (i) {
decrypter.setAuthTag(crypter.getAuthTag())
}
out.push(decrypter.final())
t.equals(data.toString('hex'), Buffer.concat(out).toString('hex'))
expect(data.toString('hex')).to.be.eql(, Buffer.concat(out).toString('hex'))
})
})
})
Expand All @@ -29,23 +31,26 @@ function runIt (i) {
*/
}
runIt(1)
test('getCiphers', function (t) {
t.plan(1)
t.ok(crypto.getCiphers().length, 'get ciphers returns an array')
})
describe('aes', function () {
it('getCiphers', function () {
expect(crypto.getCiphers()).to.not.be.empty
})

test('through crypto browserify works', function (t) {
t.plan(2)
var crypto = require('../')
var cipher = 'aes-128-ctr'
var data = randomBytes(562)
var password = randomBytes(20)
var crypter = crypto.createCipher(cipher, password)
var decrypter = crypto.createDecipher(cipher, password)
var out = []
out.push(decrypter.update(crypter.update(data)))
out.push(decrypter.update(crypter.final()))
out.push(decrypter.final())
t.equals(data.toString('hex'), Buffer.concat(out).toString('hex'))
t.ok(crypto.getCiphers().length, 'get ciphers returns an array')
it('through crypto browserify works', function () {
var cipher = 'aes-128-ctr'
var data = randomBytes(562)
var password = randomBytes(20)
var crypter = crypto.createCipher(cipher, password)
var decrypter = crypto.createDecipher(cipher, password)
var out = []
out.push(decrypter.update(crypter.update(data)))
out.push(decrypter.update(crypter.final()))
out.push(decrypter.final())
expect(
data.toString('hex')
).to.be.eql(
Buffer.concat(out).toString('hex')
)
expect(crypto.getCiphers()).to.not.be.empty
})
})
16 changes: 11 additions & 5 deletions test/index.js → test/browser.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
'use strict'

require('./create-hash')
require('./create-hmac')
if (!process.browser) {
require('./dh')
}
require('./dh')

require('./pbkdf2')

var secure = true
try {
require('randombytes')(8)
} catch (e) {
secure = false
console.log('no secure rng available')
}

if (secure) {
require('./ecdh')
require('./public-encrypt')
require('./random-bytes')
require('./sign')
} catch (e) {
console.log('no secure rng avaiable')
}

require('./aes')
79 changes: 39 additions & 40 deletions test/create-hash.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
var test = require('tape')
/* eslint-env mocha */
'use strict'

var expect = require('chai').expect
var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
var encodings = ['hex', 'base64'] // FIXME: test binary
var vectors = require('hash-test-vectors')

testLib('createHash in crypto-browserify', require('../src').createHash)
describe('createHash', function () {
testLib('in crypto-browserify', require('../src').createHash)

function testLib (name, createHash) {
algorithms.forEach(function (algorithm) {
runTest(name, createHash, algorithm)
})
}
function testLib (name, createHash) {
algorithms.forEach(function (algorithm) {
runTest(name, createHash, algorithm)
})
}

function runTest (name, createHash, algorithm) {
test(name + ' test ' + algorithm + ' against test vectors', function (t) {
run(0)
function run (i) {
if (i >= vectors.length) {
return t.end()
}
var obj = vectors[i]

var input = new Buffer(obj.input, 'base64')
var node = obj[algorithm]
var js = createHash(algorithm).update(input).digest('hex')
if (js !== node) {
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
}
function runTest (name, createHash, algorithm) {
it(name + ' test ' + algorithm + ' against test vectors', function (done) {
run(0)
function run (i) {
if (i >= vectors.length) {
return done()
}
var obj = vectors[i]

encodings.forEach(function (encoding) {
var input = new Buffer(obj.input, 'base64').toString(encoding)
var input = new Buffer(obj.input, 'base64')
var node = obj[algorithm]
var js = createHash(algorithm).update(input, encoding).digest('hex')
if (js !== node) {
t.equal(js, node, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + node)
}
})
input = new Buffer(obj.input, 'base64')
node = obj[algorithm]
var hash = createHash(algorithm)
hash.end(input)
js = hash.read().toString('hex')
if (js !== node) {
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node)
var js = createHash(algorithm).update(input).digest('hex')
expect(js).to.be.eql(node)

encodings.forEach(function (encoding) {
var input = new Buffer(obj.input, 'base64').toString(encoding)
var node = obj[algorithm]
var js = createHash(algorithm).update(input, encoding).digest('hex')
expect(js).to.be.eql(node)
})
input = new Buffer(obj.input, 'base64')
node = obj[algorithm]
var hash = createHash(algorithm)
hash.end(input)
js = hash.read().toString('hex')
expect(js).to.be.eql(node)

setTimeout(run, 0, i + 1)
}
setTimeout(run, 0, i + 1)
}
})
}
})
}
})
76 changes: 38 additions & 38 deletions test/create-hmac.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
var test = require('tape')
/* eslint-env mocha */
'use strict'

var expect = require('chai').expect
var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160']
var vectors = require('hash-test-vectors/hmac')

testLib('createHmac in crypto-browserify', require('../src').createHmac)
describe('createHmac', function () {
testLib('in crypto-browserify', require('../src').createHmac)

function testLib (name, createHmac) {
algorithms.forEach(function (alg) {
test(name + ' hmac(' + alg + ')', function (t) {
run(0)
function run (i) {
if (i >= vectors.length) {
return t.end()
}
var input = vectors[i]
var output = createHmac(alg, new Buffer(input.key, 'hex'))
.update(input.data, 'hex').digest()
function testLib (name, createHmac) {
algorithms.forEach(function (alg) {
it(name + ' hmac(' + alg + ')', function (done) {
run(0)
function run (i) {
if (i >= vectors.length) {
return done()
}
var input = vectors[i]
var output = createHmac(alg, new Buffer(input.key, 'hex'))
.update(input.data, 'hex').digest()

output = input.truncate ? output.slice(0, input.truncate) : output
output = output.toString('hex')
if (output !== input[alg]) {
t.equal(output, input[alg])
output = input.truncate ? output.slice(0, input.truncate) : output
output = output.toString('hex')
expect(output).to.be.eql(input[alg])
setTimeout(run, 0, i + 1)
}
setTimeout(run, 0, i + 1)
}
})
})

test('hmac(' + alg + ')', function (t) {
run(0)
function run (i) {
if (i >= vectors.length) {
return t.end()
}
var input = vectors[i]
var hmac = createHmac(alg, new Buffer(input.key, 'hex'))
it('hmac(' + alg + ')', function (done) {
run(0)
function run (i) {
if (i >= vectors.length) {
return done()
}
var input = vectors[i]
var hmac = createHmac(alg, new Buffer(input.key, 'hex'))

hmac.end(input.data, 'hex')
var output = hmac.read()
hmac.end(input.data, 'hex')
var output = hmac.read()

output = input.truncate ? output.slice(0, input.truncate) : output
output = output.toString('hex')
if (output !== input[alg]) {
t.equal(output, input[alg])
output = input.truncate ? output.slice(0, input.truncate) : output
output = output.toString('hex')
expect(output).to.be.eql(input[alg])
setTimeout(run, 0, i + 1)
}
setTimeout(run, 0, i + 1)
}
})
})
})
}
}
})
Loading