Ejemplo n.º 1
0
 /// <summary>
 /// 48字节,384位
 /// </summary>
 /// <param name="str"></param>
 /// <returns></returns>
 public static string SHA384(string str)
 {
     byte[] buffer = Encoding.UTF8.GetBytes(str);
     SHA384CryptoServiceProvider SHA384 = new SHA384CryptoServiceProvider();
     byte[] byteArr = SHA384.ComputeHash(buffer);
     return BitConverter.ToString(byteArr);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// SHA1编码
        /// </summary>
        /// <param name="value">明文</param>
        /// <param name="bit">位长</param>
        /// <returns></returns>
        public static string Get(string value, SHA1Bit bit)
        {
            StringBuilder sBuilder = new StringBuilder();

            if (bit == SHA1Bit.L160)
            {
                System.Security.Cryptography.SHA1 sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();
                // This is one implementation of the abstract class SHA1.
                byte[] result = sha.ComputeHash(Encoding.Default.GetBytes(value));
                sBuilder.Append(BitConverter.ToString(result).Replace("-", ""));
            }
            if (bit == SHA1Bit.L256)
            {
                System.Security.Cryptography.SHA256 sha256 = new System.Security.Cryptography.SHA256CryptoServiceProvider();
                // This is one implementation of the abstract class SHA1.
                byte[] result = sha256.ComputeHash(Encoding.Default.GetBytes(value));
                sBuilder.Append(BitConverter.ToString(result).Replace("-", ""));
            }
            if (bit == SHA1Bit.L384)
            {
                System.Security.Cryptography.SHA384 sha384 = new System.Security.Cryptography.SHA384CryptoServiceProvider();
                // This is one implementation of the abstract class SHA1.
                byte[] result = sha384.ComputeHash(Encoding.Default.GetBytes(value));
                sBuilder.Append(BitConverter.ToString(result).Replace("-", ""));
            }
            return(sBuilder.ToString());
        }
Ejemplo n.º 3
0
        private static string Hash(string password)
        {
            var provider = new SHA384CryptoServiceProvider();
            var passBytes = GetBytes(password);
            var hash = provider.ComputeHash(passBytes);
            var hashString = GetString(hash);

            return hashString;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Calculates the SHA384-hash of the given string
        /// </summary>
        /// <returns>Hexadecimal representation of the SHA384-hash.</returns>
        /// <param name="str">Input string.</param>
        public static string SHA384(string str)
        {
            var bytes = Encoding.ASCII.GetBytes (str);
            byte[] hash;

            // We're using the native SHA384 implementation here.
            using (var hasher = new SHA384CryptoServiceProvider ()) {
                hash = hasher.ComputeHash (bytes);
            }

            return hash.ToHex ();
        }
Ejemplo n.º 5
0
        public static string GetSHA384(byte[] data)
        {
            System.Security.Cryptography.SHA384 sha = new System.Security.Cryptography.SHA384CryptoServiceProvider();
            byte[] bytResult = sha.ComputeHash(data);
            //转换成字符串,32位
            string strResult = BitConverter.ToString(bytResult);

            //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉
            strResult = strResult.Replace("-", "");

            return(strResult);
        }
        /// <summary>
        /// Hashes a stream with SHA-384.
        /// </summary>
        /// <param name="stream">The stream to hash.</param>
        /// <returns>The hash.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="stream" /> is <see langword="null" />.
        /// </exception>
        public static byte[] SHA384(this Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (var hasher = new SHA384CryptoServiceProvider())
            {
                return hasher.ComputeHash(stream);
            }
        }
Ejemplo n.º 7
0
        public static string Sha384Encode(string pwd)
        {
            SHA384 sha384 = new SHA384CryptoServiceProvider();//建立一個SHA384

            byte[] source = Encoding.Default.GetBytes(pwd);//將字串轉為Byte[]

            byte[] crypto = sha384.ComputeHash(source);//進行SHA384加密

            string result = Convert.ToBase64String(crypto);//把加密後的字串從Byte[]轉為字串

            return result;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 获取文本SHA384
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string GetTextSHA_384(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return("");
            }
            System.Security.Cryptography.SHA384CryptoServiceProvider SHA384CSP = new System.Security.Cryptography.SHA384CryptoServiceProvider();
            byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(text);
            byte[] bytHash  = SHA384CSP.ComputeHash(bytValue);
            SHA384CSP.Clear();

            //根据计算得到的Hash码翻译为SHA-1码
            string sHash = ConvertHashBytes(bytHash);

            //根据大小写规则决定返回的字符串
            return(sHash.ToLower());
        }
Ejemplo n.º 9
0
 /// <summary>
 /// 获取字符串的 40 位 SHA384 大写。
 /// </summary>
 /// <param name="input">需计算 SHA384 的字符串。</param>
 /// <param name="prefix">需添加的字符串前缀。</param>
 /// <returns>40 位 SHA384 大写。</returns>
 /// <exception cref="ArgumentNullException"><c>input</c> 为 null。</exception>
 public static string GetStringSHA386(string input, string prefix = "")
 {
     if (input == null)
     {
         throw new ArgumentNullException("input", "input 不能为空。");
     }
     using (SHA384CryptoServiceProvider sha384Csp = new SHA384CryptoServiceProvider())
     {
         byte[] bytes = sha384Csp.ComputeHash(Encoding.UTF8.GetBytes(prefix + input));
         StringBuilder sb = new StringBuilder(40);
         foreach (var temp in bytes)
         {
             sb.AppendFormat("{0:X2}", temp);
         }
         return sb.ToString();
     }
 }
Ejemplo n.º 10
0
        /// <summary>
        /// 计算SHA-384码
        /// </summary>
        /// <param name="word">字符串</param>
        /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
        /// <returns></returns>
        public static string Hash_SHA_384(string word, bool toUpper = true)
        {
            try
            {
                System.Security.Cryptography.SHA384CryptoServiceProvider SHA384CSP
                    = new System.Security.Cryptography.SHA384CryptoServiceProvider();

                byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
                byte[] bytHash  = SHA384CSP.ComputeHash(bytValue);
                SHA384CSP.Clear();

                //根据计算得到的Hash码翻译为SHA-1码
                string sHash = "", sTemp = "";
                for (int counter = 0; counter < bytHash.Count(); counter++)
                {
                    long i = bytHash[counter] / 16;
                    if (i > 9)
                    {
                        sTemp = ((char)(i - 10 + 0x41)).ToString();
                    }
                    else
                    {
                        sTemp = ((char)(i + 0x30)).ToString();
                    }
                    i = bytHash[counter] % 16;
                    if (i > 9)
                    {
                        sTemp += ((char)(i - 10 + 0x41)).ToString();
                    }
                    else
                    {
                        sTemp += ((char)(i + 0x30)).ToString();
                    }
                    sHash += sTemp;
                }

                //根据大小写规则决定返回的字符串
                return(toUpper ? sHash : sHash.ToLower());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Calculate the hash value based on the hash algorith type  selected by the user
        /// Default to SHA1
        /// This result from is used for both generating a hash value as well as validating the file hash
        /// </summary>
        /// <param name="file">file to get the hash of</param>
        /// <param name="hashAlgo">hash algorithm to use</param>
        public string[] CalculateHash(string file, string hashAlgo)
        {
            string caseSwitch = hashAlgo;
            resultToReturn = new string[2];
            string hashValue = String.Empty;
            FileStream fileStream = File.Open(file, FileMode.Open);
            fileStream.Position = 0;
            try
            {
                switch (caseSwitch)
                {
                    case "MD5":
                        MD5 objMd5 = new MD5CryptoServiceProvider();
                        hashValue = BitConverter.ToString(objMd5.ComputeHash(fileStream)).Replace("-", String.Empty).ToLower();
                        break;
                    case "SHA-256":
                        SHA256 objSha256 = new SHA256CryptoServiceProvider();
                        hashValue = BitConverter.ToString(objSha256.ComputeHash(fileStream)).Replace("-", String.Empty).ToLower();
                        break;
                    case "SHA-384":
                        SHA384 objSha384 = new SHA384CryptoServiceProvider();
                        hashValue = BitConverter.ToString(objSha384.ComputeHash(fileStream)).Replace("-", String.Empty).ToLower();
                        break;
                    default:
                        SHA1 objSha1 = new SHA1CryptoServiceProvider();
                        hashValue = BitConverter.ToString(objSha1.ComputeHash(fileStream)).Replace("-", String.Empty).ToLower();
                        break;
                }

                resultToReturn[0] = SUCCESS;
                resultToReturn[1] = hashValue;
            }
            catch
            {
                resultToReturn[0] = FAIL;
                resultToReturn[1] = ERROR_MSG;
            }
            finally
            {
                fileStream.Close();
            }

            return resultToReturn;
        }
Ejemplo n.º 12
0
 /// <summary>
 /// 获取文件的 40 位 SHA384 大写。
 /// </summary>
 /// <param name="filePath">需计算 SHA384 的文件。</param>
 /// <returns>40 位 SHA384 大写。</returns>
 /// <exception cref="FileNotFoundException"><c>filePath</c> 不存在。</exception>
 public static string GetFileSHA384(string filePath)
 {
     if (File.Exists(filePath) == false)
     {
         throw new FileNotFoundException("文件不存在!", filePath);
     }
     using (SHA384CryptoServiceProvider sha384Csp = new SHA384CryptoServiceProvider())
     {
         using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
         {
             byte[] bytes = sha384Csp.ComputeHash(fs);
             StringBuilder sb = new StringBuilder(40);
             foreach (var temp in bytes)
             {
                 sb.AppendFormat("{0:X2}", temp);
             }
             return sb.ToString();
         }
     }
 }
Ejemplo n.º 13
0
        public static string SHA384(byte[] bytes) {
            System.Diagnostics.Debug.Assert((bytes != null && bytes.Length > 0), "The bytes parameter is null/empty!");

            try {
                using (SHA384CryptoServiceProvider csp = new SHA384CryptoServiceProvider()) {
                    byte[] data = csp.ComputeHash(bytes);
                    return ToHex(data, false);
                }
            } catch (Exception ex) {
                throw new ArgumentException("SHA384.Hash Error -> " + ex.Message, ex);
            }
        }
Ejemplo n.º 14
0
 private string GetSha384Password(string input)
 {
     System.Security.Cryptography.SHA384 sha1 = new System.Security.Cryptography.SHA384CryptoServiceProvider();
     return(Encoding.ASCII.GetString(sha1.ComputeHash(Encoding.ASCII.GetBytes(input))));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// sha-384
 /// </summary>
 /// <param name="s"></param>
 /// <returns></returns>
 public static string Sha384(string s)
 {
     SHA384 sha1 = new SHA384CryptoServiceProvider();
     var encryptedBytes = sha1.ComputeHash(s.ToAsciiBytes());
     var sb = new StringBuilder();
     foreach (var t in encryptedBytes) sb.AppendFormat("{0:x2}", t);
     return sb.ToString().ToLower();
 }
Ejemplo n.º 16
0
 public static string HashSHA384(string phrase)
 {
     if (phrase == null)
         return null;
     var encoder = new UTF8Encoding();
     var sha384Hasher = new SHA384CryptoServiceProvider();
     var hashedDataBytes = sha384Hasher.ComputeHash(encoder.GetBytes(phrase));
     return ByteArrayToHexString(hashedDataBytes);
 }
Ejemplo n.º 17
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            // Used to build the hash output
            StringBuilder sBuilder = new StringBuilder();

            // Get the plain text from the user
            String input = this.tbPlainText.Text;

            // Will hold the bytes of the encoded hash
            byte[] data;

            // Generate the hash based on the algorithm selection
            switch(cbAlgorithms.Items[cbAlgorithms.SelectedIndex].ToString()) {
                case "MD5":
                       MD5 md5Hash = MD5.Create();
                       data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
                       for (int i = 0; i < data.Length; i++)
                       {
                           sBuilder.Append(data[i].ToString("x2"));
                       }
                    break;

               case "SHA1":
                      SHA1 sha = new SHA1CryptoServiceProvider();
                      data = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
                      for (int i = 0; i < data.Length; i++)
                      {
                          sBuilder.Append(data[i].ToString("x2"));
                      }
                    break;

               case "SHA256":
                      SHA256 sha256 = new SHA256CryptoServiceProvider();
                      data = sha256.ComputeHash(Encoding.UTF8.GetBytes(input));
                      for (int i = 0; i < data.Length; i++)
                      {
                          sBuilder.Append(data[i].ToString("x2"));
                      }
                    break;

                case "SHA384":
                      SHA384 sha384 = new SHA384CryptoServiceProvider();
                      data = sha384.ComputeHash(Encoding.UTF8.GetBytes(input));
                      for (int i = 0; i < data.Length; i++)
                      {
                          sBuilder.Append(data[i].ToString("x2"));
                      }
                    break;

                case "SHA512":
                      SHA512 sha512 = new SHA512CryptoServiceProvider();
                      data = sha512.ComputeHash(Encoding.UTF8.GetBytes(input));
                      for (int i = 0; i < data.Length; i++)
                      {
                          sBuilder.Append(data[i].ToString("x2"));
                      }
                    break;

                case "Base 64":
                    data = Encoding.UTF8.GetBytes(this.tbPlainText.Text);
                    sBuilder.Append(Convert.ToBase64String (data));
                    break;

                default:
                    MessageBox.Show("Error: Unknown Algorithm Selected, cannot compute!");
                    break;
            }

            // Display the hash output
            this.tbHashOutput.Text = sBuilder.ToString();
        }
Ejemplo n.º 18
0
 private void Checksum(string algorithm, byte[] data)
 {
   if (algorithm == @"CRC32")
   {
     long crc = CRC32CryptoServiceProvider.Compute(data);
     OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", crc));
   }
   else if (algorithm == @"CRC64")
   {
     ulong crc = CRC64CryptoServiceProvider.Compute(data);
     OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", crc));
   }
   else if (algorithm == @"MD5")
   {
     using (MD5CryptoServiceProvider hasher = new MD5CryptoServiceProvider())
     {
       byte[] list = hasher.ComputeHash(data);
       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < list.Length; i++)
       {
         sb.Append(list[i].ToString("x2", CultureInfo.CurrentCulture));
       }
       OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", sb.ToString()));
     }
   }
   else if (algorithm == @"SHA1")
   {
     using (SHA1CryptoServiceProvider hasher = new SHA1CryptoServiceProvider())
     {
       byte[] list = hasher.ComputeHash(data);
       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < list.Length; i++)
       {
         sb.Append(list[i].ToString("x2", CultureInfo.CurrentCulture));
       }
       OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", sb.ToString()));
     }
   }
   else if (algorithm == @"SHA256")
   {
     using (SHA256CryptoServiceProvider hasher = new SHA256CryptoServiceProvider())
     {
       byte[] list = hasher.ComputeHash(data);
       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < list.Length; i++)
       {
         sb.Append(list[i].ToString("x2", CultureInfo.CurrentCulture));
       }
       OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", sb.ToString()));
     }
   }
   else if (algorithm == @"SHA384")
   {
     using (SHA384CryptoServiceProvider hasher = new SHA384CryptoServiceProvider())
     {
       byte[] list = hasher.ComputeHash(data);
       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < list.Length; i++)
       {
         sb.Append(list[i].ToString("x2", CultureInfo.CurrentCulture));
       }
       OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", sb.ToString()));
     }
   }
   else if (algorithm == @"SHA512")
   {
     using (SHA512CryptoServiceProvider hasher = new SHA512CryptoServiceProvider())
     {
       byte[] list = hasher.ComputeHash(data);
       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < list.Length; i++)
       {
         sb.Append(list[i].ToString("x2", CultureInfo.CurrentCulture));
       }
       OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", sb.ToString()));
     }
   }
   else if (algorithm == @"RIPEMD160")
   {
     using (RIPEMD160 hasher = RIPEMD160Managed.Create())
     {
       byte[] list = hasher.ComputeHash(data);
       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < list.Length; i++)
       {
         sb.Append(list[i].ToString("x2", CultureInfo.CurrentCulture));
       }
       OutputText(string.Format(CultureInfo.CurrentCulture, "{0}", sb.ToString()));
     }
   }
 }
Ejemplo n.º 19
0
            public string SHA384Hash(string data) {
                Contract.Requires<ArgumentNullException>(data != null);

                using (SHA384 sha = new SHA384CryptoServiceProvider())
                    return GetHash(sha.ComputeHash(Encoding.ASCII.GetBytes(data)));
            }