2014-11-13, 09:13
Following up on an article a few days ago here’s a script that creates a MD5 file.
# Uwe Ziegenhagen, 12.11.2014
# Reference: http://onlinemd5.com/
### Step A: Get the name of the file
# http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
} #end function Get-FileName
# *** Entry Point to Script ***
$file = Get-FileName -initialDirectory "C:\"
# write-host "file: " $file
### Step B: Create a file handle from this string and calculate the MD5 sum
$filehandle = Get-ChildItem $file
write-host "Filename: " $filehandle.Name
# http://stackoverflow.com/questions/10521061/how-to-get-an-md5-checksum-in-powershell
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($filehandle))).ToLower().Replace("-","")
write-host "Calculated Hashsum: " $hash
### Step C: Write the MD5 sum from the MD5 sum file matching the other filename
$hash | out-file -FilePath $($filehandle.DirectoryName + "\" + $filehandle.BaseName + ".md5") |
# Uwe Ziegenhagen, 12.11.2014
# Reference: http://onlinemd5.com/
### Step A: Get the name of the file
# http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
} #end function Get-FileName
# *** Entry Point to Script ***
$file = Get-FileName -initialDirectory "C:\"
# write-host "file: " $file
### Step B: Create a file handle from this string and calculate the MD5 sum
$filehandle = Get-ChildItem $file
write-host "Filename: " $filehandle.Name
# http://stackoverflow.com/questions/10521061/how-to-get-an-md5-checksum-in-powershell
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($filehandle))).ToLower().Replace("-","")
write-host "Calculated Hashsum: " $hash
### Step C: Write the MD5 sum from the MD5 sum file matching the other filename
$hash | out-file -FilePath $($filehandle.DirectoryName + "\" + $filehandle.BaseName + ".md5")
Uwe Ziegenhagen mag LaTeX und Python, auch gern in Kombination.
Hat Dir dieser Beitrag geholfen und möchtest Du Dich dafür bedanken? Dann unterstütze doch vielleicht die Dingfabrik Köln e.V. mit einem kleinen Beitrag. Details zur Bezahlung findest Du unter Spenden für die Dingfabrik.
More Posts - Website
Schlagwörter:
MD5,
Powershell Category:
Allgemein |
Kommentare deaktiviert für Using Powershell to create MD5 files
2014-11-06, 22:19
Here some Python code to calculate the MD5 hash for a given file:
# from http://www.pythoncentral.io/hashing-files-with-python/
import hashlib
hasher = hashlib.md5()
with open('e:/Mappe1.xlsx', 'rb') as afile:
buf = afile.read()
hasher.update(buf)
print(hasher.hexdigest()) |
# from http://www.pythoncentral.io/hashing-files-with-python/
import hashlib
hasher = hashlib.md5()
with open('e:/Mappe1.xlsx', 'rb') as afile:
buf = afile.read()
hasher.update(buf)
print(hasher.hexdigest())
Here the final script that provides the same functionality as the Powershell script I posted earlier today:
# http://www.pythoncentral.io/hashing-files-with-python/
import hashlib
import os
import codecs
hasher = hashlib.md5()
def calcHash(filename):
print("Filename: " + filename)
with open(filename, 'rb') as afile:
buf = afile.read()
hasher.update(buf)
print("Calculated: " + hasher.hexdigest())
md5file = (os.path.splitext(filename)[0]) + ".md5"
givenMD5 = codecs.open(md5file, "r", "utf-8-sig")
print("Provided: " + str(givenMD5.read()).lower())
calcHash("e:/Mappe1.xlsx") |
# http://www.pythoncentral.io/hashing-files-with-python/
import hashlib
import os
import codecs
hasher = hashlib.md5()
def calcHash(filename):
print("Filename: " + filename)
with open(filename, 'rb') as afile:
buf = afile.read()
hasher.update(buf)
print("Calculated: " + hasher.hexdigest())
md5file = (os.path.splitext(filename)[0]) + ".md5"
givenMD5 = codecs.open(md5file, "r", "utf-8-sig")
print("Provided: " + str(givenMD5.read()).lower())
calcHash("e:/Mappe1.xlsx")
Uwe Ziegenhagen mag LaTeX und Python, auch gern in Kombination.
Hat Dir dieser Beitrag geholfen und möchtest Du Dich dafür bedanken? Dann unterstütze doch vielleicht die Dingfabrik Köln e.V. mit einem kleinen Beitrag. Details zur Bezahlung findest Du unter Spenden für die Dingfabrik.
More Posts - Website
2014-11-06, 20:56
Here’s some code to a) calculate a MD5 hash for a given file and b) to compare the hash with a MD5 provided in an external file. The scripts expects the MD5 file to have the same name, but MD5 extension. Credit goes to the friendly souls providing the original code, I just copied and pasted everything together.
The comparison is just visual, both MD5 hashes — the calculated and the provided one — are printed on the screen, it’s easily to extend it for an automated comparison.
# Reference: http://onlinemd5.com/
### Step A: Get the name of the file
# http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
} #end function Get-FileName
# *** Entry Point to Script ***
$file = Get-FileName -initialDirectory "H:\MD5"
### Step B: Create a file handle from this string and calculate the MD5 sum
$filehandle = Get-ChildItem $file
# write-host "file: " $file
# write-host "DirectoryName: " $filehandle.DirectoryName
write-host "Name: " $filehandle.Name
# write-host "pure Name: " $filehandle.BaseName
# http://stackoverflow.com/questions/10521061/how-to-get-an-md5-checksum-in-powershell
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($filehandle))).ToLower().Replace("-","")
write-host "Calculated Hashsum: " $hash
### Step C: Get the MD5 sum from the MD5 sum file matching the other filename
$md5handle = Get-ChildItem $($filehandle.DirectoryName + "\" + $filehandle.BaseName + ".md5")
$md5sum = [IO.File]::ReadAllText($md5handle)
write-host "Provided MD5 " $md5sum |
# Reference: http://onlinemd5.com/
### Step A: Get the name of the file
# http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
} #end function Get-FileName
# *** Entry Point to Script ***
$file = Get-FileName -initialDirectory "H:\MD5"
### Step B: Create a file handle from this string and calculate the MD5 sum
$filehandle = Get-ChildItem $file
# write-host "file: " $file
# write-host "DirectoryName: " $filehandle.DirectoryName
write-host "Name: " $filehandle.Name
# write-host "pure Name: " $filehandle.BaseName
# http://stackoverflow.com/questions/10521061/how-to-get-an-md5-checksum-in-powershell
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($filehandle))).ToLower().Replace("-","")
write-host "Calculated Hashsum: " $hash
### Step C: Get the MD5 sum from the MD5 sum file matching the other filename
$md5handle = Get-ChildItem $($filehandle.DirectoryName + "\" + $filehandle.BaseName + ".md5")
$md5sum = [IO.File]::ReadAllText($md5handle)
write-host "Provided MD5 " $md5sum
Uwe Ziegenhagen mag LaTeX und Python, auch gern in Kombination.
Hat Dir dieser Beitrag geholfen und möchtest Du Dich dafür bedanken? Dann unterstütze doch vielleicht die Dingfabrik Köln e.V. mit einem kleinen Beitrag. Details zur Bezahlung findest Du unter Spenden für die Dingfabrik.
More Posts - Website
Schlagwörter:
MD5,
Powershell Category:
Powershell |
Kommentare deaktiviert für Using Powershell to calculate/compare MD5 hashes