| 19 | } |
| 20 | |
| 21 | export async function verifyPasswordStrength( |
| 22 | password: string, |
| 23 | ): Promise<boolean> { |
| 24 | if (password.length < 8 || password.length > 255) { |
| 25 | return false; |
| 26 | } |
| 27 | const hash = encodeHexLowerCase(sha1(new TextEncoder().encode(password))); |
| 28 | const hashPrefix = hash.slice(0, 5); |
| 29 | const response = await fetch( |
| 30 | `https://api.pwnedpasswords.com/range/${hashPrefix}`, |
| 31 | ); |
| 32 | const data = await response.text(); |
| 33 | const items = data.split('\n'); |
| 34 | for (const item of items) { |
| 35 | const hashSuffix = item.slice(0, 35).toLowerCase(); |
| 36 | if (hash === hashPrefix + hashSuffix) { |
| 37 | return false; |
| 38 | } |
| 39 | } |
| 40 | return true; |
| 41 | } |