Skip to content

Commit 6fef1f2

Browse files
committed
Fix HMAC usage
1 parent b861f56 commit 6fef1f2

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

source/sha3d.d

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ public struct KECCAK(uint digestSize, uint shake = 0)
6060

6161
@safe @nogc pure nothrow:
6262

63+
enum blockSize = (1600 - digestSize * 2); /// Digest size in bits
64+
6365
// ...0: Reserved
6466
// 01: SHA-3
6567
// ...11: RawSHAKE
6668
// 1111: SHAKE
6769
private enum delim = shake ? 0x1f : 0x06; /// Delimiter suffix when finishing
68-
private enum rate = (1600 - digestSize * 2) / 8; /// Sponge rate in bytes
70+
private enum rate = blockSize / 8; /// Sponge rate in bytes
6971
private enum stateSize = 200;
7072
private enum stateSt64Size = stateSize / 8;
7173
private enum stateStzSize = stateSize / size_t.sizeof;
@@ -297,6 +299,8 @@ private:
297299
}
298300
}
299301

302+
// Unittest based on https://www.di-mgt.com.au/sha_testvectors.html
303+
300304
/// Test against empty datasets
301305
@safe unittest
302306
{
@@ -596,6 +600,21 @@ public alias SHAKE256Digest = WrapperDigest!SHAKE256;
596600
hexString!("46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f"));
597601
}
598602

603+
/// Testing with HMAC
604+
@system unittest
605+
{
606+
import std.ascii : LetterCase;
607+
import std.string : representation;
608+
import std.digest.hmac : hmac;
609+
610+
auto secret = "secret".representation;
611+
assert("The quick brown fox jumps over the lazy dog"
612+
.representation
613+
.hmac!SHA3_256(secret)
614+
.toHexString!(LetterCase.lower) ==
615+
"93379fab68fae6d0fde0c816ea8a49fbd3c80f136c6af08bc61df5268d01b4d8");
616+
}
617+
599618
/// Testing out various SHAKE XOFs.
600619
@system unittest
601620
{

0 commit comments

Comments
 (0)