import { createHmac } from "crypto" ; export default function hash ( algorithm , salt , iterations , password ) { if ( algorithm == null || salt == null ) { throw new Error ( `undefined arguments: algorithm='${ algorithm }' salt='${ salt }'` ) ; } iterations = iterations || 1 ; if ( ! isFinite ( iterations ) || iterations > 10000 ) { throw Error ( "number of iterations invalid or too large" ) ; } let hash = password ; for ( let i = 1 , end = iterations , asc = 1 <= end ; asc ? i <= end : i >= end ; asc ? i ++ : i -- ) { hash = createHmac ( algorithm , salt ) . update ( hash ) . digest ( "hex" ) ; } return algorithm + "$" + salt + "$" + iterations + "$" + hash ; }