mirror of
https://github.com/outline/outline.git
synced 2025-12-30 15:30:12 -06:00
20 lines
444 B
TypeScript
20 lines
444 B
TypeScript
import crypto from "crypto";
|
|
|
|
/**
|
|
* Compare two strings in constant time to prevent timing attacks.
|
|
*
|
|
* @param a The first string to compare
|
|
* @param b The second string to compare
|
|
* @returns Whether the strings are equal
|
|
*/
|
|
export function safeEqual(a?: string, b?: string) {
|
|
if (!a || !b) {
|
|
return false;
|
|
}
|
|
if (a.length !== b.length) {
|
|
return false;
|
|
}
|
|
|
|
return crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b));
|
|
}
|