import * as crypto from 'crypto' ; import { Crypto } from '../crypto' ; export class NodeCrypto implements Crypto { async sha256DigestBase64 ( str ) { return crypto . createHash ( 'sha256' ) . update ( str ) . digest ( 'base64' ) ; } randomBytesBase64 ( count ) { return crypto . randomBytes ( count ) . toString ( 'base64' ) ; } async verify ( pubkey , data , signature ) { const verifier = crypto . createVerify ( 'sha256' ) ; verifier . update ( data ) ; verifier . end ( ) ; return verifier . verify ( pubkey , signature , 'base64' ) ; } async sign ( privateKey , data ) { const signer = crypto . createSign ( 'RSA-SHA256' ) ; signer . update ( data ) ; signer . end ( ) ; return signer . sign ( privateKey , 'base64' ) ; } decodeBase64StringUtf8 ( base64 ) { return Buffer . from ( base64 , 'base64' ) . toString ( 'utf-8' ) ; } encodeBase64StringUtf8 ( text ) { return Buffer . from ( text , 'utf-8' ) . toString ( 'base64' ) ; } param str return async sha256DigestHex ( str ) { return crypto . createHash ( 'sha256' ) . update ( str ) . digest ( 'hex' ) ; } param key param msg return async signWithHmacSha256 ( key , msg ) { const cryptoKey = typeof key === 'string' ? key : toBuffer ( key ) ; return toArrayBuffer ( crypto . createHmac ( 'sha256' , cryptoKey ) . update ( msg ) . digest ( ) ) ; } } param buffer return function toArrayBuffer ( buffer ) { return buffer . buffer . slice ( buffer . byteOffset , buffer . byteOffset + buffer . byteLength ) ; } param arrayBuffer return function toBuffer ( arrayBuffer ) { return Buffer . from ( arrayBuffer ) ; }