send hashes in JSON format

This commit is contained in:
milarin 2023-02-16 14:36:52 +01:00
parent c3fae371a5
commit d82d07628b
2 changed files with 17 additions and 3 deletions

13
hash.go Normal file
View File

@ -0,0 +1,13 @@
package main
import (
"encoding/hex"
)
type Hash struct {
Hash string `json:"hash"`
}
func NewHash(hash []byte) *Hash {
return &Hash{Hash: hex.EncodeToString(hash)}
}

View File

@ -3,7 +3,6 @@ package main
import (
"bytes"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"fmt"
"io"
@ -60,8 +59,9 @@ func GetAllHashHandler(w http.ResponseWriter, r *http.Request) {
return
}
w.Header().Add("Content-Type", "application/json")
hash := sha512.Sum512(b.Bytes())
if _, err := fmt.Fprint(w, hex.EncodeToString(hash[:])); err != nil {
if err := json.NewEncoder(w).Encode(NewHash(hash[:])); err != nil {
InternalServerError(w, err)
return
}
@ -113,7 +113,8 @@ func GetFileHashHandler(w http.ResponseWriter, r *http.Request) {
return
}
if _, err := fmt.Fprint(w, hex.EncodeToString(hasher.Sum(nil))); err != nil {
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(NewHash(hasher.Sum(nil))); err != nil {
InternalServerError(w, err)
return
}