buffer.ts
This file
Path
src/utils/buffer.ts
35 lines · 674 bytes · TypeScript
Last changed by
feat: basic auth middleware supports overriding `hashFunction` (#128)Yusuke Wadabefore adoption7a31de5
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536import { sha256 } from './crypto'
export const equal = (a: ArrayBuffer, b: ArrayBuffer) => { if (a === b) { return true } if (a.byteLength !== b.byteLength) { return false }
const va = new DataView(a) const vb = new DataView(b)
let i = va.byteLength while (i--) { if (va.getUint8(i) !== vb.getUint8(i)) { return false } }
return true}
export const timingSafeEqual = async ( a: string | object | boolean, b: string | object | boolean, hashFunction?: Function) => { if (!hashFunction) { hashFunction = sha256 } const sa = await hashFunction(a) const sb = await hashFunction(b) return sa === sb && a === b}