summary refs log tree commit diff
path: root/crypto/src/util/Shorts.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/util/Shorts.cs')
-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)));
+        }
+    }
+}