use hash-wasm

This commit is contained in:
fyears 2021-11-03 00:57:57 +08:00
parent 6431182ec3
commit bcf8586fbf
2 changed files with 19 additions and 15 deletions

View File

@ -38,6 +38,7 @@
"crypto-browserify": "^3.12.0",
"domain-browser": "^4.22.0",
"events": "^3.3.0",
"hash-wasm": "^4.9.0",
"hi-base32": "^0.5.1",
"https-browserify": "^1.0.0",
"lovefield-ts": "^0.7.0",

View File

@ -1,4 +1,5 @@
import { randomBytes, pbkdf2, createDecipheriv, createCipheriv } from "crypto";
import { randomBytes, createDecipheriv, createCipheriv } from "crypto";
import { pbkdf2, createSHA256 } from "hash-wasm";
import { promisify } from "util";
import * as base32 from "hi-base32";
import { bufferToArrayBuffer, arrayBufferToBuffer } from "./misc";
@ -11,13 +12,14 @@ export const encryptBuffer = async (
rounds: number = DEFAULT_ITER
) => {
const salt = await promisify(randomBytes)(8);
const derivedKey = await promisify(pbkdf2)(
password,
salt,
rounds,
32 + 16,
"sha256"
);
const derivedKey = await pbkdf2({
password: password,
salt: salt,
iterations: rounds,
hashLength: 32 + 16,
hashFunction: createSHA256(),
outputType: "binary",
});
const key = derivedKey.slice(0, 32);
const iv = derivedKey.slice(32, 32 + 16);
const cipher = createCipheriv("aes-256-cbc", key, iv);
@ -35,13 +37,14 @@ export const decryptBuffer = async (
) => {
const prefix = buf.slice(0, 8);
const salt = buf.slice(8, 16);
const derivedKey = await promisify(pbkdf2)(
password,
salt,
rounds,
32 + 16,
"sha256"
);
const derivedKey = await pbkdf2({
password: password,
salt: salt,
iterations: rounds,
hashLength: 32 + 16,
hashFunction: createSHA256(),
outputType: "binary",
});
const key = derivedKey.slice(0, 32);
const iv = derivedKey.slice(32, 32 + 16);
const decipher = createDecipheriv("aes-256-cbc", key, iv);