summary refs log tree commit diff
path: root/crypto/src/util/Strings.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-12-21 12:34:49 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-12-21 12:34:49 +0700
commit3c508655db514af6702bb51be63dc0b3d176e11b (patch)
tree2ae0176a5616ecc1b0b30a85a29f2805dc4b5241 /crypto/src/util/Strings.cs
parentAesWrap: update from bc-java (diff)
downloadBouncyCastle.NET-ed25519-3c508655db514af6702bb51be63dc0b3d176e11b.tar.xz
Span-based alternatives to char[]
Diffstat (limited to '')
-rw-r--r--crypto/src/util/Strings.cs22
1 files changed, 22 insertions, 0 deletions
diff --git a/crypto/src/util/Strings.cs b/crypto/src/util/Strings.cs
index 12eafd21e..29a95a07e 100644
--- a/crypto/src/util/Strings.cs
+++ b/crypto/src/util/Strings.cs
@@ -46,6 +46,18 @@ namespace Org.BouncyCastle.Utilities
             return bs;
         }
 
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static byte[] ToByteArray(ReadOnlySpan<char> cs)
+        {
+            byte[] bs = new byte[cs.Length];
+            for (int i = 0; i < bs.Length; ++i)
+            {
+                bs[i] = Convert.ToByte(cs[i]);
+            }
+            return bs;
+        }
+#endif
+
         public static string FromAsciiByteArray(byte[] bytes)
         {
             return Encoding.ASCII.GetString(bytes);
@@ -75,5 +87,15 @@ namespace Org.BouncyCastle.Utilities
         {
             return Encoding.UTF8.GetBytes(s);
         }
+
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public static byte[] ToUtf8ByteArray(ReadOnlySpan<char> cs)
+        {
+            int count = Encoding.UTF8.GetByteCount(cs);
+            byte[] bytes = new byte[count];
+            Encoding.UTF8.GetBytes(cs, bytes);
+            return bytes;
+        }
+#endif
     }
 }