-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.go
105 lines (92 loc) · 2.29 KB
/
checksum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"os"
"strings"
"hash"
"encoding/hex"
"crypto/sha256"
"crypto/md5"
"crypto/sha1"
"crypto/sha512"
)
var Reset = "\033[0m"
var Red = "\033[31m"
var Green = "\033[32m"
var Yellow = "\033[33m"
func check(e error) {
if e != nil {
panic(e)
}
}
func IdentifyHashType(hash string) string {
if len(hash) == 32 {
return "md5"
} else if len(hash) == 40 {
return "sha1"
} else if len(hash) == 56 {
return "sha224"
} else if len(hash) == 64 {
return "sha256"
} else if len(hash) == 96 {
return "sha384"
} else if len(hash) == 128 {
return "sha512"
} else {
return ""
}
}
func CalculateFileHash(filePath string, hashType string) (string, error) {
dat, err := os.ReadFile(filePath)
check(err)
var h hash.Hash
switch hashType {
case "md5":
h = md5.New()
case "sha1":
h = sha1.New()
case "sha224":
h = sha256.New224()
case "sha256":
h = sha256.New()
case "sha384":
h = sha512.New384()
case "sha512":
h = sha512.New()
default:
return "", fmt.Errorf("unsupported hash type: %s", hashType)
}
h.Write(dat)
hashBytes := h.Sum(nil)
return hex.EncodeToString(hashBytes), nil
}
func main() {
if len(os.Args) < 3 {
fmt.Println(Red + "Usage: checksum <file_path> <hash>" + Reset)
fmt.Println(Red + "Issues @ https://github.com/ibnaleem/checksum/issues" + Reset)
os.Exit(1)
} else {
filePath := os.Args[1]
hashValue := os.Args[2]
hashType := IdentifyHashType(hashValue)
if hashType == "" {
fmt.Println(Red + "Unsupported hash type provided" + Reset)
os.Exit(1)
}
fmt.Println(Yellow + "Using " + strings.ToUpper(hashType) + " algorithm to calculate hash for " + filePath + "..." + Reset)
calculatedHash, err := CalculateFileHash(filePath, hashType)
if err != nil {
fmt.Printf("%s Error: %s %s", Red, err, Reset)
} else {
if calculatedHash == hashValue {
fmt.Printf(Green + "Checksum matches for %s using %s algorithm!\n", filePath, strings.ToUpper(hashType))
fmt.Println(Green + "Provided hash: " + hashValue)
fmt.Println(Green + "Calculated hash: " + calculatedHash + Reset)
} else {
fmt.Printf(Red + "Checksum does not match for %s using %s algorithm!\n", filePath, hashType)
fmt.Println(Red + "Provided hash: " + hashValue)
fmt.Println(Red + "Calculated hash: " + calculatedHash)
}
}
}
}