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

Codec

static class Codec
using System; using System.Buffers.Binary; namespace Org.BouncyCastle.Math.EC.Rfc8032 { internal static class Codec { internal static uint Decode16(byte[] bs, int off) { return BinaryPrimitives.ReadUInt16LittleEndian(bs.AsSpan(off)); } internal static uint Decode16(ReadOnlySpan<byte> bs) { return BinaryPrimitives.ReadUInt16LittleEndian(bs); } internal static uint Decode24(byte[] bs, int off) { return (uint)(bs[off] | (bs[++off] << 8) | (bs[++off] << 16)); } internal static uint Decode24(ReadOnlySpan<byte> bs) { return (uint)(bs[0] | (bs[1] << 8) | (bs[2] << 16)); } internal static uint Decode32(byte[] bs, int off) { return BinaryPrimitives.ReadUInt32LittleEndian(bs.AsSpan(off)); } internal static uint Decode32(ReadOnlySpan<byte> bs) { return BinaryPrimitives.ReadUInt32LittleEndian(bs); } internal static void Decode32(byte[] bs, int bsOff, uint[] n, int nOff, int nLen) { for (int i = 0; i < nLen; i++) { n[nOff + i] = Decode32(bs, bsOff + i * 4); } } internal static void Decode32(ReadOnlySpan<byte> bs, Span<uint> n) { for (int i = 0; i < n.Length; i++) { ref uint reference = ref n[i]; int num = i * 4; reference = Decode32(bs.Slice(num, bs.Length - num)); } } internal static void Encode24(uint n, byte[] bs, int off) { bs[off] = (byte)n; bs[++off] = (byte)(n >> 8); bs[++off] = (byte)(n >> 16); } internal static void Encode24(uint n, Span<byte> bs) { bs[0] = (byte)n; bs[1] = (byte)(n >> 8); bs[2] = (byte)(n >> 16); } internal static void Encode32(uint n, byte[] bs, int off) { BinaryPrimitives.WriteUInt32LittleEndian(bs.AsSpan(off), n); } internal static void Encode32(uint n, Span<byte> bs) { BinaryPrimitives.WriteUInt32LittleEndian(bs, n); } internal static void Encode32(uint[] n, int nOff, int nLen, byte[] bs, int bsOff) { for (int i = 0; i < nLen; i++) { Encode32(n[nOff + i], bs, bsOff + i * 4); } } internal static void Encode32(ReadOnlySpan<uint> n, Span<byte> bs) { for (int i = 0; i < n.Length; i++) { uint n2 = n[i]; int num = i * 4; Encode32(n2, bs.Slice(num, bs.Length - num)); } } internal static void Encode56(ulong n, byte[] bs, int off) { Encode32((uint)n, bs, off); Encode24((uint)(n >> 32), bs, off + 4); } internal static void Encode56(ulong n, Span<byte> bs) { Encode32((uint)n, bs); Encode24((uint)(n >> 32), bs.Slice(4, bs.Length - 4)); } } }