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,21 @@
using System;
namespace Org.BouncyCastle.Utilities.IO.Pem;
[Serializable]
public class PemGenerationException : Exception
{
public PemGenerationException()
{
}
public PemGenerationException(string message)
: base(message)
{
}
public PemGenerationException(string message, Exception exception)
: base(message, exception)
{
}
}

View File

@@ -0,0 +1,46 @@
namespace Org.BouncyCastle.Utilities.IO.Pem;
public class PemHeader
{
private string name;
private string val;
public virtual string Name => name;
public virtual string Value => val;
public PemHeader(string name, string val)
{
this.name = name;
this.val = val;
}
public override int GetHashCode()
{
return GetHashCode(name) + 31 * GetHashCode(val);
}
public override bool Equals(object obj)
{
if (obj == this)
{
return true;
}
if (!(obj is PemHeader))
{
return false;
}
PemHeader pemHeader = (PemHeader)obj;
if (object.Equals(name, pemHeader.name))
{
return object.Equals(val, pemHeader.val);
}
return false;
}
private int GetHashCode(string s)
{
return s?.GetHashCode() ?? 1;
}
}

View File

@@ -0,0 +1,35 @@
using System.Collections;
namespace Org.BouncyCastle.Utilities.IO.Pem;
public class PemObject : PemObjectGenerator
{
private string type;
private IList headers;
private byte[] content;
public string Type => type;
public IList Headers => headers;
public byte[] Content => content;
public PemObject(string type, byte[] content)
: this(type, Platform.CreateArrayList(), content)
{
}
public PemObject(string type, IList headers, byte[] content)
{
this.type = type;
this.headers = Platform.CreateArrayList(headers);
this.content = content;
}
public PemObject Generate()
{
return this;
}
}

View File

@@ -0,0 +1,6 @@
namespace Org.BouncyCastle.Utilities.IO.Pem;
public interface PemObjectGenerator
{
PemObject Generate();
}

View File

@@ -0,0 +1,6 @@
namespace Org.BouncyCastle.Utilities.IO.Pem;
public interface PemObjectParser
{
object ParseObject(PemObject obj);
}

View File

@@ -0,0 +1,76 @@
using System;
using System.Collections;
using System.IO;
using System.Text;
using Org.BouncyCastle.Utilities.Encoders;
namespace Org.BouncyCastle.Utilities.IO.Pem;
public class PemReader
{
private const string BeginString = "-----BEGIN ";
private const string EndString = "-----END ";
private readonly TextReader reader;
public TextReader Reader => reader;
public PemReader(TextReader reader)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
this.reader = reader;
}
public PemObject ReadPemObject()
{
string text = reader.ReadLine();
if (text != null && Platform.StartsWith(text, "-----BEGIN "))
{
text = text.Substring("-----BEGIN ".Length);
int num = text.IndexOf('-');
string type = text.Substring(0, num);
if (num > 0)
{
return LoadObject(type);
}
}
return null;
}
private PemObject LoadObject(string type)
{
string text = "-----END " + type;
IList list = Platform.CreateArrayList();
StringBuilder stringBuilder = new StringBuilder();
string text2;
while ((text2 = reader.ReadLine()) != null && Platform.IndexOf(text2, text) == -1)
{
int num = text2.IndexOf(':');
if (num == -1)
{
stringBuilder.Append(text2.Trim());
continue;
}
string text3 = text2.Substring(0, num).Trim();
if (Platform.StartsWith(text3, "X-"))
{
text3 = text3.Substring(2);
}
string val = text2.Substring(num + 1).Trim();
list.Add(new PemHeader(text3, val));
}
if (text2 == null)
{
throw new IOException(text + " not found");
}
if (stringBuilder.Length % 4 != 0)
{
throw new IOException("base64 data appears to be truncated");
}
return new PemObject(type, list, Base64.Decode(stringBuilder.ToString()));
}
}

View File

@@ -0,0 +1,85 @@
using System;
using System.IO;
using Org.BouncyCastle.Utilities.Encoders;
namespace Org.BouncyCastle.Utilities.IO.Pem;
public class PemWriter
{
private const int LineLength = 64;
private readonly TextWriter writer;
private readonly int nlLength;
private char[] buf = new char[64];
public TextWriter Writer => writer;
public PemWriter(TextWriter writer)
{
if (writer == null)
{
throw new ArgumentNullException("writer");
}
this.writer = writer;
nlLength = Platform.NewLine.Length;
}
public int GetOutputSize(PemObject obj)
{
int num = 2 * (obj.Type.Length + 10 + nlLength) + 6 + 4;
if (obj.Headers.Count > 0)
{
foreach (PemHeader header in obj.Headers)
{
num += header.Name.Length + ": ".Length + header.Value.Length + nlLength;
}
num += nlLength;
}
int num2 = (obj.Content.Length + 2) / 3 * 4;
return num + (num2 + (num2 + 64 - 1) / 64 * nlLength);
}
public void WriteObject(PemObjectGenerator objGen)
{
PemObject pemObject = objGen.Generate();
WritePreEncapsulationBoundary(pemObject.Type);
if (pemObject.Headers.Count > 0)
{
foreach (PemHeader header in pemObject.Headers)
{
writer.Write(header.Name);
writer.Write(": ");
writer.WriteLine(header.Value);
}
writer.WriteLine();
}
WriteEncoded(pemObject.Content);
WritePostEncapsulationBoundary(pemObject.Type);
}
private void WriteEncoded(byte[] bytes)
{
bytes = Base64.Encode(bytes);
for (int i = 0; i < bytes.Length; i += buf.Length)
{
int j;
for (j = 0; j != buf.Length && i + j < bytes.Length; j++)
{
buf[j] = (char)bytes[i + j];
}
writer.WriteLine(buf, 0, j);
}
}
private void WritePreEncapsulationBoundary(string type)
{
writer.WriteLine("-----BEGIN " + type + "-----");
}
private void WritePostEncapsulationBoundary(string type)
{
writer.WriteLine("-----END " + type + "-----");
}
}