From eadd6510088f87b012361bfc80332cafd5af7615 Mon Sep 17 00:00:00 2001 From: siddharth Date: Sat, 5 Mar 2022 16:20:14 -0500 Subject: lib: add sha256 implementation --- lib/sha256_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lib/sha256_test.go (limited to 'lib/sha256_test.go') diff --git a/lib/sha256_test.go b/lib/sha256_test.go new file mode 100644 index 0000000..ca429fd --- /dev/null +++ b/lib/sha256_test.go @@ -0,0 +1,31 @@ +// Copyright © 2022 siddharth +// SPDX-License-Identifier: ISC + +package lib + +import "testing" + +// Tests from +// https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA256.pdf +func TestSha256Hash(t *testing.T) { + sha256 := Sha256{} + sha256.Init([]uint32{}) + + // Test 1 + m := "abc" + sha256.Message(StrToBytes(m)) + h := sha256.Hash() + e := "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" + if BytesToHexStr(h) != e { + t.Errorf("sha256 test 1 failed: %x != %s\n", h, e) + } + + // Test 2 + m = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + sha256.Message(StrToBytes(m)) + h = sha256.Hash() + e = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" + if BytesToHexStr(h) != e { + t.Errorf("sha256 test 1 failed: %x != %s\n", h, e) + } +} -- cgit v1.2.3