import bCrypt from "../internal/bCrypt.js" ; import { random } from "../random/Randomizer.js" ; import { stringToUtf8Uint8Array } from "@tutao/tutanota-utils" ; import { uint8ArrayToBitArray } from "../misc/Utils.js" ; import { KeyLength } from "../misc/Constants.js" ; import { CryptoError } from "../misc/CryptoError.js" ; import { sha256Hash } from "./Sha256.js" ; import { Aes128Key , Aes256Key } from "../encryption/Aes.js" ; const logRounds = 8 ; export type SignedBytes = number [ ] ; /**
 * Create a 128 bit random _salt value.
 * return _salt 128 bit of random data, encoded as a hex string.
 */ export function generateRandomSalt ( ) { return random . generateRandomData ( 128 / 8 ) ; } param passphrase param salt param keyLengthType return export function generateKeyFromPassphrase ( passphrase , salt , keyLengthType ) { let passphraseBytes = sha256Hash ( stringToUtf8Uint8Array ( passphrase ) ) ; let bytes = crypt_raw ( passphraseBytes , salt , logRounds ) ; if ( keyLengthType === KeyLength . b128 ) { return uint8ArrayToBitArray ( bytes . slice ( 0 , 16 ) ) ; } else { return uint8ArrayToBitArray ( sha256Hash ( bytes ) ) ; } } function crypt_raw ( passphraseBytes , saltBytes , logRounds ) { try { return _signedBytesToUint8Array ( new bCrypt ( ) . crypt_raw ( _uint8ArrayToSignedBytes ( passphraseBytes ) , _uint8ArrayToSignedBytes ( saltBytes ) , logRounds ) ) ; } catch ( e ) { const error = e as Error ; throw new CryptoError ( error . message , error ) ; } } param signedBytes return function _signedBytesToUint8Array ( signedBytes ) { return new Uint8Array ( new Int8Array ( signedBytes ) ) ; } param unsignedBytes return function _uint8ArrayToSignedBytes ( unsignedBytes ) { return Array . from ( new Uint8Array ( new Int8Array ( unsignedBytes ) ) ) ; }