Ejemplo n.º 1
0
        public void TryEncrypt_UsesEncrypt()
        {
            var rsa = new DelegateRSA {
                EncryptDelegate = (data, padding) => data
            };
            int bytesWritten;

            byte[] actual, expected;

            Assert.False(rsa.TryEncrypt(new byte[3], new byte[2], RSAEncryptionPadding.OaepSHA1, out bytesWritten));
            Assert.Equal(0, bytesWritten);

            expected = new byte[2] {
                42, 43
            };
            actual = new byte[2];
            Assert.True(rsa.TryEncrypt(expected, actual, RSAEncryptionPadding.OaepSHA1, out bytesWritten));
            Assert.Equal(2, bytesWritten);
            Assert.Equal(42, actual[0]);
            Assert.Equal(43, actual[1]);

            actual = new byte[3];
            Assert.True(rsa.TryEncrypt(expected, actual, RSAEncryptionPadding.OaepSHA1, out bytesWritten));
            Assert.Equal(2, bytesWritten);
            Assert.Equal(42, actual[0]);
            Assert.Equal(43, actual[1]);
            Assert.Equal(0, actual[2]);
        }
Ejemplo n.º 2
0
        public void TryHashData_UsesHashData()
        {
            var rsa = new DelegateRSA {
                HashDataArrayDelegate = (data, offset, count, name) => new Span <byte>(data, offset, count).ToArray()
            };
            int bytesWritten;

            byte[] actual, expected;

            Assert.False(rsa.TryHashData(new byte[3], new byte[2], HashAlgorithmName.SHA256, out bytesWritten));
            Assert.Equal(0, bytesWritten);

            expected = new byte[2] {
                42, 43
            };
            actual = new byte[2];
            Assert.True(rsa.TryHashData(expected, actual, HashAlgorithmName.SHA256, out bytesWritten));
            Assert.Equal(2, bytesWritten);
            Assert.Equal(42, actual[0]);
            Assert.Equal(43, actual[1]);

            actual = new byte[3];
            Assert.True(rsa.TryHashData(expected, actual, HashAlgorithmName.SHA256, out bytesWritten));
            Assert.Equal(2, bytesWritten);
            Assert.Equal(42, actual[0]);
            Assert.Equal(43, actual[1]);
            Assert.Equal(0, actual[2]);
        }
Ejemplo n.º 3
0
        public void TrySignHash_UsesSignHash()
        {
            var rsa = new DelegateRSA {
                SignHashDelegate = (data, name, padding) => data
            };
            int bytesWritten;

            byte[] actual, expected;

            Assert.False(rsa.TrySignHash(new byte[3], new byte[2], HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1, out bytesWritten));
            Assert.Equal(0, bytesWritten);

            expected = new byte[2] {
                42, 43
            };
            actual = new byte[2];
            Assert.True(rsa.TrySignHash(expected, actual, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1, out bytesWritten));
            Assert.Equal(2, bytesWritten);
            Assert.Equal(42, actual[0]);
            Assert.Equal(43, actual[1]);

            actual = new byte[3];
            Assert.True(rsa.TrySignHash(expected, actual, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1, out bytesWritten));
            Assert.Equal(2, bytesWritten);
            Assert.Equal(42, actual[0]);
            Assert.Equal(43, actual[1]);
            Assert.Equal(0, actual[2]);
        }
Ejemplo n.º 4
0
        public void VerifyHashSpan_UsesVerifyHashArray()
        {
            bool invoked = false;
            var  rsa     = new DelegateRSA {
                VerifyHashDelegate = delegate { invoked = true; return(true); }
            };

            Assert.True(rsa.VerifyHash(ReadOnlySpan <byte> .Empty, ReadOnlySpan <byte> .Empty, HashAlgorithmName.SHA256, RSASignaturePadding.Pss));
            Assert.True(invoked);
        }
Ejemplo n.º 5
0
        public void SignDataStream_UsesHashDataAndSignHash()
        {
            var rsa = new DelegateRSA();

            AssertExtensions.Throws <ArgumentNullException>("data", () => rsa.SignData((Stream)null, HashAlgorithmName.SHA256, null));

            AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => rsa.SignData(Stream.Null, new HashAlgorithmName(null), null));
            AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => rsa.SignData(Stream.Null, new HashAlgorithmName(""), null));

            AssertExtensions.Throws <ArgumentNullException>("padding", () => rsa.SignData(Stream.Null, new HashAlgorithmName("abc"), null));

            rsa.HashDataStreamDelegate = (stream, name) => ((MemoryStream)stream).ToArray();
            rsa.SignHashDelegate       = (data, name, padding) => data.Select(b => (byte)(b * 2)).ToArray();
            Assert.Equal <byte>(new byte[] { 6, 8, 10, 12, 14, 16 }, rsa.SignData(new MemoryStream(new byte[] { 3, 4, 5, 6, 7, 8 }), HashAlgorithmName.SHA256, RSASignaturePadding.Pss));
        }
Ejemplo n.º 6
0
        public void VerifyDataStream_UsesHashDataAndVerifyHash()
        {
            var rsa = new DelegateRSA();

            AssertExtensions.Throws <ArgumentNullException>("data", () => rsa.VerifyData((Stream)null, null, HashAlgorithmName.SHA256, null));

            AssertExtensions.Throws <ArgumentNullException>("signature", () => rsa.VerifyData(Stream.Null, null, HashAlgorithmName.SHA256, null));

            AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => rsa.VerifyData(Stream.Null, new byte[1], new HashAlgorithmName(null), null));
            AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => rsa.VerifyData(Stream.Null, new byte[1], new HashAlgorithmName(""), null));

            AssertExtensions.Throws <ArgumentNullException>("padding", () => rsa.VerifyData(Stream.Null, new byte[1], new HashAlgorithmName("abc"), null));

            rsa.HashDataStreamDelegate = (stream, name) => ((MemoryStream)stream).ToArray();
            rsa.VerifyHashDelegate     = (hash, signature, name, padding) => hash[0] == 42;
            Assert.True(rsa.VerifyData(new MemoryStream(new byte[] { 42 }), new byte[1] {
                24
            }, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
        }
Ejemplo n.º 7
0
        public void SignDataArray_UsesHashDataAndSignHash()
        {
            var rsa = new DelegateRSA();

            AssertExtensions.Throws <ArgumentNullException>("data", () => rsa.SignData((byte[])null, HashAlgorithmName.SHA256, null));
            AssertExtensions.Throws <ArgumentNullException>("data", () => rsa.SignData(null, 0, 0, HashAlgorithmName.SHA256, null));

            AssertExtensions.Throws <ArgumentOutOfRangeException>("offset", () => rsa.SignData(new byte[1], -1, 0, HashAlgorithmName.SHA256, null));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("offset", () => rsa.SignData(new byte[1], 2, 0, HashAlgorithmName.SHA256, null));

            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => rsa.SignData(new byte[1], 0, -1, HashAlgorithmName.SHA256, null));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => rsa.SignData(new byte[1], 0, 2, HashAlgorithmName.SHA256, null));

            AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => rsa.SignData(new byte[1], 0, 1, new HashAlgorithmName(null), null));
            AssertExtensions.Throws <ArgumentException>("hashAlgorithm", () => rsa.SignData(new byte[1], 0, 1, new HashAlgorithmName(""), null));

            AssertExtensions.Throws <ArgumentNullException>("padding", () => rsa.SignData(new byte[1], 0, 1, new HashAlgorithmName("abc"), null));

            rsa.HashDataArrayDelegate = (data, offset, count, name) => new Span <byte>(data, offset, count).ToArray();
            rsa.SignHashDelegate      = (data, name, padding) => data.Select(b => (byte)(b * 2)).ToArray();
            Assert.Equal <byte>(new byte[] { 6, 8, 10, 12, 14, 16 }, rsa.SignData(new byte[] { 3, 4, 5, 6, 7, 8 }, HashAlgorithmName.SHA256, RSASignaturePadding.Pss));
            Assert.Equal <byte>(new byte[] { 10, 12, 14 }, rsa.SignData(new byte[] { 3, 4, 5, 6, 7, 8 }, 2, 3, HashAlgorithmName.SHA256, RSASignaturePadding.Pss));
        }