Question [Fonction] Récupérer le Checksum MD5 ou SHA
- Alan Pinard
- Auteur du sujet
- Hors Ligne
- Membre senior
-
Réduire
Plus d'informations
- Messages : 77
- Remerciements reçus 0
il y a 16 ans 6 mois #5249
par Alan Pinard
Alan Pinard
Version A
[Fonction] Récupérer le Checksum MD5 ou SHA a été créé par Alan Pinard
Bonjour à tous,
Ce script vous permet de récupérer le Checksum MD5 ou SHA d'un fichier.
[code:1]
#region Fonctions Get-MD5Sum
Function Get-MD5Sum {
#Derniere mise a jour : 18-11-2009
Param (
[String] $File = $(Throw \"Utilisation: Get-MD5Sum -File <file.txt> [sha1|md5] \"),
[String] $HashType = \"md5\"
)
If ($HashType -eq \"\")
{
Throw \"Utilisation: Get-MD5Sum -File <file.txt> [sha1|md5] \"
}
If ($HashType -eq \"sha1\")
{
$Provider = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
}
ElseIf ($HashType -eq \"md5\")
{
$Provider = New-Object System.Security.Cryptography.MD5CryptoServiceProvider
}
Else
{
Throw \"Type de hash non supporte!\"
}
$FileInfo = New-Object System.IO.FileInfo($File)
If (-not $FileInfo.Exists)
{
# Si le fichier n'est pas trouvé, le script verifi dans le repertoire courant
$FileInfo = New-Object System.IO.FileInfo(\"$pwd\$File\")
If (-not $FileInfo.Exists)
{
Throw \"Impossible de trouver le fichier -> $FileInfo\"
}
}
$inStream = $FileInfo.OpenRead()
$hashBytes = $Provider.ComputeHash($inStream)
[void] $inStream.Close()
Trap
{
If ($inStream -ne $Null)
{
[void] $inStream.Close()
}
Break
}
[String]$MD5SumString = $Null
Foreach ($byte in $hashBytes)
{
If(($MD5SumString -eq $Null) -or ($MD5SumString -eq \"\"))
{ #Si $MD5SumString est vide alors $MD5SumString est egale a $byte.ToString(\"X2\")
$MD5SumString = $byte.ToString(\"X2\")
}
Else
{ #Sinon, je calcule le length de $MD5SumString afin d'insérer à un index précis les informations
$Index = $MD5SumString.Length
$InsertStr = $byte.ToString(\"X2\")
$MD5SumString = $MD5SumString.Insert($Index, $InsertStr)
}
}
return $MD5SumString
#Utilisation Get-MD5Sum -File \"D:\fichier.pdf\"
}
#endregion
[/code:1]
La fontion retourne une chaine de caracteres.
Alan Pinard
Version A
Message édité par: Versiona, à: 18/11/09 15:56
Message édité par: Versiona, à: 18/11/09 15:57<br><br>Message édité par: Versiona, à: 18/11/09 15:57
Ce script vous permet de récupérer le Checksum MD5 ou SHA d'un fichier.
[code:1]
#region Fonctions Get-MD5Sum
Function Get-MD5Sum {
#Derniere mise a jour : 18-11-2009
Param (
[String] $File = $(Throw \"Utilisation: Get-MD5Sum -File <file.txt> [sha1|md5] \"),
[String] $HashType = \"md5\"
)
If ($HashType -eq \"\")
{
Throw \"Utilisation: Get-MD5Sum -File <file.txt> [sha1|md5] \"
}
If ($HashType -eq \"sha1\")
{
$Provider = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
}
ElseIf ($HashType -eq \"md5\")
{
$Provider = New-Object System.Security.Cryptography.MD5CryptoServiceProvider
}
Else
{
Throw \"Type de hash non supporte!\"
}
$FileInfo = New-Object System.IO.FileInfo($File)
If (-not $FileInfo.Exists)
{
# Si le fichier n'est pas trouvé, le script verifi dans le repertoire courant
$FileInfo = New-Object System.IO.FileInfo(\"$pwd\$File\")
If (-not $FileInfo.Exists)
{
Throw \"Impossible de trouver le fichier -> $FileInfo\"
}
}
$inStream = $FileInfo.OpenRead()
$hashBytes = $Provider.ComputeHash($inStream)
[void] $inStream.Close()
Trap
{
If ($inStream -ne $Null)
{
[void] $inStream.Close()
}
Break
}
[String]$MD5SumString = $Null
Foreach ($byte in $hashBytes)
{
If(($MD5SumString -eq $Null) -or ($MD5SumString -eq \"\"))
{ #Si $MD5SumString est vide alors $MD5SumString est egale a $byte.ToString(\"X2\")
$MD5SumString = $byte.ToString(\"X2\")
}
Else
{ #Sinon, je calcule le length de $MD5SumString afin d'insérer à un index précis les informations
$Index = $MD5SumString.Length
$InsertStr = $byte.ToString(\"X2\")
$MD5SumString = $MD5SumString.Insert($Index, $InsertStr)
}
}
return $MD5SumString
#Utilisation Get-MD5Sum -File \"D:\fichier.pdf\"
}
#endregion
[/code:1]
La fontion retourne une chaine de caracteres.
Alan Pinard
Version A
Message édité par: Versiona, à: 18/11/09 15:56
Message édité par: Versiona, à: 18/11/09 15:57<br><br>Message édité par: Versiona, à: 18/11/09 15:57
Alan Pinard
Version A
Pièces jointes :
Connexion ou Créer un compte pour participer à la conversation.
Temps de génération de la page : 0.048 secondes
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Contributions à la communauté
- [Fonction] Récupérer le Checksum MD5 ou SHA