Cleanup hex functions (#130)
This commit is contained in:
+1
-1
@@ -11,6 +11,6 @@
|
||||
"test": "jest --coverage"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/dev": "^0.19.10"
|
||||
"@polkadot/dev": "^0.19.11"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
// @flow
|
||||
|
||||
const blake2 = require('./blake2');
|
||||
const kdf = require('./kdf');
|
||||
const keccak = require('./keccak');
|
||||
const nacl = require('./nacl');
|
||||
const random = require('./random');
|
||||
@@ -15,5 +14,5 @@ const xxhash = require('./xxhash');
|
||||
@summary Utility methods for this package are split into groups
|
||||
*/
|
||||
module.exports = Object.assign(
|
||||
{}, blake2, kdf, keccak, nacl, random, sha512, xxhash
|
||||
{}, blake2, keccak, nacl, random, sha512, xxhash
|
||||
);
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
type KdfU8a = {
|
||||
key: Uint8Array,
|
||||
rounds: number,
|
||||
salt: Uint8Array
|
||||
};
|
||||
|
||||
const isU8a = require('@polkadot/util/is/u8a');
|
||||
const u8aFromUtf8 = require('@polkadot/util/u8a/fromUtf8');
|
||||
|
||||
const blake2AsU8a = require('../blake2/asU8a');
|
||||
const randomAsU8a = require('../random/asU8a');
|
||||
|
||||
/**
|
||||
@name kdfAsU8a
|
||||
@signature kdfAsU8a (secret: Uint8Array | string, rounds?: number, salt?: Uint8Array): { key: Uint8Array, rounds: number, salt: Uint8Array }
|
||||
@summary Create Uint8Array key based on inputs
|
||||
@description
|
||||
From either a `string` or a `Uint8Array` input, create a key with an optional (or random) salt based on the number of rounds with Blake2. Return return the result key as a `Uint8Array` in addition to the salt used and rounds employed.
|
||||
@example
|
||||
import { kdfAsU8a } from '@polkadot/util-crypto';
|
||||
|
||||
kdfAsU8a('123') // => { key: Uint8Array, rounds: number, salt: Uint8Array }
|
||||
*/
|
||||
module.exports = function kdfAsU8a (secret: Uint8Array | string, _rounds: number = 16, salt: Uint8Array = randomAsU8a(32)): KdfU8a {
|
||||
const rounds = Math.max(1, _rounds);
|
||||
let key: Uint8Array = isU8a(secret)
|
||||
// flowlint-next-line unclear-type:off
|
||||
? ((secret: any): Uint8Array)
|
||||
// flowlint-next-line unclear-type:off
|
||||
: u8aFromUtf8(((secret: any): string));
|
||||
|
||||
for (let count = 0; count < rounds; count++) {
|
||||
key = blake2AsU8a(key, 256, salt);
|
||||
}
|
||||
|
||||
return {
|
||||
key,
|
||||
rounds,
|
||||
salt
|
||||
};
|
||||
};
|
||||
@@ -1,33 +0,0 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { kdfAsU8a } = require('./index');
|
||||
|
||||
describe('kdfAsU8a', () => {
|
||||
it('generates known via specified salt & rounds', () => {
|
||||
const kdf = kdfAsU8a('test', 4, new Uint8Array(32));
|
||||
|
||||
expect(
|
||||
kdf
|
||||
).toEqual({
|
||||
key: new Uint8Array([103, 101, 136, 180, 146, 65, 105, 190, 91, 158, 187, 202, 45, 7, 115, 229, 215, 76, 8, 12, 185, 45, 25, 148, 98, 156, 53, 167, 51, 15, 26, 150]),
|
||||
rounds: 4,
|
||||
salt: new Uint8Array(32)
|
||||
});
|
||||
});
|
||||
|
||||
it('defaults with rounds & random salt', () => {
|
||||
const kdf = kdfAsU8a(new Uint8Array(1, 2, 3, 4));
|
||||
|
||||
expect(kdf.key).toHaveLength(32);
|
||||
expect(kdf.rounds).toEqual(16);
|
||||
expect(kdf.salt).not.toEqual(new Uint8Array(32));
|
||||
});
|
||||
|
||||
it('does at least 1 round', () => {
|
||||
const kdf = kdfAsU8a(new Uint8Array(1, 2, 3, 4), -10);
|
||||
|
||||
expect(kdf.rounds).toEqual(1);
|
||||
});
|
||||
});
|
||||
@@ -1,13 +0,0 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const kdfAsU8a = require('./asU8a');
|
||||
|
||||
/**
|
||||
@summary Create Keys for use in encryption (rounds & salt) based on inputs
|
||||
*/
|
||||
module.exports = {
|
||||
kdfAsU8a
|
||||
};
|
||||
@@ -3,6 +3,8 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const assert = require('../assert');
|
||||
const isHex = require('../is/hex');
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
|
||||
/**
|
||||
@@ -23,6 +25,8 @@ module.exports = function hexToU8a (_value?: string, bitLength: number = -1): Ui
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
|
||||
assert(isHex(_value), `Expected hex value to convert, found '${_value}'`);
|
||||
|
||||
const value = hexStripPrefix(_value);
|
||||
const valLength = value.length / 2;
|
||||
const bufLength = Math.ceil(
|
||||
|
||||
@@ -19,19 +19,17 @@ describe('hexToU8a', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array with the correct values (no prefix)', () => {
|
||||
expect(
|
||||
hexToU8a('80000a')
|
||||
).toEqual(
|
||||
new Uint8Array([128, 0, 10])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array with the correct values (bitLengfth)', () => {
|
||||
expect(
|
||||
hexToU8a('80000a', 32)
|
||||
hexToU8a('0x80000a', 32)
|
||||
).toEqual(
|
||||
new Uint8Array([0, 128, 0, 10])
|
||||
);
|
||||
});
|
||||
|
||||
it('fails when non-hex value provided', () => {
|
||||
expect(
|
||||
() => hexToU8a('notahex')
|
||||
).toThrow(/hex value to convert/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -664,9 +664,9 @@
|
||||
lodash "^4.17.5"
|
||||
to-fast-properties "^2.0.0"
|
||||
|
||||
"@polkadot/dev@^0.19.10":
|
||||
version "0.19.10"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.19.10.tgz#ccfbfb8632371876456a8e3e89ff26f10b27ba26"
|
||||
"@polkadot/dev@^0.19.11":
|
||||
version "0.19.11"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.19.11.tgz#fec82f52b92959e2ce166972ee8b2f84d42e7360"
|
||||
dependencies:
|
||||
"@babel/cli" "^7.0.0-beta.47"
|
||||
"@babel/core" "^7.0.0-beta.47"
|
||||
|
||||
Reference in New Issue
Block a user