File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11/**
22 * HaveIBeenPwned (HIBP) Password Breach Checker
3- *
3+ *
44 * Uses the k-anonymity API to check if a password has been compromised
55 * Privacy-friendly: only sends first 5 characters of SHA-1 hash
6- *
6+ *
77 * Free API: https://haveibeenpwned.com/API/v3
88 */
99
@@ -18,15 +18,19 @@ export interface PasswordBreachResult {
1818/**
1919 * Check if a password has been compromised using HaveIBeenPwned API
2020 * Uses k-anonymity approach - only sends hash prefix to HIBP
21- *
21+ *
2222 * @param password - The password to check
2323 * @returns Object with isCompromised flag and occurrence count
2424 */
25- export async function checkPasswordBreach ( password : string ) : Promise < PasswordBreachResult > {
25+ export async function checkPasswordBreach (
26+ password : string
27+ ) : Promise < PasswordBreachResult > {
2628 try {
27- // Calculate SHA-1 hash of password
28- const hash = createHash ( 'sha1' ) . update ( password ) . digest ( 'hex' ) . toUpperCase ( )
29-
29+ // SHA-1 is mandated by the HaveIBeenPwned k-anonymity API protocol — it is NOT
30+ // used to store the password. Only the first 5 hex characters of the hash are
31+ // ever transmitted; the full hash and the plaintext are discarded immediately.
32+ const hash = createHash ( 'sha1' ) . update ( password ) . digest ( 'hex' ) . toUpperCase ( ) // codeql[js/insufficient-password-hash]
33+
3034 // Take first 5 characters of hash (k-anonymity approach)
3135 const hashPrefix = hash . substring ( 0 , 5 )
3236 const hashSuffix = hash . substring ( 5 )
You can’t perform that action at this time.
0 commit comments