From afc67c87e25638f19676491af9471b37a37d43a7 Mon Sep 17 00:00:00 2001 From: Kevin Jones Date: Wed, 15 Jul 2020 16:50:49 -0400 Subject: [PATCH] Make CreateHMAC(ReadOnlySpan key) public. --- .../ref/System.Security.Cryptography.Algorithms.cs | 1 + .../Security/Cryptography/IncrementalHash.cs | 24 +++++++++++++++++++++- .../tests/IncrementalHashTests.cs | 15 +++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.cs b/src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.cs index 339a77a..3ed7662 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.cs @@ -470,6 +470,7 @@ namespace System.Security.Cryptography public void AppendData(System.ReadOnlySpan data) { } public static System.Security.Cryptography.IncrementalHash CreateHash(System.Security.Cryptography.HashAlgorithmName hashAlgorithm) { throw null; } public static System.Security.Cryptography.IncrementalHash CreateHMAC(System.Security.Cryptography.HashAlgorithmName hashAlgorithm, byte[] key) { throw null; } + public static System.Security.Cryptography.IncrementalHash CreateHMAC(System.Security.Cryptography.HashAlgorithmName hashAlgorithm, System.ReadOnlySpan key) { throw null; } public void Dispose() { } public byte[] GetCurrentHash() { throw null; } public int GetCurrentHash(System.Span destination) { throw null; } diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/IncrementalHash.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/IncrementalHash.cs index 5dadb52..acf433e 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/IncrementalHash.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/IncrementalHash.cs @@ -354,7 +354,29 @@ namespace System.Security.Cryptography return CreateHMAC(hashAlgorithm, (ReadOnlySpan)key); } - internal static IncrementalHash CreateHMAC(HashAlgorithmName hashAlgorithm, ReadOnlySpan key) + /// + /// Create an for the Hash-based Message Authentication Code (HMAC) + /// algorithm utilizing the hash algorithm specified by , and a + /// key specified by . + /// + /// The name of the hash algorithm to perform within the HMAC. + /// + /// The secret key for the HMAC. The key can be any length, but a key longer than the output size + /// of the hash algorithm specified by will be hashed (using the + /// algorithm specified by ) to derive a correctly-sized key. Therefore, + /// the recommended size of the secret key is the output size of the hash specified by + /// . + /// + /// + /// An instance ready to compute the hash algorithm specified + /// by . + /// + /// + /// . is null, or + /// the empty string. + /// + /// is not a known hash algorithm. + public static IncrementalHash CreateHMAC(HashAlgorithmName hashAlgorithm, ReadOnlySpan key) { if (string.IsNullOrEmpty(hashAlgorithm.Name)) throw new ArgumentException(SR.Cryptography_HashAlgorithmNameNullOrEmpty, nameof(hashAlgorithm)); diff --git a/src/libraries/System.Security.Cryptography.Algorithms/tests/IncrementalHashTests.cs b/src/libraries/System.Security.Cryptography.Algorithms/tests/IncrementalHashTests.cs index 8b2b343..4b876c3 100644 --- a/src/libraries/System.Security.Cryptography.Algorithms/tests/IncrementalHashTests.cs +++ b/src/libraries/System.Security.Cryptography.Algorithms/tests/IncrementalHashTests.cs @@ -87,6 +87,19 @@ namespace System.Security.Cryptography.Algorithms.Tests } } + [Theory] + [MemberData(nameof(GetHMACs))] + public static void VerifyIncrementalHMAC_SpanKey(HMAC referenceAlgorithm, HashAlgorithmName hashAlgorithm) + { + using (referenceAlgorithm) + using (IncrementalHash incrementalHash = IncrementalHash.CreateHMAC(hashAlgorithm, new ReadOnlySpan(s_hmacKey))) + { + referenceAlgorithm.Key = s_hmacKey; + + VerifyIncrementalResult(referenceAlgorithm, incrementalHash); + } + } + private static void VerifyIncrementalResult(HashAlgorithm referenceAlgorithm, IncrementalHash incrementalHash) { byte[] referenceHash = referenceAlgorithm.ComputeHash(s_inputBytes); @@ -471,7 +484,7 @@ namespace System.Security.Cryptography.Algorithms.Tests public static void VerifyGetCurrentHash_Digest(HashAlgorithm referenceAlgorithm, HashAlgorithmName hashAlgorithm) { referenceAlgorithm.Dispose(); - + using (IncrementalHash single = IncrementalHash.CreateHash(hashAlgorithm)) using (IncrementalHash accumulated = IncrementalHash.CreateHash(hashAlgorithm)) { -- 2.7.4