1 import { CryptoProxy } from '@proton/crypto';
2 import { arrayToBinaryString, arrayToHexString, binaryStringToArray } from '@proton/crypto/lib/utils';
4 import { stringToUint8Array, uint8ArrayToBase64String, uint8ArrayToString } from './encoding';
6 export const getSHA256String = async (data: string) => {
7 const value = await CryptoProxy.computeHash({ algorithm: 'SHA256', data: binaryStringToArray(data) });
8 return arrayToHexString(value);
11 export const getSHA256BinaryString = async (data: string) => {
12 const value = await CryptoProxy.computeHash({ algorithm: 'SHA256', data: binaryStringToArray(data) });
13 return arrayToBinaryString(value);
16 export const getSHA256Base64String = async (data: string) => {
17 const value = await CryptoProxy.computeHash({ algorithm: 'SHA256', data: binaryStringToArray(data) });
18 return uint8ArrayToBase64String(value);
21 export const generateRandomBytes = (numberOfBytes: number) => crypto.getRandomValues(new Uint8Array(numberOfBytes));
23 export const xorEncryptDecrypt = ({ key, data }: { key: string; data: string }) => {
24 if (key.length !== data.length) {
25 throw new Error('The length of the key and data do not match.');
28 const Uint8Key = stringToUint8Array(key);
29 const Uint8Data = stringToUint8Array(data);
31 const xored = new Uint8Array(Uint8Data.length);
33 for (let j = 0; j < Uint8Data.length; j++) {
34 xored[j] = +Uint8Key[j] ^ +Uint8Data[j];
37 return uint8ArrayToString(xored);