/// <summary> /// Converts a cryptographic RSA key to a serialization model instance. /// </summary> /// <param name="key">The cryptographic RSA key.</param> /// <returns>The serialization model.</returns> /// <exception cref="ArgumentNullException">Thrown, if /// <paramref name="key"/> is null.</exception> public static SerializationModel Convert(RsaKey key) { if (key == null) { throw new ArgumentNullException(nameof(key)); } var rsaParameters = key.CreateRsa().ExportParameters(true); return(new SerializationModel { Hash = System.Convert.ToBase64String(key.Hash.Hash.ToArray()), Parameters = new RsaSerializationModel { D = System.Convert.ToBase64String(rsaParameters.D), DP = System.Convert.ToBase64String(rsaParameters.DP), DQ = System.Convert.ToBase64String(rsaParameters.DQ), Exponent = System.Convert.ToBase64String(rsaParameters.Exponent), InverseQ = System.Convert.ToBase64String(rsaParameters.InverseQ), Modulus = System.Convert.ToBase64String(rsaParameters.Modulus), P = System.Convert.ToBase64String(rsaParameters.P), Q = System.Convert.ToBase64String(rsaParameters.Q), }, EmbeddedData = key.EmbeddedData == null ? null : System.Convert.ToBase64String(key.EmbeddedData.ToArray()), SignerCertificateHash = key.Signature == null ? null : System.Convert.ToBase64String(key.Signature.SignerCertificateHash.Hash.ToArray()), Signature = key.Signature == null ? null : System.Convert.ToBase64String(key.Signature.Signature.ToArray()), }); }
public void Constructor_Should_SetProperties() { // Arrange // Act var obj = new RsaKey(parameters: ScenarioRsa.DefaultRsaParameters); // Assert var rsa = obj.CreateRsa(); var rsaParameters = rsa.ExportParameters(true); rsaParameters.Modulus.SequenceEqual(ScenarioRsa.DefaultRsaParameters.Modulus).Should().BeTrue(); rsaParameters.D.SequenceEqual(ScenarioRsa.DefaultRsaParameters.D).Should().BeTrue(); }
static ScenarioRsa() { DefaultData = new byte[] { 0x00, 0x01, 0x02, 0x03 }; DefaultDataHash = Sha512Hash.Compute(DefaultData); DefaultEmbeddedData = new byte[] { 0xff, 0xee, 0xdd, 0xcc }; DefaultKey = RsaKey.Generate(); DefaultRsa = DefaultKey.CreateRsa(); DefaultRsaParameters = DefaultRsa.ExportParameters(true); DefaultCertificateSignature = new RsaSignature(Sha512Hash.Compute(new byte[] { 0x12, 0x34, 0xaa, 0xbb }), new byte[] { 0xa1, 0xb2, 0xc3, 0xd4 }); DefaultCertificate = new RsaCertificate(DefaultRsaParameters, DefaultCertificateSignature); DefaultSignerKey = RsaKey.Generate(); DefaultSignerCertificate = (RsaCertificate)DefaultSignerKey.DeriveCertificate(); DefaultSignatureData = new byte[] { 0x1f, 0x2f, 0x3f, 0x4f }; DefaultSignature = new RsaSignature(DefaultSignerCertificate.Hash, DefaultSignatureData); DefaultChain = new ChainOfTrust(DefaultSignerCertificate); }