init commit

This commit is contained in:
2025-10-09 09:57:24 +09:00
commit 4d551bd74f
6636 changed files with 1218703 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders;
public sealed class Base64
{
private Base64()
{
}
public static string ToBase64String(byte[] data)
{
return Convert.ToBase64String(data, 0, data.Length);
}
public static string ToBase64String(byte[] data, int off, int length)
{
return Convert.ToBase64String(data, off, length);
}
public static byte[] Encode(byte[] data)
{
return Encode(data, 0, data.Length);
}
public static byte[] Encode(byte[] data, int off, int length)
{
string s = Convert.ToBase64String(data, off, length);
return Strings.ToAsciiByteArray(s);
}
public static int Encode(byte[] data, Stream outStream)
{
byte[] array = Encode(data);
outStream.Write(array, 0, array.Length);
return array.Length;
}
public static int Encode(byte[] data, int off, int length, Stream outStream)
{
byte[] array = Encode(data, off, length);
outStream.Write(array, 0, array.Length);
return array.Length;
}
public static byte[] Decode(byte[] data)
{
string s = Strings.FromAsciiByteArray(data);
return Convert.FromBase64String(s);
}
public static byte[] Decode(string data)
{
return Convert.FromBase64String(data);
}
public static int Decode(string data, Stream outStream)
{
byte[] array = Decode(data);
outStream.Write(array, 0, array.Length);
return array.Length;
}
}

View File

