55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
package cryptext
|
|
|
|
import (
|
|
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestStrSha256SameAsBytesSha256(t *testing.T) {
|
|
inputs := []string{"", "a", "Hello World", "lorem ipsum dolor sit amet", "🎉 unicode"}
|
|
for _, in := range inputs {
|
|
tst.AssertEqual(t, StrSha256(in), BytesSha256([]byte(in)))
|
|
}
|
|
}
|
|
|
|
func TestStrSha256Length(t *testing.T) {
|
|
// SHA-256 hex output must always be 64 characters
|
|
tst.AssertEqual(t, len(StrSha256("")), 64)
|
|
tst.AssertEqual(t, len(StrSha256("x")), 64)
|
|
tst.AssertEqual(t, len(StrSha256(strings.Repeat("x", 10000))), 64)
|
|
}
|
|
|
|
func TestStrSha256Deterministic(t *testing.T) {
|
|
v := "deterministic input"
|
|
a := StrSha256(v)
|
|
b := StrSha256(v)
|
|
tst.AssertEqual(t, a, b)
|
|
}
|
|
|
|
func TestStrSha256DifferentInputs(t *testing.T) {
|
|
tst.AssertNotEqual(t, StrSha256("a"), StrSha256("b"))
|
|
tst.AssertNotEqual(t, StrSha256("Hello"), StrSha256("hello"))
|
|
tst.AssertNotEqual(t, StrSha256("Hello World"), StrSha256("Hello World "))
|
|
}
|
|
|
|
func TestStrSha256IsHex(t *testing.T) {
|
|
out := StrSha256("anything")
|
|
for _, c := range out {
|
|
isLowerHex := (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')
|
|
if !isLowerHex {
|
|
t.Errorf("non-hex char %q in StrSha256 output", c)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBytesSha256NilSameAsEmpty(t *testing.T) {
|
|
tst.AssertEqual(t, BytesSha256(nil), BytesSha256([]byte{}))
|
|
}
|
|
|
|
func TestBytesSha256KnownVectors(t *testing.T) {
|
|
// "abc" => sha-256 standard vector
|
|
tst.AssertEqual(t, StrSha256("abc"), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
|
|
}
|