Add hmac{Sha256, 512} (#861)
* Add hmac{Sha256, 512}
* Adjust
* private hmac
This commit is contained in:
+2
-1
@@ -4,7 +4,8 @@
|
||||
|
||||
Changes:
|
||||
|
||||
- Added `hdLedger` to allow for Ledger-compatible bip32+ed25519 derivation
|
||||
- Added `hdLedger` to util-crypto for Ledger-compatible bip32+ed25519 derivation
|
||||
- Added `hmac{Sha256, 512}` to util-crypto
|
||||
|
||||
|
||||
## 5.4.1 Jan 24, 2021
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import BN from 'bn.js';
|
||||
import hash from 'hash.js';
|
||||
|
||||
import { bnToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { hmacSha512 } from '../../hmac';
|
||||
|
||||
// performs hard-only derivation on the xprv
|
||||
export function ledgerDerivePrivate (xprv: Uint8Array, index: number): Uint8Array {
|
||||
const kl = xprv.slice(0, 32);
|
||||
@@ -19,16 +20,11 @@ export function ledgerDerivePrivate (xprv: Uint8Array, index: number): Uint8Arra
|
||||
data.set(kr, 1 + 32);
|
||||
data[0] = 0x00;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
const z = hash.hmac(hash.sha512, cc).update(data).digest();
|
||||
const z = hmacSha512(cc, data);
|
||||
|
||||
data[0] = 0x01;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
const i = hash.hmac(hash.sha512, cc).update(data).digest();
|
||||
const chainCode = i.slice(32, 64);
|
||||
const chainCode = hmacSha512(cc, data).slice(32, 64);
|
||||
const zl = z.slice(0, 32);
|
||||
const zr = z.slice(32, 64);
|
||||
const left = bnToU8a(new BN(kl, 16, 'le').add(new BN(zl.slice(0, 28), 16, 'le').mul(new BN(8))), { bitLength: 512, isLe: true }).slice(0, 32);
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
import { u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { hmacSha256, hmacSha512 } from '../../hmac';
|
||||
import { mnemonicToSeedSync } from '../../mnemonic/bip39';
|
||||
|
||||
const ED25519_CRYPTO = 'ed25519 seed';
|
||||
@@ -15,13 +14,13 @@ export function ledgerMaster (mnemonic: string): Uint8Array {
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
const chainCode = hash.hmac(hash.sha256, ED25519_CRYPTO).update(new Uint8Array([1, ...seed])).digest();
|
||||
const chainCode = hmacSha256(ED25519_CRYPTO, new Uint8Array([1, ...seed]));
|
||||
let priv;
|
||||
|
||||
while (!priv || (priv[31] & 0b0010_0000)) {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
priv = hash.hmac(hash.sha512, ED25519_CRYPTO).update(priv || seed).digest();
|
||||
priv = hmacSha512(ED25519_CRYPTO, priv || seed);
|
||||
}
|
||||
|
||||
priv[0] &= 0b1111_1000;
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { hmacSha256 } from './sha256';
|
||||
export { hmacSha512 } from './sha512';
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
export function hmacSha256 (key: Uint8Array | string, data: Uint8Array): Uint8Array {
|
||||
return Uint8Array.from(
|
||||
hash
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
.hmac(hash.sha256, key)
|
||||
.update(data)
|
||||
.digest());
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
export function hmacSha512 (key: Uint8Array | string, data: Uint8Array): Uint8Array {
|
||||
return Uint8Array.from(
|
||||
hash
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
.hmac(hash.sha512, key)
|
||||
.update(data)
|
||||
.digest());
|
||||
}
|
||||
@@ -13,6 +13,7 @@ export * from './blake2';
|
||||
export * from './crypto';
|
||||
export * from './ethereum';
|
||||
export * from './hd';
|
||||
export * from './hmac';
|
||||
export * from './keccak';
|
||||
export * from './key';
|
||||
export * from './mnemonic';
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
|
||||
// Adapted from https://gist.github.com/calvinmetcalf/91e8e84dc63c75f2aa53
|
||||
|
||||
import hash from 'hash.js';
|
||||
|
||||
import { u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { hmacSha512 } from '../hmac';
|
||||
|
||||
export function pbkdf2Sync (password: Uint8Array, salt: Uint8Array, rounds: number, len = 64): Uint8Array {
|
||||
let out = new Uint8Array();
|
||||
let num = 0;
|
||||
@@ -16,16 +16,12 @@ export function pbkdf2Sync (password: Uint8Array, salt: Uint8Array, rounds: numb
|
||||
num++;
|
||||
block.writeUInt32BE(num, salt.length);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
let prev = hash.hmac(hash.sha512, password).update(block).digest();
|
||||
let prev = hmacSha512(password, block);
|
||||
const md = prev;
|
||||
let i = 0;
|
||||
|
||||
while (++i < rounds) {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
prev = hash.hmac(hash.sha512, password).update(prev).digest();
|
||||
prev = hmacSha512(password, prev);
|
||||
|
||||
let j = -1;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user