-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
52 lines (45 loc) · 1.06 KB
/
hash.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
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"io/ioutil"
"net/http"
)
func calculateFileHash(filePath string) (string, error) {
fileData, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
hasher := sha256.New()
hasher.Write(fileData)
hash := hex.EncodeToString(hasher.Sum(nil))
return hash, nil
}
func getHashHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
getIsFileHashed(w, r)
} else {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func getIsFileHashed(w http.ResponseWriter, r *http.Request) {
var input map[string]string
err := json.NewDecoder(r.Body).Decode(&input)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
output := make(map[string]bool)
for key, value := range input {
_, ok := FileHash[value]
output[key] = ok
}
response, err := json.Marshal(output)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}