private static byte[] ComputeHash(HashAlgName algorithmName, byte[] bytes) { using (var provider = new HashCrypto(algorithmName)) { return(provider.ComputeHash(bytes)); } }
public bool VerifyData(Stream stream, byte[] signature, HashAlgName hashAlgorithm) { ThrowIfObjectDisposed(); AssertUtil.ArgumentNotNull(stream, nameof(stream)); AssertUtil.ArgumentNotNull(signature, nameof(signature)); if (!stream.CanRead) { throw new ArgumentException($"Argument '{nameof(stream)}' cannot be read."); } switch (this.AlgorithmName) { case AsymmetricAlgName.RSA: return((this.provider as System.Security.Cryptography.RSA).VerifyData(stream, signature, ToSysHashAlgorithmName(hashAlgorithm), System.Security.Cryptography.RSASignaturePadding.Pkcs1)); case AsymmetricAlgName.DSA: return((this.provider as System.Security.Cryptography.DSA).VerifyData(stream, signature, ToSysHashAlgorithmName(hashAlgorithm))); case AsymmetricAlgName.ECDsa: return((this.provider as System.Security.Cryptography.ECDsa).VerifyData(stream, signature, ToSysHashAlgorithmName(hashAlgorithm))); default: throw new NotSupportedException($"Unsupported asymmetric algorithm '{this.AlgorithmName}'."); } }
public bool VerifyData(byte[] bytes, byte[] signature, HashAlgName hashAlgorithm) { AssertUtil.ArgumentNotNull(bytes, nameof(bytes)); AssertUtil.ArgumentNotNull(signature, nameof(signature)); return(VerifyData(bytes, 0, bytes.Length, signature, hashAlgorithm)); }
public HmacHashCrypto(HashAlgName algorithmName, byte[] key) { AssertUtil.ArgumentNotEmpty(key, nameof(key)); this.AlgorithmName = algorithmName; this.Key = key; this.provider = CreateProvider(algorithmName, key); }
public bool VerifyData(byte[] bytes, int index, int count, byte[] signature, HashAlgName hashAlgorithm) { ThrowIfObjectDisposed(); if (bytes == null) { throw new ArgumentNullException(nameof(bytes)); } if (signature == null) { throw new ArgumentNullException(nameof(signature)); } if (index < 0) { throw new ArgumentOutOfRangeException(nameof(index), $"Argument '{nameof(index)}' value must be >= 0."); } if (index >= bytes.Length) { throw new ArgumentOutOfRangeException(nameof(index), $"Argument '{nameof(index)}' value exceeds the maximum length of argument '{nameof(bytes)}'."); } if (count < 0) { throw new ArgumentOutOfRangeException(nameof(count), $"Argument '{nameof(count)}' value must be >= 0."); } if (index + count > bytes.Length) { throw new ArgumentOutOfRangeException(nameof(count), $"Argument '{nameof(index)} + {nameof(count)}' value exceeds the maximum length of argument '{nameof(bytes)}'."); } switch (this.AlgorithmName) { case AsymmetricAlgName.RSA: return((this.provider as System.Security.Cryptography.RSA).VerifyData(bytes, signature, ToSysHashAlgorithmName(hashAlgorithm), System.Security.Cryptography.RSASignaturePadding.Pkcs1)); case AsymmetricAlgName.DSA: return((this.provider as System.Security.Cryptography.DSA).VerifyData(bytes, signature, ToSysHashAlgorithmName(hashAlgorithm))); case AsymmetricAlgName.ECDsa: return((this.provider as System.Security.Cryptography.ECDsa).VerifyData(bytes, signature, ToSysHashAlgorithmName(hashAlgorithm))); default: throw new NotSupportedException($"Unsupported asymmetric algorithm '{this.AlgorithmName}'."); } }
public bool VerifyData(Stream stream, byte[] signature, HashAlgName hashAlgorithm, SignaturePaddingMode padding) { ThrowIfObjectDisposed(); if (this.AlgorithmName != AsymmetricAlgName.RSA) { throw new InvalidOperationException($"Only supported by RSA."); } AssertUtil.ArgumentNotNull(stream, nameof(stream)); if (!stream.CanRead) { throw new ArgumentException($"Argument '{nameof(stream)}' cannot be read."); } return((this.provider as System.Security.Cryptography.RSA).VerifyData(stream, signature, ToSysHashAlgorithmName(hashAlgorithm), ToSysSignaturePaddingMode(padding))); }
static System.Security.Cryptography.HashAlgorithmName ToSysHashAlgorithmName(HashAlgName hashAlgorithm) { switch (hashAlgorithm) { case HashAlgName.MD5: return(System.Security.Cryptography.HashAlgorithmName.MD5); case HashAlgName.SHA1: return(System.Security.Cryptography.HashAlgorithmName.SHA1); case HashAlgName.SHA256: return(System.Security.Cryptography.HashAlgorithmName.SHA256); case HashAlgName.SHA384: return(System.Security.Cryptography.HashAlgorithmName.SHA384); case HashAlgName.SHA512: return(System.Security.Cryptography.HashAlgorithmName.SHA512); default: throw new NotSupportedException($"Unsupported hash algorithm '{hashAlgorithm}'."); } }
private System.Security.Cryptography.HashAlgorithm CreateProvider(HashAlgName algorithmName) { switch (algorithmName) { case HashAlgName.MD5: return(System.Security.Cryptography.MD5.Create()); case HashAlgName.SHA1: return(System.Security.Cryptography.SHA1.Create()); case HashAlgName.SHA256: return(System.Security.Cryptography.SHA256.Create()); case HashAlgName.SHA384: return(System.Security.Cryptography.SHA384.Create()); case HashAlgName.SHA512: return(System.Security.Cryptography.SHA512.Create()); default: throw new NotSupportedException($"Unsupported hash algorithm '{algorithmName}'."); } }
private System.Security.Cryptography.HMAC CreateProvider(HashAlgName algorithmName, byte[] key) { switch (algorithmName) { case HashAlgName.MD5: return(new System.Security.Cryptography.HMACMD5(this.Key)); case HashAlgName.SHA1: return(new System.Security.Cryptography.HMACSHA1(this.Key)); case HashAlgName.SHA256: return(new System.Security.Cryptography.HMACSHA256(this.Key)); case HashAlgName.SHA384: return(new System.Security.Cryptography.HMACSHA384(this.Key)); case HashAlgName.SHA512: return(new System.Security.Cryptography.HMACSHA512(this.Key)); default: throw new NotSupportedException($"Unsupported hash algorithm '{algorithmName}'."); } }
public bool VerifyData(byte[] bytes, int index, int count, byte[] signature, HashAlgName hashAlgorithm, SignaturePaddingMode padding) { ThrowIfObjectDisposed(); if (this.AlgorithmName != AsymmetricAlgName.RSA) { throw new InvalidOperationException($"Only supported by RSA."); } if (bytes == null) { throw new ArgumentNullException(nameof(bytes)); } if (index < 0) { throw new ArgumentOutOfRangeException(nameof(index), $"Argument '{nameof(index)}' value must be >= 0."); } if (index >= bytes.Length) { throw new ArgumentOutOfRangeException(nameof(index), $"Argument '{nameof(index)}' value exceeds the maximum length of argument '{nameof(bytes)}'."); } if (count < 0) { throw new ArgumentOutOfRangeException(nameof(count), $"Argument '{nameof(count)}' value must be >= 0."); } if (index + count > bytes.Length) { throw new ArgumentOutOfRangeException(nameof(count), $"Argument '{nameof(index)} + {nameof(count)}' value exceeds the maximum length of argument '{nameof(bytes)}'."); } return((this.provider as System.Security.Cryptography.RSA).VerifyData(bytes, index, count, signature, ToSysHashAlgorithmName(hashAlgorithm), ToSysSignaturePaddingMode(padding))); }
public bool VerifyData(byte[] bytes, byte[] signature, HashAlgName hashAlgorithm, SignaturePaddingMode padding) { AssertUtil.ArgumentNotNull(bytes, nameof(bytes)); return(VerifyData(bytes, 0, bytes.Length, signature, hashAlgorithm, padding)); }
public byte[] SignData(byte[] bytes, HashAlgName hashAlgorithm) { AssertUtil.ArgumentNotNull(bytes, nameof(bytes)); return(SignData(bytes, 0, bytes.Length, hashAlgorithm)); }
public HashCrypto(HashAlgName algorithmName) { this.AlgorithmName = algorithmName; this.provider = CreateProvider(algorithmName); }