summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-10-04 23:48:39 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-10-04 23:48:39 +0700
commit14b2114e9fe9b89000753898d03387a76b415115 (patch)
treeb12cd5a649f06f65d22ec63ccc42b75843fcc901 /crypto
parentSave heap allocations (diff)
downloadBouncyCastle.NET-ed25519-14b2114e9fe9b89000753898d03387a76b415115.tar.xz
Span-based constructors in parameter classes
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/crypto/parameters/KeyParameter.cs36
-rw-r--r--crypto/src/crypto/parameters/ParametersWithID.cs27
-rw-r--r--crypto/src/crypto/parameters/ParametersWithIV.cs31
-rw-r--r--crypto/src/crypto/parameters/ParametersWithRandom.cs37
-rw-r--r--crypto/src/crypto/parameters/ParametersWithSalt.cs33
5 files changed, 81 insertions, 83 deletions
diff --git a/crypto/src/crypto/parameters/KeyParameter.cs b/crypto/src/crypto/parameters/KeyParameter.cs
index 043adf276..bc6c28368 100644
--- a/crypto/src/crypto/parameters/KeyParameter.cs
+++ b/crypto/src/crypto/parameters/KeyParameter.cs
@@ -7,37 +7,39 @@ namespace Org.BouncyCastle.Crypto.Parameters
     public class KeyParameter
 		: ICipherParameters
     {
-        private readonly byte[] key;
+        private readonly byte[] m_key;
 
-		public KeyParameter(
-			byte[] key)
+		public KeyParameter(byte[] key)
 		{
 			if (key == null)
-				throw new ArgumentNullException("key");
+				throw new ArgumentNullException(nameof(key));
 
-			this.key = (byte[]) key.Clone();
+			m_key = (byte[])key.Clone();
 		}
 
-		public KeyParameter(
-            byte[]	key,
-            int		keyOff,
-            int		keyLen)
+		public KeyParameter(byte[] key, int keyOff, int keyLen)
         {
 			if (key == null)
-				throw new ArgumentNullException("key");
+				throw new ArgumentNullException(nameof(key));
 			if (keyOff < 0 || keyOff > key.Length)
-				throw new ArgumentOutOfRangeException("keyOff");
+				throw new ArgumentOutOfRangeException(nameof(keyOff));
             if (keyLen < 0 || keyLen > (key.Length - keyOff))
-				throw new ArgumentOutOfRangeException("keyLen");
+				throw new ArgumentOutOfRangeException(nameof(keyLen));
 
-			this.key = new byte[keyLen];
-            Array.Copy(key, keyOff, this.key, 0, keyLen);
+			m_key = new byte[keyLen];
+            Array.Copy(key, keyOff, m_key, 0, keyLen);
         }
 
-		public byte[] GetKey()
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public KeyParameter(ReadOnlySpan<byte> key)
         {
-			return (byte[]) key.Clone();
+            m_key = key.ToArray();
         }
-    }
+#endif
 
+        public byte[] GetKey()
+        {
+			return (byte[])m_key.Clone();
+        }
+    }
 }
diff --git a/crypto/src/crypto/parameters/ParametersWithID.cs b/crypto/src/crypto/parameters/ParametersWithID.cs
index 37f68705b..2bc4ac86c 100644
--- a/crypto/src/crypto/parameters/ParametersWithID.cs
+++ b/crypto/src/crypto/parameters/ParametersWithID.cs
@@ -7,30 +7,33 @@ namespace Org.BouncyCastle.Crypto.Parameters
     public class ParametersWithID
         : ICipherParameters
     {
-        private readonly ICipherParameters parameters;
-        private readonly byte[] id;
+        private readonly ICipherParameters m_parameters;
+        private readonly byte[] m_id;
 
-        public ParametersWithID(ICipherParameters parameters,
-            byte[] id)
+        public ParametersWithID(ICipherParameters parameters, byte[] id)
             : this(parameters, id, 0, id.Length)
         {
         }
 
-        public ParametersWithID(ICipherParameters parameters,
-            byte[] id, int idOff, int idLen)
+        public ParametersWithID(ICipherParameters parameters, byte[] id, int idOff, int idLen)
         {
-            this.parameters = parameters;
-            this.id = Arrays.CopyOfRange(id, idOff, idOff + idLen);
+            m_parameters = parameters;
+            m_id = Arrays.CopyOfRange(id, idOff, idOff + idLen);
         }
 
-        public byte[] GetID()
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public ParametersWithID(ICipherParameters parameters, ReadOnlySpan<byte> id)
         {
-            return id;
+            m_parameters = parameters;
+            m_id = id.ToArray();
         }
+#endif
 
-        public ICipherParameters Parameters
+        public byte[] GetID()
         {
-            get { return parameters; }
+            return m_id;
         }
+
+        public ICipherParameters Parameters => m_parameters;
     }
 }
diff --git a/crypto/src/crypto/parameters/ParametersWithIV.cs b/crypto/src/crypto/parameters/ParametersWithIV.cs
index 4b2eb930f..ac55afc8d 100644
--- a/crypto/src/crypto/parameters/ParametersWithIV.cs
+++ b/crypto/src/crypto/parameters/ParametersWithIV.cs
@@ -7,34 +7,33 @@ namespace Org.BouncyCastle.Crypto.Parameters
     public class ParametersWithIV
         : ICipherParameters
     {
-        private readonly ICipherParameters parameters;
-        private readonly byte[] iv;
+        private readonly ICipherParameters m_parameters;
+        private readonly byte[] m_iv;
 
-        public ParametersWithIV(ICipherParameters parameters,
-            byte[] iv)
+        public ParametersWithIV(ICipherParameters parameters, byte[] iv)
             : this(parameters, iv, 0, iv.Length)
         {
         }
 
-        public ParametersWithIV(ICipherParameters parameters,
-            byte[] iv, int ivOff, int ivLen)
+        public ParametersWithIV(ICipherParameters parameters, byte[] iv, int ivOff, int ivLen)
         {
-            // NOTE: 'parameters' may be null to imply key re-use
-            if (iv == null)
-                throw new ArgumentNullException("iv");
-
-            this.parameters = parameters;
-            this.iv = Arrays.CopyOfRange(iv, ivOff, ivOff + ivLen);
+            m_parameters = parameters;
+            m_iv = Arrays.CopyOfRange(iv, ivOff, ivOff + ivLen);
         }
 
-        public byte[] GetIV()
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public ParametersWithIV(ICipherParameters parameters, ReadOnlySpan<byte> iv)
         {
-            return (byte[])iv.Clone();
+            m_parameters = parameters;
+            m_iv = iv.ToArray();
         }
+#endif
 
-        public ICipherParameters Parameters
+        public byte[] GetIV()
         {
-            get { return parameters; }
+            return (byte[])m_iv.Clone();
         }
+
+        public ICipherParameters Parameters => m_parameters;
     }
 }
diff --git a/crypto/src/crypto/parameters/ParametersWithRandom.cs b/crypto/src/crypto/parameters/ParametersWithRandom.cs
index e19c9125f..e20ddd7ce 100644
--- a/crypto/src/crypto/parameters/ParametersWithRandom.cs
+++ b/crypto/src/crypto/parameters/ParametersWithRandom.cs
@@ -7,36 +7,27 @@ namespace Org.BouncyCastle.Crypto.Parameters
     public class ParametersWithRandom
 		: ICipherParameters
     {
-        private readonly ICipherParameters	parameters;
-		private readonly SecureRandom		random;
+        private readonly ICipherParameters m_parameters;
+		private readonly SecureRandom m_random;
 
-		public ParametersWithRandom(
-            ICipherParameters	parameters,
-            SecureRandom		random)
+        public ParametersWithRandom(ICipherParameters parameters)
+            : this(parameters, new SecureRandom())
+        {
+        }
+
+        public ParametersWithRandom(ICipherParameters parameters, SecureRandom random)
         {
 			if (parameters == null)
-				throw new ArgumentNullException("parameters");
+				throw new ArgumentNullException(nameof(parameters));
 			if (random == null)
-				throw new ArgumentNullException("random");
-
-			this.parameters = parameters;
-			this.random = random;
-		}
+				throw new ArgumentNullException(nameof(random));
 
-		public ParametersWithRandom(
-            ICipherParameters parameters)
-			: this(parameters, new SecureRandom())
-        {
+			m_parameters = parameters;
+			m_random = random;
 		}
 
-		public SecureRandom Random
-        {
-			get { return random; }
-        }
+        public ICipherParameters Parameters => m_parameters;
 
-		public ICipherParameters Parameters
-        {
-            get { return parameters; }
-        }
+        public SecureRandom Random => m_random;
     }
 }
diff --git a/crypto/src/crypto/parameters/ParametersWithSalt.cs b/crypto/src/crypto/parameters/ParametersWithSalt.cs
index 7f4cd6cd1..277cd213c 100644
--- a/crypto/src/crypto/parameters/ParametersWithSalt.cs
+++ b/crypto/src/crypto/parameters/ParametersWithSalt.cs
@@ -1,39 +1,42 @@
 using System;
 
 using Org.BouncyCastle.Crypto;
+using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Crypto.Parameters
 {
 
     /// <summary> Cipher parameters with a fixed salt value associated with them.</summary>
-    public class ParametersWithSalt : ICipherParameters
+    public class ParametersWithSalt
+        : ICipherParameters
     {
-        private byte[] salt;
-        private ICipherParameters parameters;
+        private readonly ICipherParameters m_parameters;
+        private readonly byte[] m_salt;
 
-        public ParametersWithSalt(ICipherParameters parameters, byte[] salt):this(parameters, salt, 0, salt.Length)
+        public ParametersWithSalt(ICipherParameters parameters, byte[] salt)
+            : this(parameters, salt, 0, salt.Length)
         {
         }
 
         public ParametersWithSalt(ICipherParameters parameters, byte[] salt, int saltOff, int saltLen)
         {
-            this.salt = new byte[saltLen];
-            this.parameters = parameters;
-
-            Array.Copy(salt, saltOff, this.salt, 0, saltLen);
+            m_parameters = parameters;
+            m_salt = Arrays.CopyOfRange(salt, saltOff, saltOff + saltLen);
         }
 
-        public byte[] GetSalt()
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public ParametersWithSalt(ICipherParameters parameters, ReadOnlySpan<byte> salt)
         {
-            return salt;
+            m_parameters = parameters;
+            m_salt = salt.ToArray();
         }
+#endif
 
-        public ICipherParameters Parameters
+        public byte[] GetSalt()
         {
-            get
-            {
-                return parameters;
-            }
+            return m_salt;
         }
+
+        public ICipherParameters Parameters => m_parameters;
     }
 }