mirror of
https://github.com/outline/outline.git
synced 2026-06-13 03:14:59 +03:00
00fb4d1af7
- crypto → node:crypto - fs → node:fs - fs/promises → node:fs/promises - path → node:path - http → node:http - https → node:https - stream → node:stream - buffer → node:buffer - url → node:url - os → node:os - net → node:net - dns → node:dns - events → node:events - readline → node:readline - querystring → node:querystring - util → node:util
30 lines
669 B
TypeScript
30 lines
669 B
TypeScript
import crypto from "node: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));
|
|
}
|
|
|
|
/**
|
|
* Hash a string using SHA-256.
|
|
*
|
|
* @param input The input string to hash
|
|
* @returns The hashed input
|
|
*/
|
|
export function hash(input: string) {
|
|
return crypto.createHash("sha256").update(input).digest("hex");
|
|
}
|