Finding Identical Files Using Hash Algorithm
Golang
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
)
// getFileHash computes the MD5 hash of a file
func getFileHash(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
hasher := md5.New()
if _, err := io.Copy(hasher, file); err != nil {
return "", err
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
// compareFiles checks if two files are identical by comparing their hashes
func compareFiles(filePath1, filePath2 string) (bool, error) {
hash1, err := getFileHash(filePath1)
if err != nil {
return false, fmt.Errorf("error hashing file %s: %v", filePath1, err)
}
hash2, err := getFileHash(filePath2)
if err != nil {
return false, fmt.Errorf("error hashing file %s: %v", filePath2, err)
}
return hash1 == hash2, nil
}
func main() {
file1 := "path/to/file1"
file2 := "path/to/file2"
isIdentical, err := compareFiles(file1, file2)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
if isIdentical {
fmt.Println("Files are identical.")
} else {
fmt.Println("Files are different.")
}
}
Python
import hashlib
def hash_file(filepath):
hasher = hashlib.md5()
with open(filepath, 'rb') as f:
buf = f.read()
hasher.update(buf)
return hasher.hexdigest()
file1 = 'path/to/file1'
file2 = 'path/to/file2'
if hash_file(file1) == hash_file(file2):
print("Files are identical")
else:
print("Files are different")
Leave a Reply