<PackageReference Include="BouncyCastle.Cryptography" Version="2.6.1" />

XofUtilities

class XofUtilities
using Org.BouncyCastle.Utilities; using System; namespace Org.BouncyCastle.Crypto.Digests { internal class XofUtilities { internal static byte[] LeftEncode(long strLen) { byte b = 1; long num = strLen; while ((num >>= 8) != 0) { b = (byte)(b + 1); } byte[] array = new byte[b + 1]; array[0] = b; for (int i = 1; i <= b; i++) { array[i] = (byte)(strLen >> 8 * (b - i)); } return array; } internal static int LeftEncode(long length, Span<byte> lengthEncoding) { byte b = 1; long num = length; while ((num >>= 8) != 0) { b = (byte)(b + 1); } lengthEncoding[0] = b; for (int i = 1; i <= b; i++) { lengthEncoding[i] = (byte)(length >> 8 * (b - i)); } return 1 + b; } internal static byte[] RightEncode(long strLen) { byte b = 1; long num = strLen; while ((num >>= 8) != 0) { b = (byte)(b + 1); } byte[] array = new byte[b + 1]; array[b] = b; for (int i = 0; i < b; i++) { array[i] = (byte)(strLen >> 8 * (b - i - 1)); } return array; } internal static int RightEncode(long length, Span<byte> lengthEncoding) { byte b = 1; long num = length; while ((num >>= 8) != 0) { b = (byte)(b + 1); } lengthEncoding[b] = b; for (int i = 0; i < b; i++) { lengthEncoding[i] = (byte)(length >> 8 * (b - i - 1)); } return b + 1; } internal static byte[] Encode(byte X) { return Arrays.Concatenate(LeftEncode(8), new byte[1] { X }); } internal static byte[] Encode(byte[] inBuf, int inOff, int len) { if (inBuf.Length == len) return Arrays.Concatenate(LeftEncode(len * 8), inBuf); return Arrays.Concatenate(LeftEncode(len * 8), Arrays.CopyOfRange(inBuf, inOff, inOff + len)); } internal unsafe static void EncodeTo(IDigest digest, ReadOnlySpan<byte> buf) { Span<byte> lengthEncoding = new Span<byte>(stackalloc byte[9], 9); int length = LeftEncode(buf.Length * 8, lengthEncoding); digest.BlockUpdate(lengthEncoding.Slice(0, length)); digest.BlockUpdate(buf); } } }