diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-20 17:36:38 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-20 17:36:38 +0700 |
commit | 5e0744398d2601537d27ad1960533158e0bf03aa (patch) | |
tree | b69b5287273b1b2556045a1bc1d6c6f97bf37c0d /crypto | |
parent | Fix LMS tests namespace (diff) | |
download | BouncyCastle.NET-ed25519-5e0744398d2601537d27ad1960533158e0bf03aa.tar.xz |
Add Shorts utility class
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/src/util/Shorts.cs | 54 |
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))); + } + } +} |