send hashes in JSON format
This commit is contained in:
parent
c3fae371a5
commit
d82d07628b
13
hash.go
Normal file
13
hash.go
Normal 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)}
|
||||
}
|
7
main.go
7
main.go
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user