import type { Aes128Key , Aes256Key } from "./Aes.js" ; import { aes128Decrypt , aes128Encrypt , aes256Decrypt , aes256Encrypt , IV_BYTE_LENGTH } from "./Aes.js" ; import { bitArrayToUint8Array , fixedIv , uint8ArrayToBitArray } from "../misc/Utils.js" ; import { concat , hexToUint8Array , uint8ArrayToHex } from "@tutao/tutanota-utils" ; import { hexToPrivateKey , privateKeyToHex } from "./Rsa.js" ; import { random } from "../random/Randomizer.js" ; import type { PrivateKey } from "./RsaKeyPair.js" ; export function encryptKey ( encryptionKey , key ) { return aes128Encrypt ( encryptionKey , bitArrayToUint8Array ( key ) , fixedIv , false , false ) . slice ( fixedIv . length ) ; } export function decryptKey ( encryptionKey , key ) { return uint8ArrayToBitArray ( aes128Decrypt ( encryptionKey , concat ( fixedIv , key ) , false ) ) ; } export function encrypt256Key ( encryptionKey , key ) { return aes128Encrypt ( encryptionKey , bitArrayToUint8Array ( key ) , fixedIv , false , false ) . slice ( fixedIv . length ) ; } export function aes256EncryptKey ( encryptionKey , key ) { return aes256Encrypt ( encryptionKey , bitArrayToUint8Array ( key ) , fixedIv , false , false ) . slice ( fixedIv . length ) ; } export function aes256DecryptKey ( encryptionKey , key ) { return uint8ArrayToBitArray ( aes256Decrypt ( encryptionKey , concat ( fixedIv , key ) , false , false ) ) ; } export function decrypt256Key ( encryptionKey , key ) { return uint8ArrayToBitArray ( aes128Decrypt ( encryptionKey , concat ( fixedIv , key ) , false ) ) ; } export function encryptRsaKey ( encryptionKey , privateKey , iv ? ) { return aes128Encrypt ( encryptionKey , hexToUint8Array ( privateKeyToHex ( privateKey ) ) , iv ? iv : random . generateRandomData ( IV_BYTE_LENGTH ) , true , false ) ; } export function decryptRsaKey ( encryptionKey , encryptedPrivateKey ) { return hexToPrivateKey ( uint8ArrayToHex ( aes128Decrypt ( encryptionKey , encryptedPrivateKey , true ) ) ) ; }