-
Notifications
You must be signed in to change notification settings - Fork 20
/
hashes.go
222 lines (198 loc) · 4.82 KB
/
hashes.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"crypto"
"errors"
"fmt"
"io"
"log"
"os"
"regexp"
"strings"
)
var alwaysDeleteInvalidFile = false
type Hashes struct {
Hashes []Hash
}
type Hash struct {
Hash string
HashType HashTypeOption
Tags []string
Comments []string
Local bool // True if found locally on the filesystem (used with the -precheckdir flag). Default False
LocalFile string // Full file name if file found on local system (used with the -precheckdir flag)
}
type HashTypeOption int64
const (
NotAValidHashType HashTypeOption = iota
md5
sha1
sha256
)
func (hto HashTypeOption) String() string {
switch hto {
case md5:
return "md5"
case sha1:
return "sha1"
case sha256:
return "sha256"
}
return ""
}
func addHash(hashes Hashes, hash Hash) (Hashes, error) {
if hashes.hashExists(hash.Hash) {
hsh, err := hashes.getByHash(hash.Hash)
if err != nil {
return hashes, err
}
for _, t := range hash.Tags {
if !hsh.TagExists(t) {
hsh.Tags = append(hsh.Tags, t)
}
}
} else {
hashes.Hashes = append(hashes.Hashes, hash)
}
return hashes, nil
}
func (hs Hashes) updateLocalFile(hash string, filename string) {
for idx, h := range hs.Hashes {
if h.Hash == hash {
hs.Hashes[idx].Local = true
hs.Hashes[idx].LocalFile = filename
}
}
}
func (hs Hashes) hashExists(hash string) bool {
for _, h := range hs.Hashes {
if h.Hash == hash {
return true
}
}
return false
}
func (hs Hashes) getByHash(hash string) (Hash, error) {
for idx, h := range hs.Hashes {
if h.Hash == hash {
return hs.Hashes[idx], nil
}
}
return Hash{}, fmt.Errorf("Hash not found")
}
func (h Hash) TagExists(tag string) bool {
for _, t := range h.Tags {
if t == tag {
return true
}
}
return false
}
func (h Hash) ValidateFile(filename string) (bool, string) {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer f.Close()
var sum []byte
if h.HashType == md5 {
hasher := crypto.MD5.New()
if _, err := io.Copy(hasher, f); err != nil {
log.Fatal(err)
}
sum = hasher.Sum(nil)
} else if h.HashType == sha1 {
hasher := crypto.SHA1.New()
if _, err := io.Copy(hasher, f); err != nil {
log.Fatal(err)
}
sum = hasher.Sum(nil)
} else if h.HashType == sha256 {
hasher := crypto.SHA256.New()
if _, err := io.Copy(hasher, f); err != nil {
log.Fatal(err)
}
sum = hasher.Sum(nil)
}
if (fmt.Sprintf("%x", sum)) == strings.ToLower(h.Hash) {
return true, fmt.Sprintf("%x", sum)
} else {
return false, fmt.Sprintf("%x", sum)
}
}
func (h Hash) Validate(bytes []byte) (bool, string) {
var sum []byte
if h.HashType == md5 {
hasher := crypto.MD5.New()
hasher.Write(bytes)
sum = hasher.Sum(nil)
} else if h.HashType == sha1 {
hasher := crypto.SHA1.New()
hasher.Write(bytes)
sum = hasher.Sum(nil)
} else if h.HashType == sha256 {
hasher := crypto.SHA256.New()
hasher.Write(bytes)
sum = hasher.Sum(nil)
}
if (fmt.Sprintf("%x", sum)) == strings.ToLower(h.Hash) {
return true, fmt.Sprintf("%x", sum)
} else {
return false, fmt.Sprintf("%x", sum)
}
}
func deleteInvalidFile(filename string) {
if !alwaysDeleteInvalidFile {
var delete_file string
fmt.Printf(" [?] Delete invalid file? [A/Y/n] Always delete/Yes, this time/No, not this time\n")
fmt.Scanln(&delete_file)
if strings.ToUpper(delete_file) == "Y" || delete_file == "" || strings.ToUpper(delete_file) == "A" {
os.Remove(filename)
fmt.Printf(" [!] Deleted invalid file\n")
if strings.ToUpper(delete_file) == "A" {
alwaysDeleteInvalidFile = true
}
} else {
fmt.Printf(" [!] Keeping invalid file\n")
}
} else {
os.Remove(filename)
fmt.Printf(" [!] Deleted invalid file\n")
}
}
func hashType(hash string) (HashTypeOption, error) {
match, _ := regexp.MatchString("^[A-Fa-f0-9]{64}$", hash)
if match {
return sha256, nil
}
match, _ = regexp.MatchString("^[A-Fa-f0-9]{40}$", hash)
if match {
return sha1, nil
}
match, _ = regexp.MatchString("^[A-Fa-f0-9]{32}$", hash)
if match {
return md5, nil
}
return NotAValidHashType, errors.New("not a valid hash")
}
func extractHashes(text string) ([]string, error) {
hashes := make([]string, 0)
re := regexp.MustCompile(`>\s*[A-Fa-f0-9]{64}\s*<`)
matches := re.FindAllStringSubmatch(text, 100)
for m := range matches {
hashes = append(hashes, strings.TrimSpace(matches[m][0][1:len(matches[m][0])-1]))
}
re = regexp.MustCompile(`>\s*[A-Fa-f0-9]{40}\s*<`)
matches = re.FindAllStringSubmatch(text, 100)
for m := range matches {
hashes = append(hashes, strings.TrimSpace(matches[m][0][1:len(matches[m][0])-1]))
}
re = regexp.MustCompile(`>\s*[A-Fa-f0-9]{32}\s*<`)
matches = re.FindAllStringSubmatch(text, 100)
for m := range matches {
hashes = append(hashes, strings.TrimSpace(matches[m][0][1:len(matches[m][0])-1]))
}
if len(hashes) > 0 {
return hashes, fmt.Errorf("no hashes found")
}
return hashes, nil
}