Пример #1
0
 private static byte[] ComputeHash(HashAlgName algorithmName, byte[] bytes)
 {
     using (var provider = new HashCrypto(algorithmName))
     {
         return(provider.ComputeHash(bytes));
     }
 }
Пример #2
0
        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}'.");
            }
        }
Пример #3
0
        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));
        }
Пример #4
0
        public HmacHashCrypto(HashAlgName algorithmName, byte[] key)
        {
            AssertUtil.ArgumentNotEmpty(key, nameof(key));

            this.AlgorithmName = algorithmName;
            this.Key           = key;
            this.provider      = CreateProvider(algorithmName, key);
        }
Пример #5
0
        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}'.");
            }
        }
Пример #6
0
        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)));
        }
Пример #7
0
        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}'.");
            }
        }
Пример #8
0
        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}'.");
            }
        }
Пример #9
0
        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}'.");
            }
        }
Пример #10
0
        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)));
        }
Пример #11
0
        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));
        }
Пример #12
0
        public byte[] SignData(byte[] bytes, HashAlgName hashAlgorithm)
        {
            AssertUtil.ArgumentNotNull(bytes, nameof(bytes));

            return(SignData(bytes, 0, bytes.Length, hashAlgorithm));
        }
Пример #13
0
 public HashCrypto(HashAlgName algorithmName)
 {
     this.AlgorithmName = algorithmName;
     this.provider      = CreateProvider(algorithmName);
 }