buffer.ts
This file
Path
src/utils/buffer.ts
38 lines 路 687 bytes 路 TypeScript
Last changed by
refactor(utils): better management for utils modules to avoid kitchen sink issue (#59)Minghebefore adoptionc522636
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839export 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: any, b: any) => { const sa = await crypto.subtle.digest( { name: 'SHA-256', }, new TextEncoder().encode(String(a)) )
const sb = await crypto.subtle.digest( { name: 'SHA-256', }, new TextEncoder().encode(String(b)) )
return equal(sa, sb) && a === b}