Skip to content
Snippets Groups Projects
Commit 6fa1ee62 authored by Johan de Klerk's avatar Johan de Klerk
Browse files

GenerateHashFromObject

parent b7dfc89f
Branches
Tags v1.110.0
No related merge requests found
......@@ -9,6 +9,7 @@ import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
......@@ -23,21 +24,23 @@ func Hash(input string, key string) string {
}
// GenerateHashFromObject using HMAC with SHA-256
func GenerateHashFromObject(obejct any, secret string) (string, error) {
func GenerateHashFromObject(object any, secret string) (string, error) {
// Base64 Encode body
var buf bytes.Buffer
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
defer encoder.Close()
err := json.NewEncoder(encoder).Encode(obejct)
err := json.NewEncoder(encoder).Encode(object)
if err != nil {
return "", err
}
encodedBody := buf.String()
// Sign encoded body with secret
hashedObject := Hash(encodedBody, secret)
return hashedObject, nil
hash := hmac.New(sha256.New, []byte(secret))
hash.Write([]byte(encodedBody))
hashedBody := hex.EncodeToString(hash.Sum(nil))
return hashedBody, nil
}
func Md5HashString(bytesToHash []byte) string {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment