示例#1
0
        public void VerifyData_Span_UsesTryHashDataAndVerifySignature()
        {
            var input = new byte[1024];

            new Random(42).NextBytes(input);

            using (var wrapperDsa = new OverrideAbstractDSA(DSA.Create(1024)))
            {
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => wrapperDsa.VerifyData((Span <byte>) new byte[1], new byte[1], new HashAlgorithmName(null)));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => wrapperDsa.VerifyData((Span <byte>) new byte[1], new byte[1], new HashAlgorithmName("")));

                byte[] signature = wrapperDsa.SignData(input, HashAlgorithmName.SHA1);
                Assert.True(wrapperDsa.VerifyData(input.AsSpan(), signature, HashAlgorithmName.SHA1));
                Assert.False(wrapperDsa.VerifyData(input.AsSpan(), signature.AsSpan().Slice(0, signature.Length - 1), HashAlgorithmName.SHA1));
            }
        }
示例#2
0
 public void SignData_InvalidArguments_Throws()
 {
     using (var wrapperDsa = new OverrideAbstractDSA(DSA.Create(1024)))
     {
         AssertExtensions.Throws <ArgumentNullException>("data", () => wrapperDsa.SignData((byte[])null, HashAlgorithmName.SHA1));
         AssertExtensions.Throws <ArgumentNullException>("data", () => wrapperDsa.SignData((Stream)null, HashAlgorithmName.SHA1));
         AssertExtensions.Throws <ArgumentNullException>("data", () => wrapperDsa.SignData(null, 0, 0, HashAlgorithmName.SHA1));
         AssertExtensions.Throws <ArgumentOutOfRangeException>("offset", () => wrapperDsa.SignData(new byte[1], -1, 0, HashAlgorithmName.SHA1));
         AssertExtensions.Throws <ArgumentOutOfRangeException>("offset", () => wrapperDsa.SignData(new byte[1], 2, 0, HashAlgorithmName.SHA1));
         AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => wrapperDsa.SignData(new byte[1], 0, -1, HashAlgorithmName.SHA1));
         AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => wrapperDsa.SignData(new byte[1], 0, 2, HashAlgorithmName.SHA1));
         AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => wrapperDsa.SignData(new byte[1], new HashAlgorithmName(null)));
         AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => wrapperDsa.SignData(new byte[1], new HashAlgorithmName("")));
         AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => wrapperDsa.SignData(new MemoryStream(new byte[1]), new HashAlgorithmName(null)));
         AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => wrapperDsa.SignData(new MemoryStream(new byte[1]), new HashAlgorithmName("")));
     }
 }
示例#3
0
        public void VerifyData_Stream_UsesHashDataAndVerifySignature()
        {
            var input = new byte[1024];

            new Random(42).NextBytes(input);

            using (var wrapperDsa = new OverrideAbstractDSA(DSA.Create(1024)))
            {
                AssertExtensions.Throws <ArgumentNullException>("data", () => wrapperDsa.VerifyData((Stream)null, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentNullException>("signature", () => wrapperDsa.VerifyData(new MemoryStream(new byte[1]), null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => wrapperDsa.VerifyData(new MemoryStream(new byte[1]), new byte[1], new HashAlgorithmName(null)));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => wrapperDsa.VerifyData(new MemoryStream(new byte[1]), new byte[1], new HashAlgorithmName("")));

                byte[] signature = wrapperDsa.SignData(new MemoryStream(input), HashAlgorithmName.SHA1);
                Assert.True(wrapperDsa.VerifyData(new MemoryStream(input), signature, HashAlgorithmName.SHA1));
                Assert.False(wrapperDsa.VerifyData(new MemoryStream(input), signature.AsSpan().Slice(0, signature.Length - 1).ToArray(), HashAlgorithmName.SHA1));
            }
        }
示例#4
0
        public void TryCreateSignature_UsesCreateSignature()
        {
            var input = new byte[1024];

            new Random(42).NextBytes(input);
            int bytesWritten = 0;

            using (var wrapperDsa = new OverrideAbstractDSA(DSA.Create(1024)))
            {
                byte[] initialSig = wrapperDsa.CreateSignature(input);
                byte[] actualSig  = new byte[initialSig.Length];

                Assert.False(wrapperDsa.TryCreateSignature(input, new Span <byte>(actualSig, 0, actualSig.Length - 1), out bytesWritten));
                Assert.Equal(0, bytesWritten);
                Assert.All(actualSig, b => Assert.Equal(0, b));

                Assert.True(wrapperDsa.TryCreateSignature(input, actualSig, out bytesWritten));
                Assert.Equal(initialSig.Length, bytesWritten);
                Assert.Contains(actualSig, b => b != 0);
            }
        }
示例#5
0
        public void VerifyData_Array_UsesHashDataAndVerifySignature()
        {
            var input = new byte[1024];

            new Random(42).NextBytes(input);

            using (var wrapperDsa = new OverrideAbstractDSA(DSA.Create(1024)))
            {
                AssertExtensions.Throws <ArgumentNullException>("data", () => wrapperDsa.VerifyData((byte[])null, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentNullException>("data", () => wrapperDsa.VerifyData(null, 0, 0, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("offset", () => wrapperDsa.VerifyData(new byte[1], -1, 0, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("offset", () => wrapperDsa.VerifyData(new byte[1], 2, 0, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => wrapperDsa.VerifyData(new byte[1], 0, -1, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => wrapperDsa.VerifyData(new byte[1], 0, 2, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentNullException>("signature", () => wrapperDsa.VerifyData(new byte[1], 0, 1, null, HashAlgorithmName.SHA1));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => wrapperDsa.VerifyData(new byte[1], new byte[1], new HashAlgorithmName(null)));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => wrapperDsa.VerifyData(new byte[1], new byte[1], new HashAlgorithmName("")));

                byte[] signature = wrapperDsa.SignData(input, HashAlgorithmName.SHA1);
                Assert.True(wrapperDsa.VerifyData(input.AsSpan(), signature, HashAlgorithmName.SHA1));
                Assert.False(wrapperDsa.VerifyData(input.AsSpan(), signature.AsSpan().Slice(0, signature.Length - 1), HashAlgorithmName.SHA1));
            }
        }
示例#6
0
        public void TrySignData_UsesTryHashDataAndTryCreateSignature()
        {
            var input = new byte[1024];

            new Random(42).NextBytes(input);
            int bytesWritten = 0;

            using (var wrapperDsa = new OverrideAbstractDSA(DSA.Create(1024)))
            {
                byte[] initialSig = wrapperDsa.SignData(input, HashAlgorithmName.SHA1);
                byte[] actualSig  = new byte[initialSig.Length];

                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => wrapperDsa.TrySignData(new byte[1], new byte[1], new HashAlgorithmName(null), out int _));
                AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => wrapperDsa.TrySignData(new byte[1], new byte[1], new HashAlgorithmName(""), out int _));

                Assert.False(wrapperDsa.TrySignData(input, new Span <byte>(actualSig, 0, 1), HashAlgorithmName.SHA1, out bytesWritten));
                Assert.Equal(0, bytesWritten);
                Assert.All(actualSig, b => Assert.Equal(0, b));

                Assert.True(wrapperDsa.TrySignData(input, actualSig, HashAlgorithmName.SHA1, out bytesWritten));
                Assert.Equal(initialSig.Length, bytesWritten);
                Assert.Contains(actualSig, b => b != 0);
            }
        }