Innovenergy_trunk/csharp/App/OpenVpnCertificatesServer/PKI/Pem.cs

53 lines
1.2 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using Org.BouncyCastle.OpenSsl;
namespace InnovEnergy.App.OpenVpnCertificatesServer.PKI;
2023-02-16 12:57:06 +00:00
public static class Pem
{
public static String Encode(Object obj)
{
using var sw = new StringWriter();
var pem = new PemWriter(sw);
pem.WriteObject(obj);
pem.Writer.Flush();
return sw.ToString();
}
public static T Decode<T>(String pem)
{
using var reader = new StringReader(pem);
var pemReader = new PemReader(reader);
return (T) pemReader.ReadObject();
}
public static T? Decode<T>(String pem, IPasswordFinder pwf)
{
try
{
using var reader = new StringReader(pem);
var pemReader = new PemReader(reader, pwf);
return (T) pemReader.ReadObject();
}
catch
{
return default;
}
}
public static T? Decode<T>(String pem, String password)
{
try
{
using var reader = new StringReader(pem);
var pwf = new PwdFinder(password);
var pemReader = new PemReader(reader, pwf);
return (T) pemReader.ReadObject();
}
catch (Exception)
{
return default;
}
}
}