Skip to content
Myr

Merge commit from fork

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix: fixed to be more properly timing safe Co-authored-by: Taku Amano <taku@taaas.jp> * format * Remove comparing `a` and `b` I think this is necessary. * Remove `console.warn` It is annoying for users. I think only adding the `@deprecated` comment is okay. * update the test * test: add a test for well-known md5 hash collision Co-authored-by: Taku Amano <taku@taaas.jp> * Revert "Remove comparing `a` and `b`" This reverts commit 77b86fd6f7b9f8746dde0933b69eda922e96d0de. * remove the unused file ---------

authored by Yusuke Wada · committed by GitHub · parent 8b17935 · Browse files · 2 files+77 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

2 files+77 added−13 removed

  • tests:+21 added−8 removed
  • other:+56 added−5 removed

2 files+77 added−13 removed

modified:src/utils/buffer.test.ts+21 added-8 removed
modified:src/utils/buffer.ts+56 added-5 removed
24 unmodified lines
2526 return true27}2829const 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 */29export const timingSafeEqual = async (30 a: string | object | boolean,31 b: string | object | boolean,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,79 hashFunction?: Function80): Promise<boolean> => {81 if (typeof a === 'string' && typeof b === 'string') {82 return timingSafeEqualString(a, b, hashFunction as StringHashFunction)83 }8485 if (!hashFunction) {86 hashFunction = sha25687 }8889 const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)])9040 if (!sa || !sb) {91 if (!sa || !sb || typeof sa !== 'string' || typeof sb !== 'string') {92 return false93 }9444 return sa === sb && a === b95 return timingSafeEqualString(sa, sb)96}9798export const bufferToString = (buffer: ArrayBuffer): string => {99 if (buffer instanceof ArrayBuffer) {
17 unmodified lines