Update to BouncyCastle 1.9.0

This commit is contained in:
BruceChen 2022-08-25 21:29:33 +08:00
parent ed8e97fd2d
commit a3971f9097
9 changed files with 392 additions and 115 deletions

View file

@ -20,11 +20,41 @@ namespace Org.BouncyCastle.Crypto.Utilities
bs[off + 1] = (byte)(n);
}
internal static ushort BE_To_UInt16(byte[] bs)
internal static void UInt16_To_BE(ushort[] ns, byte[] bs, int off)
{
uint n = (uint)bs[0] << 8
| (uint)bs[1];
return (ushort)n;
for (int i = 0; i < ns.Length; ++i)
{
UInt16_To_BE(ns[i], bs, off);
off += 2;
}
}
internal static void UInt16_To_BE(ushort[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
{
for (int i = 0; i < nsLen; ++i)
{
UInt16_To_BE(ns[nsOff + i], bs, bsOff);
bsOff += 2;
}
}
internal static byte[] UInt16_To_BE(ushort n)
{
byte[] bs = new byte[2];
UInt16_To_BE(n, bs, 0);
return bs;
}
internal static byte[] UInt16_To_BE(ushort[] ns)
{
return UInt16_To_BE(ns, 0, ns.Length);
}
internal static byte[] UInt16_To_BE(ushort[] ns, int nsOff, int nsLen)
{
byte[] bs = new byte[2 * nsLen];
UInt16_To_BE(ns, nsOff, nsLen, bs, 0);
return bs;
}
internal static ushort BE_To_UInt16(byte[] bs, int off)
@ -34,11 +64,27 @@ namespace Org.BouncyCastle.Crypto.Utilities
return (ushort)n;
}
internal static byte[] UInt32_To_BE(uint n)
internal static void BE_To_UInt16(byte[] bs, int bsOff, ushort[] ns, int nsOff)
{
byte[] bs = new byte[4];
UInt32_To_BE(n, bs, 0);
return bs;
ns[nsOff] = BE_To_UInt16(bs, bsOff);
}
internal static ushort[] BE_To_UInt16(byte[] bs)
{
return BE_To_UInt16(bs, 0, bs.Length);
}
internal static ushort[] BE_To_UInt16(byte[] bs, int off, int len)
{
if ((len & 1) != 0)
throw new ArgumentException("must be a multiple of 2", "len");
ushort[] ns = new ushort[len / 2];
for (int i = 0; i < len; i += 2)
{
BE_To_UInt16(bs, off + i, ns, i >> 1);
}
return ns;
}
internal static void UInt32_To_BE(uint n, byte[] bs)
@ -57,13 +103,6 @@ namespace Org.BouncyCastle.Crypto.Utilities
bs[off + 3] = (byte)(n);
}
internal static byte[] UInt32_To_BE(uint[] ns)
{
byte[] bs = new byte[4 * ns.Length];
UInt32_To_BE(ns, bs, 0);
return bs;
}
internal static void UInt32_To_BE(uint[] ns, byte[] bs, int off)
{
for (int i = 0; i < ns.Length; ++i)
@ -73,6 +112,29 @@ namespace Org.BouncyCastle.Crypto.Utilities
}
}
internal static void UInt32_To_BE(uint[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
{
for (int i = 0; i < nsLen; ++i)
{
UInt32_To_BE(ns[nsOff + i], bs, bsOff);
bsOff += 4;
}
}
internal static byte[] UInt32_To_BE(uint n)
{
byte[] bs = new byte[4];
UInt32_To_BE(n, bs, 0);
return bs;
}
internal static byte[] UInt32_To_BE(uint[] ns)
{
byte[] bs = new byte[4 * ns.Length];
UInt32_To_BE(ns, bs, 0);
return bs;
}
internal static uint BE_To_UInt32(byte[] bs)
{
return (uint)bs[0] << 24
@ -98,6 +160,15 @@ namespace Org.BouncyCastle.Crypto.Utilities
}
}
internal static void BE_To_UInt32(byte[] bs, int bsOff, uint[] ns, int nsOff, int nsLen)
{
for (int i = 0; i < nsLen; ++i)
{
ns[nsOff + i] = BE_To_UInt32(bs, bsOff);
bsOff += 4;
}
}
internal static byte[] UInt64_To_BE(ulong n)
{
byte[] bs = new byte[8];
@ -117,11 +188,29 @@ namespace Org.BouncyCastle.Crypto.Utilities
UInt32_To_BE((uint)(n), bs, off + 4);
}
internal static ulong BE_To_UInt64(byte[] bs)
internal static byte[] UInt64_To_BE(ulong[] ns)
{
uint hi = BE_To_UInt32(bs);
uint lo = BE_To_UInt32(bs, 4);
return ((ulong)hi << 32) | (ulong)lo;
byte[] bs = new byte[8 * ns.Length];
UInt64_To_BE(ns, bs, 0);
return bs;
}
internal static void UInt64_To_BE(ulong[] ns, byte[] bs, int off)
{
for (int i = 0; i < ns.Length; ++i)
{
UInt64_To_BE(ns[i], bs, off);
off += 8;
}
}
internal static void UInt64_To_BE(ulong[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
{
for (int i = 0; i < nsLen; ++i)
{
UInt64_To_BE(ns[nsOff + i], bs, bsOff);
bsOff += 8;
}
}
internal static ulong BE_To_UInt64(byte[] bs, int off)
@ -131,6 +220,24 @@ namespace Org.BouncyCastle.Crypto.Utilities
return ((ulong)hi << 32) | (ulong)lo;
}
internal static void BE_To_UInt64(byte[] bs, int off, ulong[] ns)
{
for (int i = 0; i < ns.Length; ++i)
{
ns[i] = BE_To_UInt64(bs, off);
off += 8;
}
}
internal static void BE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, int nsLen)
{
for (int i = 0; i < nsLen; ++i)
{
ns[nsOff + i] = BE_To_UInt64(bs, bsOff);
bsOff += 8;
}
}
internal static void UInt16_To_LE(ushort n, byte[] bs)
{
bs[0] = (byte)(n);
@ -196,6 +303,15 @@ namespace Org.BouncyCastle.Crypto.Utilities
}
}
internal static void UInt32_To_LE(uint[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
{
for (int i = 0; i < nsLen; ++i)
{
UInt32_To_LE(ns[nsOff + i], bs, bsOff);
bsOff += 4;
}
}
internal static uint LE_To_UInt32(byte[] bs)
{
return (uint)bs[0]
@ -230,6 +346,17 @@ namespace Org.BouncyCastle.Crypto.Utilities
}
}
internal static uint[] LE_To_UInt32(byte[] bs, int off, int count)
{
uint[] ns = new uint[count];
for (int i = 0; i < ns.Length; ++i)
{
ns[i] = LE_To_UInt32(bs, off);
off += 4;
}
return ns;
}
internal static byte[] UInt64_To_LE(ulong n)
{
byte[] bs = new byte[8];
@ -249,6 +376,31 @@ namespace Org.BouncyCastle.Crypto.Utilities
UInt32_To_LE((uint)(n >> 32), bs, off + 4);
}
internal static byte[] UInt64_To_LE(ulong[] ns)
{
byte[] bs = new byte[8 * ns.Length];
UInt64_To_LE(ns, bs, 0);
return bs;
}
internal static void UInt64_To_LE(ulong[] ns, byte[] bs, int off)
{
for (int i = 0; i < ns.Length; ++i)
{
UInt64_To_LE(ns[i], bs, off);
off += 8;
}
}
internal static void UInt64_To_LE(ulong[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
{
for (int i = 0; i < nsLen; ++i)
{
UInt64_To_LE(ns[nsOff + i], bs, bsOff);
bsOff += 8;
}
}
internal static ulong LE_To_UInt64(byte[] bs)
{
uint lo = LE_To_UInt32(bs);
@ -262,5 +414,23 @@ namespace Org.BouncyCastle.Crypto.Utilities
uint hi = LE_To_UInt32(bs, off + 4);
return ((ulong)hi << 32) | (ulong)lo;
}
internal static void LE_To_UInt64(byte[] bs, int off, ulong[] ns)
{
for (int i = 0; i < ns.Length; ++i)
{
ns[i] = LE_To_UInt64(bs, off);
off += 8;
}
}
internal static void LE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, int nsLen)
{
for (int i = 0; i < nsLen; ++i)
{
ns[nsOff + i] = LE_To_UInt64(bs, bsOff);
bsOff += 8;
}
}
}
}