1 import bcrypt from 'bcryptjs';
3 import { arrayToBinaryString, binaryStringToArray, decodeBase64, encodeBase64 } from '@proton/crypto/lib/utils';
5 import { BCRYPT_PREFIX } from './constants';
8 * Compute the key password.
10 export const computeKeyPassword = async (password: string, salt: string) => {
11 if (!password || !salt || salt.length !== 24 || password.length < 1) {
12 throw new Error('Password and salt required.');
14 const saltBinary = binaryStringToArray(decodeBase64(salt));
15 const hash: string = await bcrypt.hash(password, BCRYPT_PREFIX + bcrypt.encodeBase64(saltBinary, 16));
16 // Remove bcrypt prefix and salt (first 29 characters)
17 return hash.slice(29);
21 * Generate salt for a key.
23 export const generateKeySalt = () => encodeBase64(arrayToBinaryString(crypto.getRandomValues(new Uint8Array(16))));