Last active
March 12, 2019 20:06
-
-
Save vexx32/55753d35e58e0006728e8641bf89c718 to your computer and use it in GitHub Desktop.
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
function Test-PasswordLeaked { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory, Position = 0, ValueFromPipeline)] | |
[PSCredential] | |
$Credential | |
) | |
begin { | |
$SHA1 = [System.Security.Cryptography.SHA1Managed]::new() | |
} | |
process { | |
$Hash = $SHA1.ComputeHash( | |
[System.Text.Encoding]::UTF8.GetBytes($Credential.GetNetworkCredential().Password) | |
) | |
[GC]::Collect() | |
$HashString = -join $Hash.ForEach{ $_.ToString("x2") } | |
$QueryPortion = $HashString.Substring(0, 5) | |
$Results = (Invoke-RestMethod -Uri "https://api.pwnedpasswords.com/range/$QueryPortion") -split '\r?\n' | |
$Match = @($Results) -match $HashString.Substring(5) | |
if ($Match) { | |
foreach ($Item in $Match) { | |
[PSCustomObject]@{ | |
Hash = $Item -replace '\:.+$' | |
PwnCount = [int]($Item -replace '^.+\:') | |
} | |
} | |
} | |
else { | |
[PSCustomObject]@{ | |
Hash = $HashString | |
PwnCount = 0 | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment