├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── browser.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "4" 5 | - "5" 6 | - "6" 7 | - "7" 8 | - "8" 9 | - "10" 10 | matrix: 11 | include: 12 | - node_js: "10" 13 | env: TEST_SUITE=standard 14 | env: 15 | - TEST_SUITE=unit 16 | script: npm run-script $TEST_SUITE 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2017 Calvin Metcalf & contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | browserify-cipher 2 | === 3 | 4 | [![Build Status](https://travis-ci.org/crypto-browserify/browserify-cipher.svg)](https://travis-ci.org/crypto-browserify/browserify-cipher) 5 | 6 | Provides createCipher, createDecipher, createCipheriv, createDecipheriv and 7 | getCiphers for the browserify. Includes AES and DES ciphers. 8 | -------------------------------------------------------------------------------- /browser.js: -------------------------------------------------------------------------------- 1 | var DES = require('browserify-des') 2 | var aes = require('browserify-aes/browser') 3 | var aesModes = require('browserify-aes/modes') 4 | var desModes = require('browserify-des/modes') 5 | var ebtk = require('evp_bytestokey') 6 | 7 | function createCipher (suite, password) { 8 | suite = suite.toLowerCase() 9 | 10 | var keyLen, ivLen 11 | if (aesModes[suite]) { 12 | keyLen = aesModes[suite].key 13 | ivLen = aesModes[suite].iv 14 | } else if (desModes[suite]) { 15 | keyLen = desModes[suite].key * 8 16 | ivLen = desModes[suite].iv 17 | } else { 18 | throw new TypeError('invalid suite type') 19 | } 20 | 21 | var keys = ebtk(password, false, keyLen, ivLen) 22 | return createCipheriv(suite, keys.key, keys.iv) 23 | } 24 | 25 | function createDecipher (suite, password) { 26 | suite = suite.toLowerCase() 27 | 28 | var keyLen, ivLen 29 | if (aesModes[suite]) { 30 | keyLen = aesModes[suite].key 31 | ivLen = aesModes[suite].iv 32 | } else if (desModes[suite]) { 33 | keyLen = desModes[suite].key * 8 34 | ivLen = desModes[suite].iv 35 | } else { 36 | throw new TypeError('invalid suite type') 37 | } 38 | 39 | var keys = ebtk(password, false, keyLen, ivLen) 40 | return createDecipheriv(suite, keys.key, keys.iv) 41 | } 42 | 43 | function createCipheriv (suite, key, iv) { 44 | suite = suite.toLowerCase() 45 | if (aesModes[suite]) return aes.createCipheriv(suite, key, iv) 46 | if (desModes[suite]) return new DES({ key: key, iv: iv, mode: suite }) 47 | 48 | throw new TypeError('invalid suite type') 49 | } 50 | 51 | function createDecipheriv (suite, key, iv) { 52 | suite = suite.toLowerCase() 53 | if (aesModes[suite]) return aes.createDecipheriv(suite, key, iv) 54 | if (desModes[suite]) return new DES({ key: key, iv: iv, mode: suite, decrypt: true }) 55 | 56 | throw new TypeError('invalid suite type') 57 | } 58 | 59 | function getCiphers () { 60 | return Object.keys(desModes).concat(aes.getCiphers()) 61 | } 62 | 63 | exports.createCipher = exports.Cipher = createCipher 64 | exports.createCipheriv = exports.Cipheriv = createCipheriv 65 | exports.createDecipher = exports.Decipher = createDecipher 66 | exports.createDecipheriv = exports.Decipheriv = createDecipheriv 67 | exports.listCiphers = exports.getCiphers = getCiphers 68 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var crypto = require('crypto') 2 | 3 | exports.createCipher = exports.Cipher = crypto.createCipher 4 | exports.createCipheriv = exports.Cipheriv = crypto.createCipheriv 5 | exports.createDecipher = exports.Decipher = crypto.createDecipher 6 | exports.createDecipheriv = exports.Decipheriv = crypto.createDecipheriv 7 | exports.listCiphers = exports.getCiphers = crypto.getCiphers 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "browserify-cipher", 3 | "version": "1.0.1", 4 | "description": "ciphers for the browser", 5 | "main": "index.js", 6 | "dependencies": { 7 | "browserify-aes": "^1.0.4", 8 | "browserify-des": "^1.0.0", 9 | "evp_bytestokey": "^1.0.0" 10 | }, 11 | "browser": "browser.js", 12 | "devDependencies": { 13 | "standard": "^10.0.2", 14 | "tap-spec": "^4.1.0", 15 | "tape": "^4.2.0" 16 | }, 17 | "scripts": { 18 | "standard": "standard", 19 | "unit": "node test.js | tspec", 20 | "test": "npm run standard && npm run unit" 21 | }, 22 | "author": "Calvin Metcalf ", 23 | "license": "MIT", 24 | "repository": { 25 | "type": "git", 26 | "url": "git@github.com:crypto-browserify/browserify-cipher.git" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | var crypto = require('crypto') 3 | var desModes = require('browserify-des/modes') 4 | var aesModes = require('browserify-aes/modes') 5 | var ourCrypto = require('./browser') 6 | 7 | function runIvTest (mode, keyLen, ivLen) { 8 | test('mode: ' + mode, function (t) { 9 | var i = 0 10 | while (++i < 10) { 11 | run(i) 12 | } 13 | function run (i) { 14 | t.test('run: ' + i, function (t) { 15 | t.plan(2) 16 | var key = crypto.randomBytes(keyLen) 17 | var iv = crypto.randomBytes(ivLen) 18 | var text = crypto.randomBytes(200) 19 | var ourEncrypt 20 | try { 21 | ourEncrypt = ourCrypto.createCipheriv(mode, key, iv) 22 | } catch (e) { 23 | t.notOk(e, e.stack) 24 | } 25 | var nodeEncrypt 26 | try { 27 | nodeEncrypt = crypto.createCipheriv(mode, key, iv) 28 | } catch (e) { 29 | t.notOk(e, e.stack) 30 | } 31 | var ourCipherText = Buffer.concat([ourEncrypt.update(text), ourEncrypt.final()]) 32 | var authTag 33 | if (mode.slice(-3) === 'gcm') { 34 | authTag = ourEncrypt.getAuthTag() 35 | } 36 | var nodeCipherText = Buffer.concat([nodeEncrypt.update(text), nodeEncrypt.final()]) 37 | t.equals(nodeCipherText.toString('hex'), ourCipherText.toString('hex')) 38 | var ourDecrypt = ourCrypto.createDecipheriv(mode, key, iv) 39 | if (mode.slice(-3) === 'gcm') { 40 | ourDecrypt.setAuthTag(authTag) 41 | } 42 | var plainText = Buffer.concat([ourDecrypt.update(ourCipherText), ourDecrypt.final()]) 43 | t.equals(text.toString('hex'), plainText.toString('hex')) 44 | }) 45 | } 46 | }) 47 | } 48 | Object.keys(aesModes).forEach(function (modeName) { 49 | var mode = aesModes[modeName] 50 | runIvTest(modeName, mode.key / 8, mode.iv) 51 | }) 52 | Object.keys(desModes).forEach(function (modeName) { 53 | var mode = desModes[modeName] 54 | runIvTest(modeName, mode.key, mode.iv) 55 | }) 56 | --------------------------------------------------------------------------------