Beispiel #1
0
        /// <summary>
        /// Verify the given data and signature using RSA.
        /// </summary>
        public static bool VerifySignedHashUsingRSA(byte[] dataToVerify, byte[] signedData,
                                                    string keyContainerName, HashAlgorithmTypeEnum hashType)
        {
            RSACryptoServiceProvider rsa = AsymmetricOperation.GetRSACryptoServiceProvider(keyContainerName);

            return(rsa.VerifyData(dataToVerify, HashOperation.CreateHashAlgorithmProvider(hashType), signedData));
        }
 /// <summary>
 /// Creates a algorithm specific .NET implementation of the hash algorithm provider.
 /// </summary>
 public static HashAlgorithm CreateHashAlgorithmProvider(HashAlgorithmTypeEnum algType)
 {
     if (algType == HashAlgorithmTypeEnum.MD5HashAlgorithm)
     {
         return(new MD5CryptoServiceProvider());
     }
     else if (algType == HashAlgorithmTypeEnum.SHA1HashAlgorithm)
     {
         return(new SHA1CryptoServiceProvider());
     }
     else if (algType == HashAlgorithmTypeEnum.SHA256HashAlgorithm)
     {
         return(new SHA256Managed());
     }
     else if (algType == HashAlgorithmTypeEnum.SHA384HashAlgorithm)
     {
         return(new SHA384Managed());
     }
     else if (algType == HashAlgorithmTypeEnum.SHA512HashAlgorithm)
     {
         return(new SHA512Managed());
     }
     else
     {
         return(new SHA1CryptoServiceProvider());
     }
 }
Beispiel #3
0
        /// <summary>
        /// Hash the given data and sign it using RSA.
        /// </summary>
        public static byte[] HashAndSignUsingRSA(byte[] dataToSign, string keyContainerName,
                                                 HashAlgorithmTypeEnum hashType)
        {
            RSACryptoServiceProvider rsa = AsymmetricOperation.GetRSACryptoServiceProvider(keyContainerName);

            return(rsa.SignData(dataToSign, HashOperation.CreateHashAlgorithmProvider(hashType)));
        }
Beispiel #4
0
        /// <summary>
        /// Verify the given data and signature using RSA.
        /// </summary>
        public static bool VerifySignedHashUsingDSA(byte[] dataToVerify, byte[] signedData,
                                                    string keyContainerName, HashAlgorithmTypeEnum hashType)
        {
            DSACryptoServiceProvider dsa = AsymmetricOperation.GetDSACryptoServiceProvider(keyContainerName);

            byte[] hashedData = HashOperation.CreateHashAlgorithmProvider(hashType).ComputeHash(dataToVerify);
            return(dsa.VerifySignature(hashedData, signedData));
        }
        /// <summary>
        /// Create random salt for a given hash algorithm provider.
        /// </summary>
        public static string CreateRandomSalt(HashAlgorithmTypeEnum algType)
        {
            HashAlgorithm            alg = CreateHashAlgorithmProvider(algType);
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            byte[] bufSalt = new byte[alg.HashSize / 10];
            rng.GetNonZeroBytes(bufSalt);
            return(Convert.ToBase64String(bufSalt));
        }
Beispiel #6
0
        /// <summary>
        /// 获取 二进制数组的 哈希值
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="offset">偏移量 默认值 0 不偏移</param>
        /// <param name="hashAlgorithmType">哈希散列算法类型 默认使用 SHA256</param>
        /// <returns></returns>
        public static string GetBytesHashCode(byte[] bytes, int offset = 0, HashAlgorithmTypeEnum hashAlgorithmType = HashAlgorithmTypeEnum.SHA256)
        {
            string hashString = string.Empty;

            using (MemoryStream ms = new MemoryStream(bytes))
            {
                hashString = GetStreamHashCode(ms, offset, hashAlgorithmType);
            }
            return(hashString);
        }
Beispiel #7
0
        /// <summary>
        /// 获取数据流的哈希值
        /// </summary>
        /// <param name="inputStream"></param>
        /// <param name="offset">偏移量 默认值 0 不偏移</param>
        /// <param name="hashAlgorithmType">哈希散列算法类型 默认使用 SHA256</param>
        /// <returns></returns>
        public static string GetStreamHashCode(Stream inputStream, int offset = 0, HashAlgorithmTypeEnum hashAlgorithmType = HashAlgorithmTypeEnum.SHA256)
        {
            string hashString = string.Empty;

            using (var hash = HashAlgorithm.Create(hashAlgorithmType.ToString()))
            {
                if (offset >= 0 && offset < inputStream.Length)
                {
                    inputStream.Position = offset;
                }
                var bytes = hash.ComputeHash(inputStream);
                hashString = BitConverter.ToString(bytes).Replace("-", "");
            }

            return(hashString);
        }
 /// <summary>
 /// Create hash for a plainText using the given hash algorithm type and salt.
 /// </summary>
 public static string ComputeHash(HashAlgorithmTypeEnum algType, string plainText, string salt)
 {
     return(ComputeHash(CreateHashAlgorithmProvider(algType), plainText, salt));
 }
Beispiel #9
0
        /// <summary>
        /// Hash the given data and sign it using DSA.
        /// </summary>
        public static byte[] HashAndSignUsingDSA(byte[] dataToSign, string keyContainerName, HashAlgorithmTypeEnum hashType)
        {
            DSACryptoServiceProvider dsa = AsymmetricOperation.GetDSACryptoServiceProvider(keyContainerName);

            byte[] hashedData = HashOperation.CreateHashAlgorithmProvider(hashType).ComputeHash(dataToSign);
            return(dsa.CreateSignature(hashedData));
        }