import { normalizeInput , HashInput } from './hash-fn' ; import { BaseHash , IHasher , IInternalHash , IHasherDigestOptions } from '../base/index' ; import { Transform , TransformCallback } from 'stream' ; import { IBaseHashOptions } from '../base/hash-fn' ; import { getWasm } from './wasm' ; import { NodeHashReader } from './hash-reader' ; export interface INodeHash extends IHasher < Buffer > { inheritdoc @override
      update ( data , encoding ? ) : this ; inheritdoc @override
      digest ( options ? ) : Buffer ; /**
     * Returns a digest of the hash with the given set of hash options.
     */ digest ( encoding , options ) : Buffer ; /**
     * Returns a digest of the hash with the given encoding.
     */ digest ( encoding , options ? ) : string ; } inheritdoc export class NodeHash < Reader > extends Transform implements IHasher < Buffer > { private readonly hash : BaseHash < Buffer , Reader , NodeHashReader > ; constructor ( implementation , getReader ) { super ( ) ; this . hash = new BaseHash ( implementation , l => Buffer . alloc ( l ) , getReader ) ; } reader public reader ( options ? ) { const reader = this . hash . reader ( options ) ; return reader ; } inheritdoc public update ( data , encoding ? ) { this . hash . update ( normalizeInput ( data , encoding ) ) ; return this ; } inheritdoc public digest ( encoding ? ) ; public digest ( encoding , options ) ; public digest ( encoding , options ? ) ; public digest ( encoding ? , options ? ) { let resolvedOpts ; let resolvedEnc ; if ( encoding && typeof encoding === 'object' ) { resolvedOpts = encoding ; resolvedEnc = undefined ; } else { resolvedOpts = options ; resolvedEnc = encoding ; } const result = this . hash . digest ( resolvedOpts ) ; return resolvedEnc ? result . toString ( resolvedEnc ) : result ; } inheritdoc public dispose ( ) { this . hash . dispose ( ) ; } inheritdoc hidden _transform ( chunk , encoding , callback ) { this . update ( chunk , encoding as BufferEncoding ) ; callback ( ) ; } inheritdoc hidden _flush ( callback ) { callback ( null , this . digest ( ) ) ; } } /**
 * A Node.js crypto-like createHash method.
 */ export const createHash = ( ) => new NodeHash ( getWasm ( ) . create_hasher ( ) , r => new NodeHashReader ( r ) ) ; /**
 * Construct a new Hasher for the keyed hash function.
 */ export const createKeyed = ( key ) => new NodeHash ( getWasm ( ) . create_keyed ( key ) , r => new NodeHashReader ( r ) ) ; /**
 * Construct a new Hasher for the key derivation function.
 */ export const createDeriveKey = ( context ) => new NodeHash ( getWasm ( ) . create_derive ( context ) , r => new NodeHashReader ( r ) ) ;