| 1 | # | ||||||||||
| 2 | #=============================================================================== | ||||||||||
| 3 | # | ||||||||||
| 4 | # FILE: Pwned.pm | ||||||||||
| 5 | # | ||||||||||
| 6 | # DESCRIPTION: Check HIBP to see if this password has been pwned | ||||||||||
| 7 | # | ||||||||||
| 8 | # FILES: --- | ||||||||||
| 9 | # BUGS: --- | ||||||||||
| 10 | # NOTES: https://haveibeenpwned.com/API/v2#PwnedPasswords | ||||||||||
| 11 | # AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk | ||||||||||
| 12 | # ORGANIZATION: Openstrike | ||||||||||
| 13 | # VERSION: See $VERSION in code | ||||||||||
| 14 | # CREATED: 29/05/18 14:44:30 | ||||||||||
| 15 | # REVISION: --- | ||||||||||
| 16 | #=============================================================================== | ||||||||||
| 17 | |||||||||||
| 18 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | package Password::Policy::Rule::Pwned; | ||||||||||
| 22 | |||||||||||
| 23 | 4 | ▶ | use parent 'Password::Policy::Rule'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | 4 | ▶ | use Password::Policy::Exception::Pwned; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 4 | ▶ | use Password::Policy::Exception::PwnedError; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 4 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 4 | ▶ | use Digest::SHA 'sha1_hex'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 4 | ▶ | use String::Multibyte; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 4 | ▶ | use Encode 'encode'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | 4 | our $VERSION = '0.03'; | |||||||||
| 33 | 4 | my $ua = __PACKAGE__ . '/' . $VERSION; | |||||||||
| 34 | 4 | my $timeout = 5; | |||||||||
| 35 | 4 | our $base_url = 'https://api.pwnedpasswords.com/range/'; | |||||||||
| 36 | |||||||||||
| 37 | 25 | ▶ | sub check { | ||||||||
|
Subroutine
check: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 38 | 25 | my $self = shift; | |||||||||
| 39 | 25 | my $password = $self->prepare (shift); | |||||||||
| 40 | 25 | my $strmb = String::Multibyte->new ('UTF8'); | |||||||||
| 41 | 25 | ▶ | my $hash = uc sha1_hex ($strmb->islegal($password) ? $password : | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | encode ('UTF-8', $password)); | ||||||||||
| 43 | 25 | my $range = substr ($hash, 0, 5, ''); | |||||||||
| 44 | 25 | my $url = $base_url . $range; | |||||||||
| 45 | 25 | my $res = LWP::UserAgent->new (agent => $ua, timeout => $timeout)->get ($url); | |||||||||
| 46 | 25 | ▶ | if ($res->code != 200) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 3 | warn $res->status_line; | |||||||||
| 48 | 3 | Password::Policy::Exception::PwnedError->throw; | |||||||||
| 49 | } | ||||||||||
| 50 | 22 | ▶ | if (index ($res->content, "$hash:") > -1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 12 | Password::Policy::Exception::Pwned->throw; | |||||||||
| 52 | } | ||||||||||
| 53 | 10 | return 1; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | __END__ | ||||||||||