import { BrowserCrypto } from './browser/crypto' ; import { NodeCrypto } from './node/crypto' ; export interface JwkCertificate { kty : string ; alg : string ; use ? : string ; kid : string ; n : string ; e : string ; } export interface CryptoSigner { update ( data ) : void ; sign ( key , outputFormat ) : string ; } export interface Crypto { sha256DigestBase64 ( str ) : Promise < string > ; randomBytesBase64 ( n ) : string ; verify ( pubkey , data , signature ) : Promise < boolean > ; sign ( privateKey , data ) : Promise < string > ; decodeBase64StringUtf8 ( base64 ) : string ; encodeBase64StringUtf8 ( text ) : string ; param str return sha256DigestHex ( str ) : Promise < string > ; param key param msg return signWithHmacSha256 ( key , msg ) : Promise < ArrayBuffer > ; } export function createCrypto ( ) { if ( hasBrowserCrypto ( ) ) { return new BrowserCrypto ( ) ; } return new NodeCrypto ( ) ; } export function hasBrowserCrypto ( ) { return ( typeof window !== 'undefined' && typeof window . crypto !== 'undefined' && typeof window . crypto . subtle !== 'undefined' ) ; } param arrayBuffer return export function fromArrayBufferToHex ( arrayBuffer ) { const byteArray = Array . from ( new Uint8Array ( arrayBuffer ) ) ; return byteArray . map ( byte => { return byte . toString ( 16 ) . padStart ( 2 , '0' ) ; } ) . join ( '' ) ; }