import { Crypto } from './interfaces' ; const cryptoLib = window . crypto ; export const crypto = { async digest ( cipherText ) { const checksum = await cryptoLib . subtle . digest ( 'SHA-256' , cipherText ) ; return Buffer . from ( checksum ) ; } , async decrypt ( cipherText , keyBytes ) { const key = await cryptoLib . subtle . importKey ( 'raw' , keyBytes , 'AES-CBC' , false , [ 'decrypt' ] ) ; const initializationVector = cipherText . slice ( 0 , 16 ) ; const assetCipherText = cipherText . slice ( 16 ) ; const decipher = await cryptoLib . subtle . decrypt ( { iv : initializationVector , name : 'AES-CBC' } , key , assetCipherText ) ; return Buffer . from ( decipher ) ; } , getRandomValues ( size ) { return Buffer . from ( cryptoLib . getRandomValues ( new Uint8Array ( size ) ) ) ; } , async encrypt ( plainText , keyBytes , initializationVector ) { const key = await cryptoLib . subtle . importKey ( 'raw' , keyBytes , 'AES-CBC' , true , [ 'encrypt' ] ) ; return { key : Buffer . from ( await cryptoLib . subtle . exportKey ( 'raw' , key ) ) , cipher : await cryptoLib . subtle . encrypt ( { iv : initializationVector , name : 'AES-CBC' } , key , plainText ) , } ; } , } ;