Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Une fonctionnalité simple, mais souvent nécessaire dans les scripts PowerShell : obtenir le hash d’une chaîne de caractères. En ayant écrit une pour mon collègue Laurent Banon (blog), je me permet de la partager avec vous dans ce post.
function Hash($textToHash)
{
$hasher = new-object System.Security.Cryptography.SHA256Managed
$toHash = [System.Text.Encoding]::UTF8.GetBytes($textToHash)
$hashByteArray = $hasher.ComputeHash($toHash)
foreach($byte in $hashByteArray)
{
$res += $byte.ToString()
}
return $res;
}
Et son utilisation :
PS #> . Hash.ps1
PS #> Hash("hi")
Notez que d’autres algorithmes de hashage peuvent êtres utilisés. On pourra donc substituer au SHA256 un des autres algorithmes disponibles par défaut dans .NET 3.5 ou 4 :
- System.Security.Cryptography.MD5
- System.Security.Cryptography.RIPEMD160
- System.Security.Cryptography.SHA1
- System.Security.Cryptography.SHA256
- System.Security.Cryptography.SHA384
- System.Security.Cryptography.SHA512
Bon scripting!