Created
July 31, 2019 13:20
-
-
Save joaopgrassi/ffcd3271b93d6b84a64b28dbfe079c38 to your computer and use it in GitHub Desktop.
Creates a random RSA key used for signing jwt tokens. Also exports the public key in PEM format.
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
private static (string publicKeyPem, SigningCredentials signingCredentials) GenerateRamdomRsaKeyPair() | |
{ | |
SigningCredentials signingCredentials = null; | |
string publicKeyPem; | |
using (RSA rsa = RSA.Create()) | |
{ | |
rsa.KeySize = 4096; | |
var rsaParams = rsa.ExportParameters(false); | |
var rsaSecurityKey = new RsaSecurityKey(rsa); | |
signingCredentials = new SigningCredentials(rsaSecurityKey, SecurityAlgorithms.RsaSha256); | |
using (var stream = new MemoryStream()) | |
using (var writer = new PemWriter(stream)) | |
{ | |
writer.WritePublicKey(rsaParams); | |
stream.Seek(0, SeekOrigin.Begin); | |
using (var reader = new StreamReader(stream, Encoding.UTF8)) | |
{ | |
publicKeyPem = reader.ReadToEnd(); | |
} | |
} | |
} | |
return (publicKeyPem, signingCredentials); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment