Skip to content

Commit

Permalink
Add hashx.Sha256Sum() & hashx.Sha256Hex()
Browse files Browse the repository at this point in the history
  • Loading branch information
taobig committed Jul 15, 2024
1 parent 9ada4ad commit 866131d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
23 changes: 23 additions & 0 deletions hashx/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package hashx

import (
"crypto/sha256"
"encoding/hex"
)

func Sha256Sum(input ...[]byte) []byte {
h := sha256.New()
for _, v := range input {
h.Write(v)
}
return h.Sum(nil)
}

func Sha256Hex(input ...[]byte) string {
h := sha256.New()
for _, v := range input {
h.Write(v)
}
hash := h.Sum(nil)
return hex.EncodeToString(hash)
}
22 changes: 22 additions & 0 deletions hashx/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package hashx

import (
"encoding/hex"
"github.com/stretchr/testify/require"
"testing"
)

func TestSha256Sum(t *testing.T) {
t.Parallel()

hash := Sha256Sum([]byte("hello"))
hexHash := hex.EncodeToString(hash)
require.Equal(t, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", hexHash)
}

func TestSha256Hex(t *testing.T) {
t.Parallel()

hexHash := Sha256Hex([]byte("hello"))
require.Equal(t, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", hexHash)
}

0 comments on commit 866131d

Please sign in to comment.