@@ -0,0 +1,212 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders;
public class Base64Encoder : IEncoder
{
protected readonly byte[] encodingTable = new byte[64]
{
65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 97, 98, 99, 100,
101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 43, 47
};
protected byte padding = 61;
protected readonly byte[] decodingTable = new byte[128];
protected void InitialiseDecodingTable()
{
Arrays.Fill(decodingTable, byte.MaxValue);
for (int i = 0; i < encodingTable.Length; i++)
{
decodingTable[encodingTable[i]] = (byte)i;
}
}
public Base64Encoder()
{
InitialiseDecodingTable();
}
public int Encode(byte[] data, int off, int length, Stream outStream)
{
int num = length % 3;
int num2 = length - num;
for (int i = off; i < off + num2; i += 3)
{
int num3 = data[i] & 0xFF;
int num4 = data[i + 1] & 0xFF;
int num5 = data[i + 2] & 0xFF;
outStream.WriteByte(encodingTable[(num3 >>> 2) & 0x3F]);
outStream.WriteByte(encodingTable[((num3 << 4) | (num4 >>> 4)) & 0x3F]);
outStream.WriteByte(encodingTable[((num4 << 2) | (num5 >>> 6)) & 0x3F]);
outStream.WriteByte(encodingTable[num5 & 0x3F]);
}
switch (num)
{
case 1:
{
int num6 = data[off + num2] & 0xFF;
int num8 = (num6 >> 2) & 0x3F;
int num9 = (num6 << 4) & 0x3F;
outStream.WriteByte(encodingTable[num8]);
outStream.WriteByte(encodingTable[num9]);
outStream.WriteByte(padding);
outStream.WriteByte(padding);
break;
}
case 2:
{
int num6 = data[off + num2] & 0xFF;
int num7 = data[off + num2 + 1] & 0xFF;
int num8 = (num6 >> 2) & 0x3F;
int num9 = ((num6 << 4) | (num7 >> 4)) & 0x3F;
int num10 = (num7 << 2) & 0x3F;
outStream.WriteByte(encodingTable[num8]);
outStream.WriteByte(encodingTable[num9]);
outStream.WriteByte(encodingTable[num10]);
outStream.WriteByte(padding);
break;
}
}
return num2 / 3 * 4 + ((num != 0) ? 4 : 0);
}
private bool ignore(char c)
{
if (c != '\n' && c != '\r' && c != '\t')
{
return c == ' ';
}
return true;
}
public int Decode(byte[] data, int off, int length, Stream outStream)
{
int num = 0;
int num2 = off + length;
while (num2 > off && ignore((char)data[num2 - 1]))
{
num2--;
}
int i = off;
int num3 = num2 - 4;
for (i = nextI(data, i, num3); i < num3; i = nextI(data, i, num3))
{
byte b = decodingTable[data[i++]];
i = nextI(data, i, num3);
byte b2 = decodingTable[data[i++]];
i = nextI(data, i, num3);
byte b3 = decodingTable[data[i++]];
i = nextI(data, i, num3);
byte b4 = decodingTable[data[i++]];
if ((b | b2 | b3 | b4) >= 128)
{
throw new IOException("invalid characters encountered in base64 data");
}
outStream.WriteByte((byte)((b << 2) | (b2 >> 4)));
outStream.WriteByte((byte)((b2 << 4) | (b3 >> 2)));
outStream.WriteByte((byte)((b3 << 6) | b4));
num += 3;
}
return num + decodeLastBlock(outStream, (char)data[num2 - 4], (char)data[num2 - 3], (char)data[num2 - 2], (char)data[num2 - 1]);
}
private int nextI(byte[] data, int i, int finish)
{
while (i < finish && ignore((char)data[i]))
{
i++;
}
return i;
}
public int DecodeString(string data, Stream outStream)
{
int num = 0;
int num2 = data.Length;
while (num2 > 0 && ignore(data[num2 - 1]))
{
num2--;
}
int i = 0;
int num3 = num2 - 4;
for (i = nextI(data, i, num3); i < num3; i = nextI(data, i, num3))
{
byte b = decodingTable[(uint)data[i++]];
i = nextI(data, i, num3);
byte b2 = decodingTable[(uint)data[i++]];
i = nextI(data, i, num3);
byte b3 = decodingTable[(uint)data[i++]];
i = nextI(data, i, num3);
byte b4 = decodingTable[(uint)data[i++]];
if ((b | b2 | b3 | b4) >= 128)
{
throw new IOException("invalid characters encountered in base64 data");
}
outStream.WriteByte((byte)((b << 2) | (b2 >> 4)));
outStream.WriteByte((byte)((b2 << 4) | (b3 >> 2)));
outStream.WriteByte((byte)((b3 << 6) | b4));
num += 3;
}
return num + decodeLastBlock(outStream, data[num2 - 4], data[num2 - 3], data[num2 - 2], data[num2 - 1]);
}
private int decodeLastBlock(Stream outStream, char c1, char c2, char c3, char c4)
{
if (c3 == padding)
{
if (c4 != padding)
{
throw new IOException("invalid characters encountered at end of base64 data");
}
byte b = decodingTable[(uint)c1];
byte b2 = decodingTable[(uint)c2];
if ((b | b2) >= 128)
{
throw new IOException("invalid characters encountered at end of base64 data");
}
outStream.WriteByte((byte)((b << 2) | (b2 >> 4)));
return 1;
}
if (c4 == padding)
{
byte b3 = decodingTable[(uint)c1];
byte b4 = decodingTable[(uint)c2];
byte b5 = decodingTable[(uint)c3];
if ((b3 | b4 | b5) >= 128)
{
throw new IOException("invalid characters encountered at end of base64 data");
}
outStream.WriteByte((byte)((b3 << 2) | (b4 >> 4)));
outStream.WriteByte((byte)((b4 << 4) | (b5 >> 2)));
return 2;
}
byte b6 = decodingTable[(uint)c1];
byte b7 = decodingTable[(uint)c2];
byte b8 = decodingTable[(uint)c3];
byte b9 = decodingTable[(uint)c4];
if ((b6 | b7 | b8 | b9) >= 128)
{
throw new IOException("invalid characters encountered at end of base64 data");
}
outStream.WriteByte((byte)((b6 << 2) | (b7 >> 4)));
outStream.WriteByte((byte)((b7 << 4) | (b8 >> 2)));
outStream.WriteByte((byte)((b8 << 6) | b9));
return 3;
}
private int nextI(string data, int i, int finish)
{
while (i < finish && ignore(data[i]))
{
i++;
}
return i;
}
}

