buffer.ts
This file
Path
src/utils/buffer.ts
74 lines 路 1.7 KiB 路 TypeScript
Last changed by
chore: set up prettier (#106)Yusuke Wadabefore adoption9c397fb
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475export 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 decodeBase64 = (str: string) => { try { const text = atob(str) const length = text.length const bytes = new Uint8Array(length) for (let i = 0; i < length; i++) { bytes[i] = text.charCodeAt(i) } const decoder = new TextDecoder() return decoder.decode(bytes) } catch {}
try { const { Buffer } = require('buffer') return Buffer.from(str, 'base64').toString() } catch (e) { console.error('If you want to do "decodeBase64", polyfill "buffer" module.') throw e }}
export const sha256 = async (a: string | object | boolean): Promise<string> => { if (crypto && crypto.subtle) { const buffer = await crypto.subtle.digest( { name: 'SHA-256', }, new TextEncoder().encode(String(a)) ) const hash = Array.prototype.map .call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2)) .join('') return hash }
try { const crypto = require('crypto') const hash = crypto.createHash('sha256').update(a).digest('hex') return hash } catch (e) { console.error('If you want to do "sha256", polyfill "crypto" module.') throw e }}
export const timingSafeEqual = async ( a: string | object | boolean, b: string | object | boolean) => { const sa = await sha256(a) const sb = await sha256(b) return sa === sb && a === b}