Skip to content

Commit

Permalink
test: refactor a number of tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Aug 9, 2017
1 parent 7d4d2e6 commit 2cf45c8
Show file tree
Hide file tree
Showing 27 changed files with 1,997 additions and 1,935 deletions.
163 changes: 28 additions & 135 deletions test/aes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,153 +4,46 @@
'use strict';

const assert = require('assert');
const digest = require('../lib/crypto/digest');
const aes = require('../lib/crypto/aes');
const pbkdf2 = require('../lib/crypto/pbkdf2');
const ncrypto = require('crypto');

describe('AES', function() {
function pbkdf2key(passphrase, iterations, dkLen, ivLen, alg) {
const key = pbkdf2.derive(
passphrase, '', iterations, dkLen + ivLen, 'sha512');
return {
key: key.slice(0, dkLen),
iv: key.slice(dkLen, dkLen + ivLen)
};
}
const key = Buffer.from(
'3a0c0bf669694ac7685e6806eeadee8e56c9b9bd22c3caa81c718ed4bbf809a1',
'hex');

function nencrypt(data, passphrase) {
assert(ncrypto, 'No crypto module available.');
assert(passphrase, 'No passphrase.');
const iv = Buffer.from('6dd26d9045b73c377a9ed2ffeca72ffd', 'hex');

if (typeof data === 'string')
data = Buffer.from(data, 'utf8');
it('should encrypt and decrypt with 2 blocks', () => {
const data = Buffer.from(
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
'hex');

if (typeof passphrase === 'string')
passphrase = Buffer.from(passphrase, 'utf8');
const expected = Buffer.from(''
+ '83de502a9c83112ca6383f2214a892a0cdad5ab2b3e192e'
+ '9921ddb126b25262c41f1dcff4d67ccfb40e4116e5a4569c1',
'hex');

const key = pbkdf2key(passphrase, 2048, 32, 16);
const cipher = ncrypto.createCipheriv('aes-256-cbc', key.key, key.iv);
const ciphertext = aes.encipher(data, key, iv);
assert.deepStrictEqual(ciphertext, expected);

return Buffer.concat([
cipher.update(data),
cipher.final()
]);
}

function ndecrypt(data, passphrase) {
assert(ncrypto, 'No crypto module available.');
assert(passphrase, 'No passphrase.');

if (typeof data === 'string')
data = Buffer.from(data, 'hex');

if (typeof passphrase === 'string')
passphrase = Buffer.from(passphrase, 'utf8');

const key = pbkdf2key(passphrase, 2048, 32, 16);
const decipher = ncrypto.createDecipheriv('aes-256-cbc', key.key, key.iv);

return Buffer.concat([
decipher.update(data),
decipher.final()
]);
}

function bencrypt(data, passphrase) {
assert(ncrypto, 'No crypto module available.');
assert(passphrase, 'No passphrase.');

if (typeof data === 'string')
data = Buffer.from(data, 'utf8');

if (typeof passphrase === 'string')
passphrase = Buffer.from(passphrase, 'utf8');

const key = pbkdf2key(passphrase, 2048, 32, 16);
return aes.encipher(data, key.key, key.iv);
}

function bdecrypt(data, passphrase) {
assert(ncrypto, 'No crypto module available.');
assert(passphrase, 'No passphrase.');

if (typeof data === 'string')
data = Buffer.from(data, 'hex');

if (typeof passphrase === 'string')
passphrase = Buffer.from(passphrase, 'utf8');

const key = pbkdf2key(passphrase, 2048, 32, 16);
return aes.decipher(data, key.key, key.iv);
}

function encrypt(data, passphrase) {
assert(ncrypto, 'No crypto module available.');
assert(passphrase, 'No passphrase.');

if (typeof data === 'string')
data = Buffer.from(data, 'utf8');

if (typeof passphrase === 'string')
passphrase = Buffer.from(passphrase, 'utf8');

const key = pbkdf2key(passphrase, 2048, 32, 16);

return aes.encipher(data, key.key, key.iv);
}

function decrypt(data, passphrase) {
assert(ncrypto, 'No crypto module available.');
assert(passphrase, 'No passphrase.');

if (typeof data === 'string')
data = Buffer.from(data, 'hex');

if (typeof passphrase === 'string')
passphrase = Buffer.from(passphrase, 'utf8');

const key = pbkdf2key(passphrase, 2048, 32, 16);

return aes.decipher(data, key.key, key.iv);
}

it('should encrypt and decrypt a hash with 2 blocks', () => {
const hash = digest.sha256(Buffer.alloc(0));
const enchash = encrypt(hash, 'foo');
const dechash = decrypt(enchash, 'foo');

const hash2 = digest.sha256(Buffer.alloc(0));
const enchash2 = nencrypt(hash2, 'foo');
const dechash2 = ndecrypt(enchash2, 'foo');

const hash3 = digest.sha256(Buffer.alloc(0));
const enchash3 = bencrypt(hash3, 'foo');
const dechash3 = bdecrypt(enchash3, 'foo');

assert.deepEqual(hash, hash2);
assert.deepEqual(enchash, enchash2);
assert.deepEqual(dechash, dechash2);
assert.deepEqual(dechash, dechash3);
const plaintext = aes.decipher(ciphertext, key, iv);
assert.deepStrictEqual(plaintext, data);
});

it('should encrypt and decrypt a hash with uneven blocks', () => {
const hash = Buffer.concat([
digest.sha256(Buffer.alloc(0)),
Buffer.from([1,2,3])]);

const enchash = encrypt(hash, 'foo');
const dechash = decrypt(enchash, 'foo');
it('should encrypt and decrypt with uneven blocks', () => {
const data = Buffer.from(
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855010203',
'hex');

const hash2 = Buffer.concat([
digest.sha256(Buffer.alloc(0)),
Buffer.from([1,2,3])]);
const expected = Buffer.from(''
+ '83de502a9c83112ca6383f2214a892a0cdad5ab2b3e192e9'
+ '921ddb126b25262c5211801019a30c0c6f795296923e0af8',
'hex');

const enchash2 = nencrypt(hash2, 'foo');
const dechash2 = ndecrypt(enchash2, 'foo');
const ciphertext = aes.encipher(data, key, iv);
assert.deepStrictEqual(ciphertext, expected);

assert.deepEqual(hash, hash2);
assert.deepEqual(enchash, enchash2);
assert.deepEqual(dechash, dechash2);
const plaintext = aes.decipher(ciphertext, key, iv);
assert.deepStrictEqual(plaintext, data);
});
});
29 changes: 18 additions & 11 deletions test/bech32-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
// THE SOFTWARE.

/* eslint-env mocha */
/* eslint max-len: "off" */
/* eslint prefer-arrow-callback: "off" */

'use strict';
Expand All @@ -33,9 +32,11 @@ const Address = require('../lib/primitives/address');

const validChecksums = [
'A12UEL5L',
'an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs',
'an83characterlonghumanreadablepartthatcontains'
+ 'thenumber1andtheexcludedcharactersbio1tt5tgs',
'abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw',
'11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j',
'11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq'
+ 'qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j',
'split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w'
];

Expand All @@ -57,7 +58,8 @@ const validAddresses = [
])
],
[
'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx',
'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw50'
+ '8d6qejxtdg4y5r3zarvary0c5xw7k7grplx',
Buffer.from([
0x81, 0x28, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54,
0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6,
Expand Down Expand Up @@ -94,7 +96,8 @@ const invalidAddresses = [
'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5',
'BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2',
'bc1rw5uspcuh',
'bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90',
'bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d'
+ '6qejxtdg4y5r3zarvary0c5xw7kw5rljs90',
'BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P',
'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7',
'tb1pw508d6qejxtdg4y5r3zarqfsj6c3',
Expand Down Expand Up @@ -140,7 +143,7 @@ describe('Bech32', function() {
for (const [addr, script] of validAddresses) {
it(`should have valid address for ${addr}`, () => {
let hrp = 'bc';
let ret;
let ret = null;

try {
ret = fromAddress(hrp, addr);
Expand Down Expand Up @@ -175,14 +178,16 @@ describe('Bech32', function() {

for (const addr of invalidAddresses) {
it(`should have invalid address for ${addr}`, () => {
let ok1;
let ok1 = null;

try {
ok1 = fromAddress('bc', addr);
} catch (e) {
ok1 = null;
}

let ok2;
let ok2 = null;

try {
ok2 = fromAddress('tb', addr);
} catch (e) {
Expand All @@ -196,7 +201,7 @@ describe('Bech32', function() {

for (const [addr, script] of validAddresses) {
it(`should have valid address for ${addr}`, () => {
let ret;
let ret = null;

try {
ret = Address.fromBech32(addr, 'main');
Expand Down Expand Up @@ -230,14 +235,16 @@ describe('Bech32', function() {

for (const addr of invalidAddresses) {
it(`should have invalid address for ${addr}`, () => {
let ok1;
let ok1 = null;

try {
ok1 = Address.fromBech32(addr, 'main');
} catch (e) {
ok1 = null;
}

let ok2;
let ok2 = null;

try {
ok2 = Address.fromBech32(addr, 'testnet');
} catch (e) {
Expand Down
Loading

0 comments on commit 2cf45c8

Please sign in to comment.