View File

@@ -0,0 +1,63 @@
using System;
namespace Org.BouncyCastle.Utilities.Encoders;
public class BufferedDecoder
{
internal byte[] buffer;
internal int bufOff;
internal ITranslator translator;
public BufferedDecoder(ITranslator translator, int bufferSize)
{
this.translator = translator;
if (bufferSize % translator.GetEncodedBlockSize() != 0)
{
throw new ArgumentException("buffer size not multiple of input block size");
}
buffer = new byte[bufferSize];
}
public int ProcessByte(byte input, byte[] output, int outOff)
{
int result = 0;
buffer[bufOff++] = input;
if (bufOff == buffer.Length)
{
result = translator.Decode(buffer, 0, buffer.Length, output, outOff);
bufOff = 0;
}
return result;
}
public int ProcessBytes(byte[] input, int inOff, int len, byte[] outBytes, int outOff)
{
if (len < 0)
{
throw new ArgumentException("Can't have a negative input length!");
}
int num = 0;
int num2 = buffer.Length - bufOff;
if (len > num2)
{
Array.Copy(input, inOff, buffer, bufOff, num2);
num += translator.Decode(buffer, 0, buffer.Length, outBytes, outOff);
bufOff = 0;
len -= num2;
inOff += num2;
outOff += num;
int num3 = len - len % buffer.Length;
num += translator.Decode(input, inOff, num3, outBytes, outOff);
len -= num3;
inOff += num3;
}
if (len != 0)
{
Array.Copy(input, inOff, buffer, bufOff, len);
bufOff += len;
}
return num;
}
}

View File

@@ -0,0 +1,63 @@
using System;
namespace Org.BouncyCastle.Utilities.Encoders;
public class BufferedEncoder
{
internal byte[] Buffer;
internal int bufOff;
internal ITranslator translator;
public BufferedEncoder(ITranslator translator, int bufferSize)
{
this.translator = translator;
if (bufferSize % translator.GetEncodedBlockSize() != 0)
{
throw new ArgumentException("buffer size not multiple of input block size");
}
Buffer = new byte[bufferSize];
}
public int ProcessByte(byte input, byte[] outBytes, int outOff)
{
int result = 0;
Buffer[bufOff++] = input;
if (bufOff == Buffer.Length)
{
result = translator.Encode(Buffer, 0, Buffer.Length, outBytes, outOff);
bufOff = 0;
}
return result;
}
public int ProcessBytes(byte[] input, int inOff, int len, byte[] outBytes, int outOff)
{
if (len < 0)
{
throw new ArgumentException("Can't have a negative input length!");
}
int num = 0;
int num2 = Buffer.Length - bufOff;
if (len > num2)
{
Array.Copy(input, inOff, Buffer, bufOff, num2);
num += translator.Encode(Buffer, 0, Buffer.Length, outBytes, outOff);
bufOff = 0;
len -= num2;
inOff += num2;
outOff += num;
int num3 = len - len % Buffer.Length;
num += translator.Encode(input, inOff, num3, outBytes, outOff);
len -= num3;
inOff += num3;
}
if (len != 0)
{
Array.Copy(input, inOff, Buffer, bufOff, len);
bufOff += len;
}
return num;
}
}

View File

