This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Nuget: BouncyCastle.NetCoreSdk 1.9.3.1 | |
using System; | |
using System.IO; | |
using System.Net; | |
using System.Net.Security; | |
using System.Net.Sockets; | |
using System.Runtime.Intrinsics.Arm; | |
using System.Runtime.Serialization.Json; | |
using System.Security.Authentication; | |
using System.Security.Cryptography; | |
using System.Security.Cryptography.X509Certificates; | |
using System.Threading.Channels; | |
using Org.BouncyCastle.Asn1.X509; | |
using Org.BouncyCastle.Crypto; | |
using Org.BouncyCastle.Crypto.Generators; | |
using Org.BouncyCastle.Crypto.Operators; | |
using Org.BouncyCastle.Crypto.Parameters; | |
using Org.BouncyCastle.Math; | |
using Org.BouncyCastle.Pkcs; | |
using Org.BouncyCastle.Security; | |
using Org.BouncyCastle.X509; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var keyPairGenerator = new Ed25519KeyPairGenerator(); | |
keyPairGenerator.Init(new Ed25519KeyGenerationParameters(new SecureRandom())); | |
var keyPair = keyPairGenerator.GenerateKeyPair(); | |
var certGen = new X509V3CertificateGenerator(); | |
certGen.SetNotAfter(DateTime.Today.AddYears(3)); | |
certGen.SetNotBefore(DateTime.Today.AddDays(-1)); | |
certGen.SetIssuerDN(new X509Name("CN=sample")); | |
certGen.SetSerialNumber(new BigInteger(20*8, new SecureRandom()).Abs()); | |
certGen.SetSubjectDN(new X509Name("CN=sample")); | |
certGen.SetPublicKey(keyPair.Public); | |
var x509Certificate = certGen.Generate(new Ed25519Asn1SignatureFactory( keyPair)); | |
var store = new Pkcs12Store(); | |
string friendlyName = x509Certificate.SubjectDN.ToString(); | |
var certificateEntry = new X509CertificateEntry(x509Certificate); | |
store.SetCertificateEntry(friendlyName, certificateEntry); | |
store.SetKeyEntry(friendlyName, new AsymmetricKeyEntry(keyPair.Private), new[] { certificateEntry }); | |
var ms = new MemoryStream(); | |
store.Save(ms, null, new SecureRandom()); | |
File.WriteAllBytes("cert.pfx", ms.ToArray()); | |
var x509Certificate2 = new X509Certificate2(ms.ToArray()); | |
} | |
private class Ed25519Asn1SignatureFactory : ISignatureFactory | |
{ | |
private AsymmetricCipherKeyPair _keyPair; | |
private Asn1SignatureFactory _asn1SignatureFactory; | |
private string _algorithm; | |
public Ed25519Asn1SignatureFactory(AsymmetricCipherKeyPair keyPair) | |
{ | |
_keyPair = keyPair; | |
_algorithm = "1.3.101.112"; | |
_asn1SignatureFactory = new Asn1SignatureFactory(_algorithm, keyPair.Private, new SecureRandom()); | |
} | |
public IStreamCalculator CreateCalculator() | |
{ | |
ISigner signer = SignerUtilities.GetSigner(_algorithm); | |
signer.Init(true, _keyPair.Private); | |
return new DefaultSignatureCalculator(signer); | |
} | |
public object AlgorithmDetails => _asn1SignatureFactory.AlgorithmDetails; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment