From db1a6adfc5335528422ce3fc77de0b697a2ae7b4 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 25 May 2018 10:27:53 +0200 Subject: [PATCH] Cleanup hex functions (#130) --- package.json | 2 +- packages/util-crypto/src/index.js | 3 +- packages/util-crypto/src/kdf/asU8a.js | 46 ---------------------- packages/util-crypto/src/kdf/asU8a.spec.js | 33 ---------------- packages/util-crypto/src/kdf/index.js | 13 ------ packages/util/src/hex/toU8a.js | 4 ++ packages/util/src/hex/toU8a.spec.js | 16 ++++---- yarn.lock | 6 +-- 8 files changed, 16 insertions(+), 107 deletions(-) delete mode 100644 packages/util-crypto/src/kdf/asU8a.js delete mode 100644 packages/util-crypto/src/kdf/asU8a.spec.js delete mode 100644 packages/util-crypto/src/kdf/index.js diff --git a/package.json b/package.json index a031b7dfe..0fb880333 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,6 @@ "test": "jest --coverage" }, "devDependencies": { - "@polkadot/dev": "^0.19.10" + "@polkadot/dev": "^0.19.11" } } diff --git a/packages/util-crypto/src/index.js b/packages/util-crypto/src/index.js index 06cdf6199..82334939f 100644 --- a/packages/util-crypto/src/index.js +++ b/packages/util-crypto/src/index.js @@ -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 ); diff --git a/packages/util-crypto/src/kdf/asU8a.js b/packages/util-crypto/src/kdf/asU8a.js deleted file mode 100644 index ce25f370d..000000000 --- a/packages/util-crypto/src/kdf/asU8a.js +++ /dev/null @@ -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 - }; -}; diff --git a/packages/util-crypto/src/kdf/asU8a.spec.js b/packages/util-crypto/src/kdf/asU8a.spec.js deleted file mode 100644 index 956818b4b..000000000 --- a/packages/util-crypto/src/kdf/asU8a.spec.js +++ /dev/null @@ -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); - }); -}); diff --git a/packages/util-crypto/src/kdf/index.js b/packages/util-crypto/src/kdf/index.js deleted file mode 100644 index 2f41ab55d..000000000 --- a/packages/util-crypto/src/kdf/index.js +++ /dev/null @@ -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 -}; diff --git a/packages/util/src/hex/toU8a.js b/packages/util/src/hex/toU8a.js index 87da25dec..6279bb095 100644 --- a/packages/util/src/hex/toU8a.js +++ b/packages/util/src/hex/toU8a.js @@ -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( diff --git a/packages/util/src/hex/toU8a.spec.js b/packages/util/src/hex/toU8a.spec.js index 0e9a03844..b69b7e11e 100644 --- a/packages/util/src/hex/toU8a.spec.js +++ b/packages/util/src/hex/toU8a.spec.js @@ -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/); + }); }); diff --git a/yarn.lock b/yarn.lock index c92de1f72..78dabd248 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"