Created
June 14, 2024 07:48
-
-
Save kojix2/3eb9eb56cb4cd07804b6c0ee8cf6b97a to your computer and use it in GitHub Desktop.
Crystal lang md5 check
This file contains hidden or 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
require "digest/md5" | |
require "colorize" | |
# Get the filename from the command-line arguments | |
filename = ARGV[0] | |
# Change the directory to the location of the file | |
Dir.cd File.dirname(filename) | |
# Define a FileRecord structure to store the md5sum and file path | |
struct FileRecord | |
property md5sum : String | |
property filepath : Path | |
def initialize(@md5sum, @filepath) | |
end | |
end | |
# Read the checksum file and parse each line into records | |
def parse_checksum_file(filename) | |
records = [] of FileRecord | |
File.open(filename) do |file| | |
file.each_line do |line| | |
sum, path = line.chomp.split | |
records << FileRecord.new(sum, Path[path]) | |
end | |
end | |
records | |
end | |
# Verify the MD5 checksums of the files | |
def verify_checksums(records : Array(FileRecord)) | |
digest = Digest::MD5.new | |
records.each_with_index do |file_record, index| | |
actual_md5 = digest.file(file_record.filepath).hexfinal | |
expected_md5 = file_record.md5sum | |
if actual_md5 == expected_md5 | |
print("\x1b[2K\r") # Clear the line | |
print "(#{index + 1}/#{records.size})" | |
print " OK ".colorize(:green) | |
print file_record.filepath | |
else | |
print("\x1b[2K\r") # Clear the line | |
print "(#{index + 1}/#{records.size})" | |
print " ERROR ".colorize(:red) | |
puts file_record.filepath | |
puts " expected: #{expected_md5}" | |
puts " actual: #{actual_md5}" | |
end | |
digest.reset | |
end | |
print("\x1b[2K\r") # Clear the line | |
puts "\nVerification complete." | |
end | |
# Main script execution | |
records = parse_checksum_file(filename) | |
verify_checksums(records) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment