diff --git a/encryption/encryption.go b/encryption/encryption.go index 2a0dfc0269e21ab5a7f556d4d0dce4c3b5d2bc7b..9d06d057de8806812f23988d817fd48ab766db24 100644 --- a/encryption/encryption.go +++ b/encryption/encryption.go @@ -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 {