Replace pbkdf2 (#758)

* Replace pbkdf2

* Optional length

* cleanups
This commit is contained in:
Jaco Greeff
2020-11-08 21:34:34 +01:00
committed by GitHub
parent 9b204fc660
commit 82b25aecfb
6 changed files with 50 additions and 23 deletions
+1 -2
View File
@@ -35,10 +35,10 @@
"bn.js": "^5.1.3",
"create-hash": "^1.2.0",
"elliptic": "^6.5.3",
"hash.js": "^1.1.7",
"js-sha3": "^0.8.0",
"pbkdf2": "^3.1.1",
"scryptsy": "^2.1.0",
"sha.js": "^2.4.11",
"tweetnacl": "^1.0.3",
"xxhashjs": "^0.2.2"
},
@@ -48,7 +48,6 @@
"@types/elliptic": "^6.4.12",
"@types/pbkdf2": "^3.1.0",
"@types/scryptsy": "^2.0.0",
"@types/sha.js": "^2.4.0",
"@types/xxhashjs": "^0.2.2"
},
"peerDependencies": {
+2 -4
View File
@@ -13,7 +13,7 @@
// - Remove setting of wordlost passing of wordlist in functions
// - Remove mnemonicToSeed (we only use the sync variant)
import createSha from 'sha.js';
import { sha256 } from 'hash.js';
import { assert, u8aToU8a } from '@polkadot/util';
import { pbkdf2Encode } from '../pbkdf2';
@@ -39,9 +39,7 @@ function bytesToBinary (bytes: number[]): string {
function deriveChecksumBits (entropyBuffer: Uint8Array): string {
const ENT = entropyBuffer.length * 8;
const CS = ENT / 32;
const hash = createSha('sha256')
.update(entropyBuffer)
.digest();
const hash = sha256().update(entropyBuffer).digest();
return bytesToBinary(Array.from(hash)).slice(0, CS);
}
+3 -3
View File
@@ -1,11 +1,11 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { pbkdf2Sync } from 'pbkdf2';
import { bufferToU8a, u8aToBuffer, u8aToU8a } from '@polkadot/util';
import { u8aToU8a } from '@polkadot/util';
import { isReady, pbkdf2 } from '@polkadot/wasm-crypto';
import { randomAsU8a } from '../random/asU8a';
import { pbkdf2Sync } from './pbkdf2';
interface Result {
password: Uint8Array;
@@ -18,7 +18,7 @@ export function pbkdf2Encode (passphrase?: Buffer | Uint8Array | string, salt: B
const u8aSalt = u8aToU8a(salt);
const password = isReady() && !onlyJs
? pbkdf2(u8aPass, u8aSalt, rounds)
: bufferToU8a(pbkdf2Sync(u8aToBuffer(u8aPass), u8aToBuffer(u8aSalt), rounds, 64, 'sha512'));
: pbkdf2Sync(u8aPass, u8aSalt, rounds);
return { password, rounds, salt };
}
+40
View File
@@ -0,0 +1,40 @@
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Adapted from https://gist.github.com/calvinmetcalf/91e8e84dc63c75f2aa53
import { hmac, sha512 } from 'hash.js';
import { u8aConcat } from '@polkadot/util';
export function pbkdf2Sync (password: Uint8Array, salt: Uint8Array, rounds: number, len = 64): Uint8Array {
let out = new Uint8Array();
let num = 0;
const block = Buffer.concat([salt, Buffer.from([0, 0, 0, 0])]);
while (out.length < len) {
num++;
block.writeUInt32BE(num, salt.length);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
let prev = hmac(sha512, password).update(block).digest();
const md = prev;
let i = 0;
while (++i < rounds) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
prev = hmac(sha512, password).update(prev).digest();
let j = -1;
while (++j < prev.length) {
md[j] ^= prev[j];
}
}
out = u8aConcat(out, md);
}
return out.slice(0, len);
}
+1 -1
View File
@@ -20,7 +20,7 @@ import { u8aToU8a } from './toU8a';
* ); // [1, 2, 3, 4, 5, 6]
* ```
*/
export function u8aConcat (...list: (Uint8Array | string)[]): Uint8Array {
export function u8aConcat (...list: (Buffer | Uint8Array | number[] | string)[]): Uint8Array {
let length = 0;
let offset = 0;
const u8as = new Array(list.length) as Uint8Array[];
+3 -13
View File
@@ -1871,17 +1871,16 @@ __metadata:
"@types/elliptic": ^6.4.12
"@types/pbkdf2": ^3.1.0
"@types/scryptsy": ^2.0.0
"@types/sha.js": ^2.4.0
"@types/xxhashjs": ^0.2.2
base-x: ^3.0.8
blakejs: ^1.1.0
bn.js: ^5.1.3
create-hash: ^1.2.0
elliptic: ^6.5.3
hash.js: ^1.1.7
js-sha3: ^0.8.0
pbkdf2: ^3.1.1
scryptsy: ^2.1.0
sha.js: ^2.4.11
tweetnacl: ^1.0.3
xxhashjs: ^0.2.2
peerDependencies:
@@ -2251,15 +2250,6 @@ __metadata:
languageName: node
linkType: hard
"@types/sha.js@npm:^2.4.0":
version: 2.4.0
resolution: "@types/sha.js@npm:2.4.0"
dependencies:
"@types/node": "*"
checksum: d23139fb0c9ad65c884b057728b19944399e167fc2a05a22931760d3541c861af7c3c274de11ecc26d3e340f38c249e6d3b0359d249fb674bb702c3232c2d53f
languageName: node
linkType: hard
"@types/stack-utils@npm:^2.0.0":
version: 2.0.0
resolution: "@types/stack-utils@npm:2.0.0"
@@ -5662,7 +5652,7 @@ fsevents@^1.2.7:
languageName: node
linkType: hard
"hash.js@npm:^1.0.0, hash.js@npm:^1.0.3":
"hash.js@npm:^1.0.0, hash.js@npm:^1.0.3, hash.js@npm:^1.1.7":
version: 1.1.7
resolution: "hash.js@npm:1.1.7"
dependencies:
@@ -9277,7 +9267,7 @@ fsevents@^1.2.7:
languageName: node
linkType: hard
"sha.js@npm:^2.4.0, sha.js@npm:^2.4.11, sha.js@npm:^2.4.8":
"sha.js@npm:^2.4.0, sha.js@npm:^2.4.8":
version: 2.4.11
resolution: "sha.js@npm:2.4.11"
dependencies: