Skip to content
Myr

buffer.ts

This file

Path

src/utils/buffer.ts

116 lines · 2.7 KiB · TypeScript

Last changed by

Merge commit from forkYusuke Wadabefore adoption91def7c

History of this file

Take it

The walk stopped after 200 commits: lines older than 82bdc4d point at it.

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 1–28why, one step back
1/**
2 * @module3 * Buffer utility.4 */56import { sha256 } from './crypto'78export const equal = (a: ArrayBuffer, b: ArrayBuffer): boolean => {9 if (a === b) {10 return true11 }12 if (a.byteLength !== b.byteLength) {13 return false14 }1516 const va = new DataView(a)17 const vb = new DataView(b)1819 let i = va.byteLength20 while (i--) {21 if (va.getUint8(i) !== vb.getUint8(i)) {22 return false23 }24 }2526 return true27}28

Merge commit from forkYusuke Wadabefore adoption91def7c

lines 29–78why, one step back
29const constantTimeEqualString = (a: string, b: string): boolean => {
30 const aLen = a.length31 const bLen = b.length32 const maxLen = Math.max(aLen, bLen)33 let out = aLen ^ bLen34 for (let i = 0; i < maxLen; i++) {35 const aChar = i < aLen ? a.charCodeAt(i) : 036 const bChar = i < bLen ? b.charCodeAt(i) : 037 out |= aChar ^ bChar38 }39 return out === 040}4142type StringHashFunction = (input: string) => string | null | Promise<string | null>4344const timingSafeEqualString = async (45 a: string,46 b: string,47 hashFunction?: StringHashFunction48): Promise<boolean> => {49 if (!hashFunction) {50 hashFunction = sha25651 }5253 const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)])5455 if (sa == null || sb == null || typeof sa !== 'string' || typeof sb !== 'string') {56 return false57 }5859 const hashEqual = constantTimeEqualString(sa, sb)60 const originalEqual = constantTimeEqualString(a, b)6162 return hashEqual && originalEqual63}6465type TimingSafeEqual = {66 (a: string, b: string, hashFunction?: StringHashFunction): Promise<boolean>67 /**68 * @deprecated object and boolean signatures that take boolean as first and second arguments, and functions with signatures that take non-string arguments have been deprecated69 */70 (71 a: string | object | boolean,72 b: string | object | boolean,73 hashFunction?: Function74 ): Promise<boolean>75}76export const timingSafeEqual: TimingSafeEqual = async (77 a,78 b,

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 79–80why, one step back
79 hashFunction?: Function
80): Promise<boolean> => {

Merge commit from forkYusuke Wadabefore adoption91def7c

lines 81–84why, one step back
81 if (typeof a === 'string' && typeof b === 'string') {
82 return timingSafeEqualString(a, b, hashFunction as StringHashFunction)83 }84

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 85–90why, one step back
85 if (!hashFunction) {
86 hashFunction = sha25687 }8889 const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)])90

Merge commit from forkYusuke Wadabefore adoption91def7c

line 91why, one step back
91 if (!sa || !sb || typeof sa !== 'string' || typeof sb !== 'string') {

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 92–94why, one step back
92 return false
93 }94

Merge commit from forkYusuke Wadabefore adoption91def7c

line 95why, one step back
95 return timingSafeEqualString(sa, sb)

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 96–116why, one step back
96}
9798export const bufferToString = (buffer: ArrayBuffer): string => {99 if (buffer instanceof ArrayBuffer) {100 const enc = new TextDecoder('utf-8')101 return enc.decode(buffer)102 }103 return buffer104}105106export const bufferToFormData = (107 arrayBuffer: ArrayBuffer,108 contentType: string109): Promise<FormData> => {110 const response = new Response(arrayBuffer, {111 headers: {112 'Content-Type': contentType,113 },114 })115 return response.formData()116}