53 lines
1.2 KiB
C#
53 lines
1.2 KiB
C#
|
using Org.BouncyCastle.OpenSsl;
|
||
|
|
||
|
namespace InnovEnergy.OpenVpnCertificatesServer.PKI;
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|