예제 #1
0
        public void InitializedWithAlgorithmFromDITest()
        {
            try
            {
                InitializeHashAlgorithmTest();

                var target = new HashAlgorithmFactory();

                target.Initialize();
                var hasher1 = target.Create();

                Assert.IsNotNull(hasher1);

                var hasher2 = target.Create();

                Assert.IsNotNull(hasher2);
                Assert.IsFalse(object.ReferenceEquals(hasher1, hasher2));
                Assert.IsInstanceOfType(hasher1, typeof(MD5));
                Assert.IsInstanceOfType(hasher2, typeof(MD5));
            }
            finally
            {
                CleanupTest();
            }
        }
예제 #2
0
        public void InitializedTest5()
        {
            try
            {
                ServiceLocatorWrapper.Reset();
                _unityServiceLocator = new UnityServiceLocator(
                    new UnityContainer()
                    /*.RegisterInstance<string>(Algorithms.Hash.ResolveName, "SHA1")*/
                    /*.RegisterType<HashAlgorithm, SHA512Cng>()*/);
                ServiceLocator.SetLocatorProvider(() => _unityServiceLocator);

                var target = new HashAlgorithmFactory();

                target.Initialize("SHA384");

                var hasher = target.Create();

                Assert.IsNotNull(hasher);
                Assert.IsInstanceOfType(hasher, typeof(SHA384));
            }
            finally
            {
                CleanupTest();
            }
        }
예제 #3
0
        protected override void ProcessRecordInEH()
        {
            string encodingName = EncodingName;

            if (string.IsNullOrWhiteSpace(encodingName))
            {
                encodingName = EncodingFactory.EncodingNames.UTF8;
            }

            Encoding encoding = EncodingFactory.Get(encodingName);

            if (encoding == null)
            {
                throw new PSArgumentException(string.Format("Unsupported encoding: {0}", encodingName));
            }

            HashAlgorithm algorithm = HashAlgorithmFactory.Create(Algorithm);

            if (algorithm == null)
            {
                throw new PSArgumentException(string.Format("Unsupported algorithm: {0}", Algorithm));
            }

            byte[] hashBuffer = HashGenerator.ComputeStringHash(InputObject, algorithm, encoding);

            HashResult result = new HashResult()
            {
                Algorithm  = Algorithm,
                Hash       = hashBuffer.ToHex(),
                HashBuffer = hashBuffer,
            };

            WriteObject(result);
        }
예제 #4
0
        public void HashAlgorithmFactoryTest1()
        {
            var algorithm = HashAlgorithmFactory.Create("md5");

            Assert.Equal(typeof(System.Security.Cryptography.MD5CryptoServiceProvider), algorithm.GetType());
            algorithm = HashAlgorithmFactory.Create("MD5");
            Assert.Equal(typeof(System.Security.Cryptography.MD5CryptoServiceProvider), algorithm.GetType());

            algorithm = HashAlgorithmFactory.Create("foo");
            Assert.Null(algorithm);

            Assert.Throws <ArgumentException>(() => { HashAlgorithmFactory.Create(""); });
        }
예제 #5
0
        public void UninitializedTest()
        {
            try
            {
                var target = new HashAlgorithmFactory();

                target.Create();
            }
            finally
            {
                CleanupTest();
            }
        }
예제 #6
0
        public void InitializedWithGoodNameTest()
        {
            try
            {
                InitializeHashNameTest("MD5");

                var target = new HashAlgorithmFactory();

                target.Initialize("SHA1");
                var hasher1 = target.Create();

                Assert.IsNotNull(hasher1);
                Assert.AreEqual("SHA1", target.HashAlgorithmName);

                var hasher2 = target.Create();

                Assert.IsNotNull(hasher2);
                Assert.AreNotEqual(hasher1, hasher2);
            }
            finally
            {
                CleanupTest();
            }
        }
        public async Task EnsureHeader(HttpRequestMessage request, SigningSettings signingSettings, DateTimeOffset timeOfSigning)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (signingSettings == null)
            {
                throw new ArgumentNullException(nameof(signingSettings));
            }

            if (!request.Method.SupportsBody())
            {
                return;
            }
            if (string.IsNullOrEmpty(signingSettings.DigestHashAlgorithm.Name))
            {
                return;
            }
            if (request.Headers.Contains("Digest"))
            {
                return;
            }

            if (request.Content == null)
            {
                request.Headers.Add("Digest", string.Empty);
                return;
            }

            var bodyBytes = await request.Content.ReadAsByteArrayAsync().ConfigureAwait(false);

            using (var hashAlgorithm = HashAlgorithmFactory.Create(signingSettings.DigestHashAlgorithm)) {
                if (hashAlgorithm == null)
                {
                    throw new NotSupportedException($"The specified hash algorithm ({signingSettings.DigestHashAlgorithm.Name}) for digest is currently not supported.");
                }
                var payloadBytes        = hashAlgorithm.ComputeHash(bodyBytes);
                var digest              = _base64Converter.ToBase64(payloadBytes);
                var digestAlgorithmName = Constants.DigestHashAlgorithmNames[signingSettings.DigestHashAlgorithm.Name];
                request.Headers.Add("Digest", $"{digestAlgorithmName}={digest}");
            }
        }
예제 #8
0
 new static public Bkdr Create(string hashName)
 {
     return((Bkdr)HashAlgorithmFactory.Create(hashName));
 }
예제 #9
0
 public static new SuperFast Create(string hashName)
 {
     return((SuperFast)HashAlgorithmFactory.Create(hashName));
 }
예제 #10
0
 new static public Dek Create(string hashName)
 {
     return((Dek)HashAlgorithmFactory.Create(hashName));
 }
예제 #11
0
 public static new Fnv1a64 Create(string hashName)
 {
     return((Fnv1a64)HashAlgorithmFactory.Create(hashName));
 }
예제 #12
0
 new static public APHash Create(string hashName)
 {
     return((APHash)HashAlgorithmFactory.Create(hashName));
 }
예제 #13
0
 public static new MD5 Create(string hashName)
 {
     return((MD5)HashAlgorithmFactory.Create(hashName));
 }
예제 #14
0
 public static new Pjw Create(string hashName)
 {
     return((Pjw)HashAlgorithmFactory.Create(hashName));
 }
예제 #15
0
 public static new Sdbm Create(string hashName)
 {
     return((Sdbm)HashAlgorithmFactory.Create(hashName));
 }
예제 #16
0
 public static new Joaat Create(string hashName)
 {
     return((Joaat)HashAlgorithmFactory.Create(hashName));
 }
예제 #17
0
 public static new Adler32 Create(string hashName)
 {
     return((Adler32)HashAlgorithmFactory.Create(hashName));
 }
예제 #18
0
        public override SignatureVerificationFailure VerifySync(HttpRequestForSigning signedRequest, Signature signature, Client client)
        {
            if (signature.Headers.Contains(HeaderName.PredefinedHeaderNames.Digest) &&
                !signedRequest.Headers.Contains(HeaderName.PredefinedHeaderNames.Digest))
            {
                return(SignatureVerificationFailure.HeaderMissing($"The {HeaderName.PredefinedHeaderNames.Digest} header is indicated as part of the signature, but it is not included in the request."));
            }

            if (!signedRequest.Headers.Contains(HeaderName.PredefinedHeaderNames.Digest))
            {
                _logger?.LogDebug("{0} header verification is not required, because it is not present in the request to verify.", HeaderName.PredefinedHeaderNames.Digest);
                return(null);
            }

            if (signedRequest.Body == null)
            {
                return(SignatureVerificationFailure.InvalidDigestHeader($"The {HeaderName.PredefinedHeaderNames.Digest} header verification failed. The request has no body."));
            }

            var digestHeaderValue = signedRequest.Headers.GetValues(HeaderName.PredefinedHeaderNames.Digest).FirstOrDefault();
            var digestParams      = new List <string>();

            if (!string.IsNullOrEmpty(digestHeaderValue))
            {
                var separatorIndex = digestHeaderValue.IndexOf('=');
                if (separatorIndex < 0 || separatorIndex >= digestHeaderValue.Length - 1)
                {
                    digestParams.Add(digestHeaderValue);
                }
                else
                {
                    digestParams.Add(digestHeaderValue.Substring(0, separatorIndex));
                    digestParams.Add(digestHeaderValue.Substring(separatorIndex + 1));
                }
            }

            if (digestParams.Count < 2)
            {
                return(SignatureVerificationFailure.InvalidDigestHeader($"The {HeaderName.PredefinedHeaderNames.Digest} request header is invalid."));
            }

            if (!Constants.DigestHashAlgorithms.TryGetValue(digestParams[0], out var digestAlgorithmName))
            {
                return(SignatureVerificationFailure.InvalidDigestHeader($"The {HeaderName.PredefinedHeaderNames.Digest} algorithm name ({digestParams[0] ?? "[null]"}) is invalid."));
            }

            using (var hashAlgorithm = HashAlgorithmFactory.Create(new HashAlgorithmName(digestAlgorithmName))) {
                if (hashAlgorithm == null)
                {
                    return(SignatureVerificationFailure.InvalidDigestHeader($"The {HeaderName.PredefinedHeaderNames.Digest} algorithm name ({digestParams[0] ?? "[null]"}) is currently not supported."));
                }

                var payloadBytes     = hashAlgorithm.ComputeHash(signedRequest.Body);
                var calculatedDigest = _base64Converter.ToBase64(payloadBytes);
                var receivedDigest   = digestParams[1];

                if (calculatedDigest != receivedDigest)
                {
                    return(SignatureVerificationFailure.InvalidDigestHeader("The digest header verification failed."));
                }
            }

            return(null);
        }
예제 #19
0
 new static public Elf Create(string hashName)
 {
     return((Elf)HashAlgorithmFactory.Create(hashName));
 }