Skip to content

Instantly share code, notes, and snippets.

@jishanshaikh4
Created May 20, 2022 09:48
Show Gist options
  • Save jishanshaikh4/6ce995a7a8991cb80f4be5c8ec4ed94e to your computer and use it in GitHub Desktop.
Save jishanshaikh4/6ce995a7a8991cb80f4be5c8ec4ed94e to your computer and use it in GitHub Desktop.
Check whether your password has been leaked
<?php
/**
* Simple method to use the API from https://www.troyhunt.com/ive-just-launched-pwned-passwords-version-2/
* Released to public domain
* @return int count
*/
function checkPawnedPasswords(string $password) : int
{
$sha1 = strtoupper(sha1($password));
$data = file_get_contents('https://api.pwnedpasswords.com/range/'.substr($sha1, 0, 5));
if (FALSE !== strpos($data, substr($sha1, 5))) {
$data = explode(substr($sha1, 5).':', $data);
$count = (int) $data[1];
}
return $count ?? 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment