summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-04-11 18:55:01 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-04-11 18:55:01 +0700
commit767f1c498d07d59c1f98940c0e45db9206a33c30 (patch)
tree1bbf95c542d4f12e8b648dd3e770236dd44de94d
parentReduce allocations in hex encoding (diff)
downloadBouncyCastle.NET-ed25519-767f1c498d07d59c1f98940c0e45db9206a33c30.tar.xz
Support BitString construction from ReadOnlySpan
-rw-r--r--crypto/src/asn1/BERBitString.cs13
-rw-r--r--crypto/src/asn1/DLBitString.cs12
-rw-r--r--crypto/src/asn1/DerBitString.cs17
-rw-r--r--crypto/src/asn1/x509/AltSignatureValue.cs12
-rw-r--r--crypto/src/util/Arrays.cs7
5 files changed, 57 insertions, 4 deletions
diff --git a/crypto/src/asn1/BERBitString.cs b/crypto/src/asn1/BERBitString.cs
index e69e7a35f..98159e1eb 100644
--- a/crypto/src/asn1/BERBitString.cs
+++ b/crypto/src/asn1/BERBitString.cs
@@ -81,6 +81,19 @@ namespace Org.BouncyCastle.Asn1
         {
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public BerBitString(ReadOnlySpan<byte> data)
+            : this(data, 0)
+        {
+        }
+
+        public BerBitString(ReadOnlySpan<byte> data, int padBits)
+            : base(data, padBits)
+        {
+            this.elements = null;
+        }
+#endif
+
         public BerBitString(int namedBits)
             : base(namedBits)
         {
diff --git a/crypto/src/asn1/DLBitString.cs b/crypto/src/asn1/DLBitString.cs
index 5d8f3ac5e..624b8bb56 100644
--- a/crypto/src/asn1/DLBitString.cs
+++ b/crypto/src/asn1/DLBitString.cs
@@ -21,6 +21,18 @@ namespace Org.BouncyCastle.Asn1
         {
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public DLBitString(ReadOnlySpan<byte> data)
+            : this(data, 0)
+        {
+        }
+
+        public DLBitString(ReadOnlySpan<byte> data, int padBits)
+            : base(data, padBits)
+        {
+        }
+#endif
+
         public DLBitString(int namedBits)
             : base(namedBits)
         {
diff --git a/crypto/src/asn1/DerBitString.cs b/crypto/src/asn1/DerBitString.cs
index b929de357..bc3de7fe4 100644
--- a/crypto/src/asn1/DerBitString.cs
+++ b/crypto/src/asn1/DerBitString.cs
@@ -118,6 +118,23 @@ namespace Org.BouncyCastle.Asn1
             this.contents = Arrays.Prepend(data, (byte)padBits);
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public DerBitString(ReadOnlySpan<byte> data)
+            : this(data, 0)
+        {
+        }
+
+        public DerBitString(ReadOnlySpan<byte> data, int padBits)
+        {
+            if (padBits < 0 || padBits > 7)
+                throw new ArgumentException("must be in the range 0 to 7", "padBits");
+            if (data.IsEmpty && padBits != 0)
+                throw new ArgumentException("if 'data' is empty, 'padBits' must be 0");
+
+            this.contents = Arrays.Prepend(data, (byte)padBits);
+        }
+#endif
+
         public DerBitString(int namedBits)
         {
             if (namedBits == 0)
diff --git a/crypto/src/asn1/x509/AltSignatureValue.cs b/crypto/src/asn1/x509/AltSignatureValue.cs
index 498c1d6d0..a46301e33 100644
--- a/crypto/src/asn1/x509/AltSignatureValue.cs
+++ b/crypto/src/asn1/x509/AltSignatureValue.cs
@@ -60,11 +60,15 @@ namespace Org.BouncyCastle.Asn1.X509
             m_signature = new DerBitString(signature);
         }
 
-        public DerBitString Signature => m_signature;
-
-        public override Asn1Object ToAsn1Object()
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public AltSignatureValue(ReadOnlySpan<byte> signature)
         {
-            return m_signature;
+            m_signature = new DerBitString(signature);
         }
+#endif
+
+        public DerBitString Signature => m_signature;
+
+        public override Asn1Object ToAsn1Object() => m_signature;
     }
 }
diff --git a/crypto/src/util/Arrays.cs b/crypto/src/util/Arrays.cs
index da74d467a..83fafb388 100644
--- a/crypto/src/util/Arrays.cs
+++ b/crypto/src/util/Arrays.cs
@@ -1037,6 +1037,13 @@ namespace Org.BouncyCastle.Utilities
             return rv;
         }
 
+        public static T[] Prepend<T>(ReadOnlySpan<T> a, T b)
+        {
+            T[] result = new T[1 + a.Length];
+            result[0] = b;
+            a.CopyTo(result.AsSpan(1));
+            return result;
+        }
 #endif
     }
 }