@@ -0,0 +1,64 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders;
public sealed class Hex
{
private static readonly IEncoder encoder = new HexEncoder();
private Hex()
{
}
public static string ToHexString(byte[] data)
{
return ToHexString(data, 0, data.Length);
}
public static string ToHexString(byte[] data, int off, int length)
{
byte[] bytes = Encode(data, off, length);
return Strings.FromAsciiByteArray(bytes);
}
public static byte[] Encode(byte[] data)
{
return Encode(data, 0, data.Length);
}
public static byte[] Encode(byte[] data, int off, int length)
{
MemoryStream memoryStream = new MemoryStream(length * 2);
encoder.Encode(data, off, length, memoryStream);
return memoryStream.ToArray();
}
public static int Encode(byte[] data, Stream outStream)
{
return encoder.Encode(data, 0, data.Length, outStream);
}
public static int Encode(byte[] data, int off, int length, Stream outStream)
{
return encoder.Encode(data, off, length, outStream);
}
public static byte[] Decode(byte[] data)
{
MemoryStream memoryStream = new MemoryStream((data.Length + 1) / 2);
encoder.Decode(data, 0, data.Length, memoryStream);
return memoryStream.ToArray();
}
public static byte[] Decode(string data)
{
MemoryStream memoryStream = new MemoryStream((data.Length + 1) / 2);
encoder.DecodeString(data, memoryStream);
return memoryStream.ToArray();
}
public static int Decode(string data, Stream outStream)
{
return encoder.DecodeString(data, outStream);
}
}

View File

@@ -0,0 +1,112 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders;
public class HexEncoder : IEncoder
{
protected readonly byte[] encodingTable = new byte[16]
{
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
97, 98, 99, 100, 101, 102
};
protected readonly byte[] decodingTable = new byte[128];
protected void InitialiseDecodingTable()
{
Arrays.Fill(decodingTable, byte.MaxValue);
for (int i = 0; i < encodingTable.Length; i++)
{
decodingTable[encodingTable[i]] = (byte)i;
}
decodingTable[65] = decodingTable[97];
decodingTable[66] = decodingTable[98];
decodingTable[67] = decodingTable[99];
decodingTable[68] = decodingTable[100];
decodingTable[69] = decodingTable[101];
decodingTable[70] = decodingTable[102];
}
public HexEncoder()
{
InitialiseDecodingTable();
}
public int Encode(byte[] data, int off, int length, Stream outStream)
{
for (int i = off; i < off + length; i++)
{
int num = data[i];
outStream.WriteByte(encodingTable[num >> 4]);
outStream.WriteByte(encodingTable[num & 0xF]);
}
return length * 2;
}
private static bool Ignore(char c)
{
if (c != '\n' && c != '\r' && c != '\t')
{
return c == ' ';
}
return true;
}
public int Decode(byte[] data, int off, int length, Stream outStream)
{
int num = 0;
int num2 = off + length;
while (num2 > off && Ignore((char)data[num2 - 1]))
{
num2--;
}
int i = off;
while (i < num2)
{
for (; i < num2 && Ignore((char)data[i]); i++)
{
}
byte b = decodingTable[data[i++]];
for (; i < num2 && Ignore((char)data[i]); i++)
{
}
byte b2 = decodingTable[data[i++]];
if ((b | b2) >= 128)
{
throw new IOException("invalid characters encountered in Hex data");
}
outStream.WriteByte((byte)((b << 4) | b2));
num++;
}
return num;
}
public int DecodeString(string data, Stream outStream)
{
int num = 0;
int num2 = data.Length;
while (num2 > 0 && Ignore(data[num2 - 1]))
{
num2--;
}
int i = 0;
while (i < num2)
{
for (; i < num2 && Ignore(data[i]); i++)
{
}
byte b = decodingTable[(uint)data[i++]];
for (; i < num2 && Ignore(data[i]); i++)
{
}
byte b2 = decodingTable[(uint)data[i++]];
if ((b | b2) >= 128)
{
throw new IOException("invalid characters encountered in Hex data");
}
outStream.WriteByte((byte)((b << 4) | b2));
num++;
}
return num;
}
}

View File

@@ -0,0 +1,71 @@
namespace Org.BouncyCastle.Utilities.Encoders;
public class HexTranslator : ITranslator
{
private static readonly byte[] hexTable = new byte[16]
{
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
97, 98, 99, 100, 101, 102
};
public int GetEncodedBlockSize()
{
return 2;
}
public int Encode(byte[] input, int inOff, int length, byte[] outBytes, int outOff)
{
int num = 0;
int num2 = 0;
while (num < length)
{
outBytes[outOff + num2] = hexTable[(input[inOff] >> 4) & 0xF];
outBytes[outOff + num2 + 1] = hexTable[input[inOff] & 0xF];
inOff++;
num++;
num2 += 2;
}
return length * 2;
}
public int GetDecodedBlockSize()
{
return 1;
}
public int Decode(byte[] input, int inOff, int length, byte[] outBytes, int outOff)
{
int num = length / 2;
for (int i = 0; i < num; i++)
{
byte b = input[inOff + i * 2];
byte b2 = input[inOff + i * 2 + 1];
if (b < 97)
{
outBytes[outOff] = (byte)(b - 48 << 4);
}
else
{
outBytes[outOff] = (byte)(b - 97 + 10 << 4);
}
if (b2 < 97)
{
byte[] array2;
byte[] array = (array2 = outBytes);
int num2 = outOff;
nint num3 = num2;
array[num2] = (byte)(array2[num3] + (byte)(b2 - 48));
}
else
{
byte[] array2;
byte[] array3 = (array2 = outBytes);
int num4 = outOff;
nint num3 = num4;
array3[num4] = (byte)(array2[num3] + (byte)(b2 - 97 + 10));
}
outOff++;
}
return num;
}
}

View File

@@ -0,0 +1,12 @@
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders;
public interface IEncoder
{
int Encode(byte[] data, int off, int length, Stream outStream);
int Decode(byte[] data, int off, int length, Stream outStream);
int DecodeString(string data, Stream outStream);
}

View File

@@ -0,0 +1,12 @@
namespace Org.BouncyCastle.Utilities.Encoders;
public interface ITranslator
{
int GetEncodedBlockSize();
int Encode(byte[] input, int inOff, int length, byte[] outBytes, int outOff);
int GetDecodedBlockSize();
int Decode(byte[] input, int inOff, int length, byte[] outBytes, int outOff);
}

View File

@@ -0,0 +1,66 @@
using System;
using System.IO;
namespace Org.BouncyCastle.Utilities.Encoders;
public class UrlBase64
{
private static readonly IEncoder encoder = new UrlBase64Encoder();
public static byte[] Encode(byte[] data)
{
MemoryStream memoryStream = new MemoryStream();
try
{
encoder.Encode(data, 0, data.Length, memoryStream);
}
catch (IOException ex)
{
throw new Exception("exception encoding URL safe base64 string: " + ex.Message, ex);
}
return memoryStream.ToArray();
}
public static int Encode(byte[] data, Stream outStr)
{
return encoder.Encode(data, 0, data.Length, outStr);
}
public static byte[] Decode(byte[] data)
{
MemoryStream memoryStream = new MemoryStream();
try
{
encoder.Decode(data, 0, data.Length, memoryStream);
}
catch (IOException ex)
{
throw new Exception("exception decoding URL safe base64 string: " + ex.Message, ex);
}
return memoryStream.ToArray();
}
public static int Decode(byte[] data, Stream outStr)
{
return encoder.Decode(data, 0, data.Length, outStr);
}
public static byte[] Decode(string data)
{
MemoryStream memoryStream = new MemoryStream();
try
{
encoder.DecodeString(data, memoryStream);
}
catch (IOException ex)
{
throw new Exception("exception decoding URL safe base64 string: " + ex.Message, ex);
}
return memoryStream.ToArray();
}
public static int Decode(string data, Stream outStr)
{
return encoder.DecodeString(data, outStr);
}
}

View File

@@ -0,0 +1,12 @@
namespace Org.BouncyCastle.Utilities.Encoders;
public class UrlBase64Encoder : Base64Encoder
{
public UrlBase64Encoder()
{
encodingTable[encodingTable.Length - 2] = 45;
encodingTable[encodingTable.Length - 1] = 95;
padding = 46;
InitialiseDecodingTable();
}
}