Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
6fa1ee62
Commit
6fa1ee62
authored
2 years ago
by
Johan de Klerk
Browse files
Options
Downloads
Patches
Plain Diff
GenerateHashFromObject
parent
b7dfc89f
Branches
Branches containing commit
Tags
v1.110.0
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
encryption/encryption.go
+7
-4
7 additions, 4 deletions
encryption/encryption.go
with
7 additions
and
4 deletions
encryption/encryption.go
+
7
−
4
View file @
6fa1ee62
...
...
@@ -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
(
ob
e
jct
any
,
secret
string
)
(
string
,
error
)
{
func
GenerateHashFromObject
(
obj
e
ct
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
(
ob
e
jct
)
err
:=
json
.
NewEncoder
(
encoder
)
.
Encode
(
obj
e
ct
)
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
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment