/// <summary>
        /// The class responsible for creating and validating hash and HMAC.
        /// </summary>
        /// <param name="algorithmToUse">The hashing algorithm to use</param>
        internal Hasher(SupportedAlgorithms algorithmToUse)
        {
            if (!Enum.IsDefined(typeof(SupportedAlgorithms), algorithmToUse))
                throw new ArgumentException("Invalid algorithm");

            this.algorithmName = algorithmToUse.ToString();
        }
        /// <summary>
        /// The class responsible for creating and validating hash and HMAC.
        /// </summary>
        /// <param name="algorithmToUse">The hashing algorithm to use</param>
        internal Hasher(SupportedAlgorithms algorithmToUse)
        {
            if (!Enum.IsDefined(typeof(SupportedAlgorithms), algorithmToUse))
            {
                throw new ArgumentException("Invalid algorithm");
            }

            this.algorithmName = algorithmToUse.ToString();
        }
        public void GetDigestFromSignatureAlgorithm(SupportedAlgorithmTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.GetDigestFromSignatureAlgorithm", theoryData);

            try
            {
                if (!theoryData.Digest.Equals(SupportedAlgorithms.GetDigestFromSignatureAlgorithm(theoryData.Algorithm)))
                {
                    context.AddDiff($"(!theoryData.Digest.Equals(SupportedAlgorithms.GetDigestFromSignatureAlgorithm(theoryData.Algorithm)). '{theoryData.Digest}' != Expected result from: '{theoryData.Algorithm}'.");
                }

                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
        public void IsSymmetricKeyWrapSupported(SupportedAlgorithmTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.IsSymmetricKeyWrapSupported", theoryData);

            try
            {
                if (SupportedAlgorithms.IsSupportedSymmetricKeyWrap(theoryData.Algorithm, theoryData.SecurityKey) != theoryData.IsSupportedAlgorithm)
                {
                    context.AddDiff($"SupportedAlgorithms.IsSymmetricKeyWrapSupported != theoryData.IsSupportedAlgorithm. Algorithm: '{theoryData.Algorithm}', theoryData.SecurityKey: '{theoryData.SecurityKey}', theoryData.IsSupportedAlgorithm: '{theoryData.IsSupportedAlgorithm}'.");
                }

                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
Esempio n. 5
0
 public CustomCryptoProviderFactory(string[] supportedAlgorithms)
 {
     SupportedAlgorithms.AddRange(supportedAlgorithms);
 }
Esempio n. 6
0
        public void AsymmetricAdapterUsageTests(AsymmetricAdatperTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.AsymmetricAdapterUsageTests", theoryData);

            byte[]        bytes         = Encoding.UTF8.GetBytes("var context = TestUtilities.WriteHeader($'{ this}.AsymmetricAdapterUsageTests', theoryData);");
            HashAlgorithm hashAlgorithm = CryptoProviderFactory.Default.CreateHashAlgorithm(theoryData.HashAlorithmString);

            try
            {
#if NET461 || NETCOREAPP2_1
                AsymmetricAdapter asymmetricdapter = new AsymmetricAdapter(theoryData.SecurityKey, theoryData.Algorithm, hashAlgorithm, SupportedAlgorithms.GetHashAlgorithmName(theoryData.Algorithm), true);
#else
                AsymmetricAdapter asymmetricdapter = new AsymmetricAdapter(theoryData.SecurityKey, theoryData.Algorithm, hashAlgorithm, true);
#endif
                byte[] signature = asymmetricdapter.Sign(bytes);
                if (!asymmetricdapter.Verify(bytes, signature))
                {
                    context.AddDiff($"Verify failed for test: {theoryData.TestId}");
                }

                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }