Forked from JimWestergren/checkPawnedPasswords.php
Created
February 22, 2018 14:25
-
-
Save designviacode/10af4b369863a0a46cd05ed980480567 to your computer and use it in GitHub Desktop.
Simple method to check the Pwned Passwords API using PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple method to use the API from https://www.troyhunt.com/ive-just-launched-pwned-passwords-version-2/ | |
* Written by Jim Westergren and 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 (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