diff options
Diffstat (limited to 'internal/cryptoutil')
-rw-r--r-- | internal/cryptoutil/sha.go (renamed from internal/cryptoutil/sha1.go) | 8 | ||||
-rw-r--r-- | internal/cryptoutil/sha_test.go (renamed from internal/cryptoutil/sha1_test.go) | 16 |
2 files changed, 24 insertions, 0 deletions
diff --git a/internal/cryptoutil/sha1.go b/internal/cryptoutil/sha.go index 38133960..d829d526 100644 --- a/internal/cryptoutil/sha1.go +++ b/internal/cryptoutil/sha.go @@ -6,6 +6,7 @@ package cryptoutil import ( "crypto/sha1" + "crypto/sha256" "encoding/hex" ) @@ -15,3 +16,10 @@ func SHA1(str string) string { _, _ = h.Write([]byte(str)) return hex.EncodeToString(h.Sum(nil)) } + +// SHA256 encodes string to hexadecimal of SHA256 checksum. +func SHA256(str string) string { + h := sha256.New() + _, _ = h.Write([]byte(str)) + return hex.EncodeToString(h.Sum(nil)) +} diff --git a/internal/cryptoutil/sha1_test.go b/internal/cryptoutil/sha_test.go index c9795c98..a3cbf484 100644 --- a/internal/cryptoutil/sha1_test.go +++ b/internal/cryptoutil/sha_test.go @@ -25,3 +25,19 @@ func TestSHA1(t *testing.T) { }) } } + +func TestSHA256(t *testing.T) { + tests := []struct { + input string + output string + }{ + {input: "", output: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}, + {input: "The quick brown fox jumps over the lazy dog", output: "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"}, + {input: "The quick brown fox jumps over the lazy dog.", output: "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c"}, + } + for _, test := range tests { + t.Run(test.input, func(t *testing.T) { + assert.Equal(t, test.output, SHA256(test.input)) + }) + } +} |