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(); } }
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(); } }
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); }
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(""); }); }
public void UninitializedTest() { try { var target = new HashAlgorithmFactory(); target.Create(); } finally { CleanupTest(); } }
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}"); } }
new static public Bkdr Create(string hashName) { return((Bkdr)HashAlgorithmFactory.Create(hashName)); }
public static new SuperFast Create(string hashName) { return((SuperFast)HashAlgorithmFactory.Create(hashName)); }
new static public Dek Create(string hashName) { return((Dek)HashAlgorithmFactory.Create(hashName)); }
public static new Fnv1a64 Create(string hashName) { return((Fnv1a64)HashAlgorithmFactory.Create(hashName)); }
new static public APHash Create(string hashName) { return((APHash)HashAlgorithmFactory.Create(hashName)); }
public static new MD5 Create(string hashName) { return((MD5)HashAlgorithmFactory.Create(hashName)); }
public static new Pjw Create(string hashName) { return((Pjw)HashAlgorithmFactory.Create(hashName)); }
public static new Sdbm Create(string hashName) { return((Sdbm)HashAlgorithmFactory.Create(hashName)); }
public static new Joaat Create(string hashName) { return((Joaat)HashAlgorithmFactory.Create(hashName)); }
public static new Adler32 Create(string hashName) { return((Adler32)HashAlgorithmFactory.Create(hashName)); }
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); }
new static public Elf Create(string hashName) { return((Elf)HashAlgorithmFactory.Create(hashName)); }