Exemplo n.º 1
0
        public void Array_SignData_VerifyData_UsesHashDataAndSignHashAndVerifyHash()
        {
            using (var ecdsa = new OverrideAbstractECDsa(ECDsaFactory.Create()))
            {
                AssertExtensions.Throws <ArgumentNullException>("data", () => ecdsa.SignData((byte[])null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentNullException>("data", () => ecdsa.SignData(null, 0, 0, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("offset", () => ecdsa.SignData(new byte[1], -1, 0, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("offset", () => ecdsa.SignData(new byte[1], 2, 0, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => ecdsa.SignData(new byte[1], 0, -1, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => ecdsa.SignData(new byte[1], 0, 2, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => ecdsa.SignData(new byte[1], 0, 1, new HashAlgorithmName(null)));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => ecdsa.SignData(new byte[1], 0, 1, new HashAlgorithmName("")));

                AssertExtensions.Throws <ArgumentNullException>("data", () => ecdsa.VerifyData((byte[])null, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentNullException>("data", () => ecdsa.VerifyData(null, 0, 0, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("offset", () => ecdsa.VerifyData(new byte[1], -1, 0, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("offset", () => ecdsa.VerifyData(new byte[1], 2, 0, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => ecdsa.VerifyData(new byte[1], 0, -1, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => ecdsa.VerifyData(new byte[1], 0, 2, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentNullException>("signature", () => ecdsa.VerifyData(new byte[1], 0, 1, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => ecdsa.VerifyData(new byte[1], 0, 1, new byte[1], new HashAlgorithmName(null)));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => ecdsa.VerifyData(new byte[1], 0, 1, new byte[1], new HashAlgorithmName("")));

                var input = new byte[1024];
                Random.Shared.NextBytes(input);

                byte[] result = ecdsa.SignData(input, HashAlgorithmName.SHA256);
                Assert.NotNull(result);
                Assert.NotEmpty(result);

                Assert.False(ecdsa.VerifyData(input.AsSpan(1).ToArray(), result, HashAlgorithmName.SHA256));
                Assert.True(ecdsa.VerifyData(input, result, HashAlgorithmName.SHA256));
            }
        }
Exemplo n.º 2
0
 public void BaseProperties_ExpectedValues()
 {
     using (var ecdsa = new OverrideAbstractECDsa(ECDsaFactory.Create()))
     {
         Assert.Null(ecdsa.KeyExchangeAlgorithm);
         Assert.Equal("ECDsa", ecdsa.SignatureAlgorithm);
     }
 }
Exemplo n.º 3
0
        public void NotSupportedBaseMethods_Throw()
        {
            using (var ecdsa = new OverrideAbstractECDsa(ECDsaFactory.Create()))
            {
                Assert.Throws <NotSupportedException>(() => ecdsa.ExportParameters(false));
                Assert.Throws <NotSupportedException>(() => ecdsa.ExportExplicitParameters(false));
                Assert.Throws <NotSupportedException>(() => ecdsa.ImportParameters(default(ECParameters)));
                Assert.Throws <NotSupportedException>(() => ecdsa.GenerateKey(default(ECCurve)));

                Assert.Throws <NotImplementedException>(() => ecdsa.FromXmlString(null));
                Assert.Throws <NotImplementedException>(() => ecdsa.ToXmlString(false));
            }
        }
Exemplo n.º 4
0
        public void Span_TrySignData_VerifyData_UsesTryHashDataAndTrySignHashAndTryVerifyHash()
        {
            using (var ecdsa = new OverrideAbstractECDsa(ECDsaFactory.Create()))
            {
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => ecdsa.TrySignData(new byte[1], new byte[1], new HashAlgorithmName(null), out int bytesWritten));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => ecdsa.TrySignData(new byte[1], new byte[1], new HashAlgorithmName(""), out int bytesWritten));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => ecdsa.VerifyData((ReadOnlySpan <byte>) new byte[1], new byte[1], new HashAlgorithmName("")));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => ecdsa.VerifyData((ReadOnlySpan <byte>) new byte[1], new byte[1], new HashAlgorithmName(null)));

                var input = new byte[1024];
                Random.Shared.NextBytes(input);

                byte[] output = new byte[1];
                int    outputLength;
                while (!ecdsa.TrySignData(input, output, HashAlgorithmName.SHA256, out outputLength))
                {
                    output = new byte[output.Length * 2];
                }

                Assert.False(ecdsa.VerifyData((ReadOnlySpan <byte>)input, new ReadOnlySpan <byte>(output, 0, outputLength - 1), HashAlgorithmName.SHA256));
                Assert.True(ecdsa.VerifyData((ReadOnlySpan <byte>)input, new ReadOnlySpan <byte>(output, 0, outputLength), HashAlgorithmName.SHA256));
            }
        }
Exemplo n.º 5
0
        public void Stream_SignData_VerifyData_UsesHashDataAndSignHashAndVerifyHash()
        {
            using (var ecdsa = new OverrideAbstractECDsa(ECDsaFactory.Create()))
            {
                AssertExtensions.Throws <ArgumentNullException>("data", () => ecdsa.SignData((Stream)null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => ecdsa.SignData(new MemoryStream(new byte[1]), new HashAlgorithmName(null)));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => ecdsa.SignData(new MemoryStream(new byte[1]), new HashAlgorithmName("")));

                AssertExtensions.Throws <ArgumentNullException>("data", () => ecdsa.VerifyData((Stream)null, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentNullException>("signature", () => ecdsa.VerifyData(new MemoryStream(new byte[1]), null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => ecdsa.VerifyData(new MemoryStream(new byte[1]), new byte[1], new HashAlgorithmName(null)));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => ecdsa.VerifyData(new MemoryStream(new byte[1]), new byte[1], new HashAlgorithmName("")));

                var input = new byte[1024];
                new Random().NextBytes(input);

                byte[] result = ecdsa.SignData(new MemoryStream(input), HashAlgorithmName.SHA256);
                Assert.NotNull(result);
                Assert.NotEmpty(result);

                Assert.False(ecdsa.VerifyData(new MemoryStream(input.AsSpan(1).ToArray()), result, HashAlgorithmName.SHA256));
                Assert.True(ecdsa.VerifyData(new MemoryStream(input), result, HashAlgorithmName.SHA256));
            }
        }