summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-05-12 23:49:03 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-05-12 23:49:03 +0700
commite9ef49c697b94d4afabb6d3e549104c32cc9ac20 (patch)
treed4ff8f94e3cbf868d89584d81fd3b065f66c6e46 /crypto
parentRefactoring in SeedEngine (diff)
downloadBouncyCastle.NET-ed25519-e9ef49c697b94d4afabb6d3e549104c32cc9ac20.tar.xz
Refactoring in HMac
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/crypto/macs/HMac.cs29
1 files changed, 18 insertions, 11 deletions
diff --git a/crypto/src/crypto/macs/HMac.cs b/crypto/src/crypto/macs/HMac.cs
index 03e2212d6..28503f852 100644
--- a/crypto/src/crypto/macs/HMac.cs
+++ b/crypto/src/crypto/macs/HMac.cs
@@ -56,19 +56,25 @@ namespace Org.BouncyCastle.Crypto.Macs
         {
             digest.Reset();
 
-            byte[] key = ((KeyParameter)parameters).GetKey();
-			int keyLength = key.Length;
+            KeyParameter keyParameter = (KeyParameter)parameters;
 
+            int keyLength = keyParameter.KeyLength;
             if (keyLength > blockLength)
             {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                digest.BlockUpdate(keyParameter.Key);
+#else
+                byte[] key = keyParameter.GetKey();
                 digest.BlockUpdate(key, 0, keyLength);
+#endif
+
                 digest.DoFinal(inputPad, 0);
 
 				keyLength = digestSize;
             }
             else
             {
-				Array.Copy(key, 0, inputPad, 0, keyLength);
+                keyParameter.CopyTo(inputPad, 0, keyLength);
             }
 
 			Array.Clear(inputPad, keyLength, blockLength - keyLength);
@@ -77,19 +83,20 @@ namespace Org.BouncyCastle.Crypto.Macs
 			XorPad(inputPad, blockLength, IPAD);
             XorPad(outputBuf, blockLength, OPAD);
 
-			if (digest is IMemoable)
+			if (digest is IMemoable memoable)
 			{
-				opadState = ((IMemoable)digest).Copy();
+				opadState = memoable.Copy();
 
 				((IDigest)opadState).BlockUpdate(outputBuf, 0, blockLength);
-			}
 
-			digest.BlockUpdate(inputPad, 0, inputPad.Length);
+			    digest.BlockUpdate(inputPad, 0, inputPad.Length);
 
-			if (digest is IMemoable)
-			{
-				ipadState = ((IMemoable)digest).Copy();
-			}
+				ipadState = memoable.Copy();
+            }
+            else
+            {
+                digest.BlockUpdate(inputPad, 0, inputPad.Length);
+            }
         }
 
         public virtual int GetMacSize()