public static void CreateFromSubjectPublicKeyInfo_Roundtrip_ECDSA() { using ECDsa ecdsa = ECDsa.Create(); ecdsa.ImportFromPem(TestData.ECDsaPkcs8PublicKey); byte[] spki = ecdsa.ExportSubjectPublicKeyInfo(); PublicKey key = PublicKey.CreateFromSubjectPublicKeyInfo(spki, out int read); Assert.Throws <NotSupportedException>(() => key.Key); Assert.Equal("1.2.840.10045.2.1", key.Oid.Value); Assert.Equal(spki, key.ExportSubjectPublicKeyInfo()); Assert.Equal(spki.Length, read); }
public static void ExportSubjectPublicKeyInfo_ECDSA() { using ECDsa ecdsa = ECDsa.Create(); ecdsa.ImportFromPem(TestData.ECDsaPkcs8PublicKey); PublicKey key = new PublicKey(ecdsa); Span <byte> algSpki = ecdsa.ExportSubjectPublicKeyInfo(); Assert.True(algSpki.SequenceEqual(key.ExportSubjectPublicKeyInfo()), "SequenceEquals(ExportSubjectPublicKeyInfo)"); // Just right Assert.True(key.TryExportSubjectPublicKeyInfo(algSpki, out int written), nameof(key.TryExportSubjectPublicKeyInfo)); Assert.Equal(algSpki.Length, written); // Too small Assert.False(key.TryExportSubjectPublicKeyInfo(algSpki.Slice(1), out written), nameof(key.TryExportSubjectPublicKeyInfo)); Assert.Equal(0, written); }