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

Generate hash from object function

parent 5f2d86e2
No related branches found
No related tags found
No related merge requests found
package encryption
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
......@@ -21,6 +22,24 @@ func Hash(input string, key string) string {
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
// GenerateHashFromObject using HMAC with SHA-256
func GenerateHashFromObject(obejct 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)
if err != nil {
return "", err
}
encodedBody := buf.String()
// Sign encoded body with secret
hashedObject := Hash(encodedBody, secret)
return hashedObject, nil
}
func Md5HashString(bytesToHash []byte) string {
hash := md5.Sum(bytesToHash)
hashString := fmt.Sprintf("%X", hash)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment