import { Crypto } from './interfaces' ; import * as cryptoLib from 'crypto' ; export const crypto = { async digest ( cipherText ) { return cryptoLib . createHash ( 'SHA256' ) . update ( cipherText ) . digest ( ) ; } , async decrypt ( cipherText , keyBytes ) { const initializationVector = cipherText . slice ( 0 , 16 ) ; const assetCipherText = cipherText . slice ( 16 ) ; const decipher = cryptoLib . createDecipheriv ( 'AES-256-CBC' , keyBytes , initializationVector ) ; const decipherUpdated = decipher . update ( assetCipherText ) ; const decipherFinal = decipher . final ( ) ; return Buffer . concat ( [ decipherUpdated , decipherFinal ] ) ; } , getRandomValues ( size ) { return cryptoLib . randomBytes ( size ) ; } , async encrypt ( plainText , keyBytes , initializationVector , algorithm ) { const cipher = cryptoLib . createCipheriv ( algorithm , keyBytes , initializationVector ) ; const cipherUpdated = cipher . update ( plainText ) ; const cipherFinal = cipher . final ( ) ; const cipherText = Buffer . concat ( [ cipherUpdated , cipherFinal ] ) ; return { key : keyBytes , cipher : cipherText , } ; } , } ;