Skip to content
Myr

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 file

Take it

refactor(utils): better management for utils modules to avoid kitchen sink issue (#59)Minghebefore adoptionc522636

lines 1–38why, one step back
1export const equal = (a: ArrayBuffer, b: ArrayBuffer) => {
2 if (a === b) {3 return true4 }5 if (a.byteLength !== b.byteLength) {6 return false7 }89 const va = new DataView(a)10 const vb = new DataView(b)1112 let i = va.byteLength13 while (i--) {14 if (va.getUint8(i) !== vb.getUint8(i)) {15 return false16 }17 }1819 return true20}2122export const timingSafeEqual = async (a: any, b: any) => {23 const sa = await crypto.subtle.digest(24 {25 name: 'SHA-256',26 },27 new TextEncoder().encode(String(a))28 )2930 const sb = await crypto.subtle.digest(31 {32 name: 'SHA-256',33 },34 new TextEncoder().encode(String(b))35 )3637 return equal(sa, sb) && a === b38}