-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hashx.Sha256Sum() & hashx.Sha256Hex()
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |