summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-10-20 17:36:38 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-10-20 17:36:38 +0700
commit5e0744398d2601537d27ad1960533158e0bf03aa (patch)
treeb69b5287273b1b2556045a1bc1d6c6f97bf37c0d
parentFix LMS tests namespace (diff)
downloadBouncyCastle.NET-ed25519-5e0744398d2601537d27ad1960533158e0bf03aa.tar.xz
Add Shorts utility class
-rw-r--r--crypto/src/util/Shorts.cs54
1 files changed, 54 insertions, 0 deletions
diff --git a/crypto/src/util/Shorts.cs b/crypto/src/util/Shorts.cs
new file mode 100644
index 000000000..eb9f13fa1
--- /dev/null
+++ b/crypto/src/util/Shorts.cs
@@ -0,0 +1,54 @@
+using System;
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+using System.Buffers.Binary;
+#endif
+
+namespace Org.BouncyCastle.Utilities
+{
+    public static class Shorts
+    {
+        public const int NumBits = 16;
+        public const int NumBytes = 2;
+
+        public static short ReverseBytes(short i)
+        {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            return BinaryPrimitives.ReverseEndianness(i);
+#else
+            return RotateLeft(i, 8);
+#endif
+        }
+
+        [CLSCompliant(false)]
+        public static ushort ReverseBytes(ushort i)
+        {
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            return BinaryPrimitives.ReverseEndianness(i);
+#else
+            return RotateLeft(i, 8);
+#endif
+        }
+
+        public static short RotateLeft(short i, int distance)
+        {
+            return (short)RotateLeft((ushort)i, distance);
+        }
+
+        [CLSCompliant(false)]
+        public static ushort RotateLeft(ushort i, int distance)
+        {
+            return (ushort)((i << distance) | (i >> (16 - distance)));
+        }
+
+        public static short RotateRight(short i, int distance)
+        {
+            return (short)RotateRight((ushort)i, distance);
+        }
+
+        [CLSCompliant(false)]
+        public static ushort RotateRight(ushort i, int distance)
+        {
+            return (ushort)((i >> distance) | (i << (16 - distance)));
+        }
+    }
+}