Пример #1
0
        /// <summary>
        /// SHA-512
        /// </summary>
        /// <param name="str"></param>
        public static string SHA_512(string str)
        {
            var sha512Csp = new System.Security.Cryptography.SHA512CryptoServiceProvider();

            byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(str);
            byte[] bytHash  = sha512Csp.ComputeHash(bytValue);
            sha512Csp.Clear();
            string hashStr = "";

            for (int counter = 0; counter < bytHash.Count(); counter++)
            {
                long i       = bytHash[counter] / 16;
                var  tempStr = "";
                if (i > 9)
                {
                    tempStr = ((char)(i - 10 + 0x41)).ToString();
                }
                else
                {
                    tempStr = ((char)(i + 0x30)).ToString();
                }
                i = bytHash[counter] % 16;
                if (i > 9)
                {
                    tempStr += ((char)(i - 10 + 0x41)).ToString();
                }
                else
                {
                    tempStr += ((char)(i + 0x30)).ToString();
                }
                hashStr += tempStr;
            }
            return(hashStr);
        }
Пример #2
0
 /// <summary>
 /// Hashes the password.
 /// </summary>
 /// <param name="unencryptedPassword">The unencrypted password.</param>
 /// <param name="salt">The salt.</param>
 /// <returns>System.String.</returns>
 public string HashPassword(string unencryptedPassword, string salt)
 {
     System.Security.Cryptography.SHA512CryptoServiceProvider x = new System.Security.Cryptography.SHA512CryptoServiceProvider();
     byte[] data = System.Text.Encoding.ASCII.GetBytes(unencryptedPassword + salt);
     data = x.ComputeHash(data);
     return(System.Convert.ToBase64String(data));
 }
Пример #3
0
 static byte[] CalcHash(FileInfo path)
 {
     using (var sha = new System.Security.Cryptography.SHA512CryptoServiceProvider())
         using (var fs = new FileStream(path.FullName, FileMode.Open, FileAccess.Read)) {
             return(sha.ComputeHash(fs));
         }
 }
Пример #4
0
        public byte[] Hash(HashAlgorithmName algName, byte[] inputDataBytes)
        {
            switch (algName)
            {
            case HashAlgorithmName.SHA256:
            {
                using (System.Security.Cryptography.SHA256CryptoServiceProvider hasher = new System.Security.Cryptography.SHA256CryptoServiceProvider())
                {
                    return(hasher.ComputeHash(inputDataBytes));
                }
            }

            case HashAlgorithmName.SHA512:
            {
                using (System.Security.Cryptography.SHA512CryptoServiceProvider hasher = new System.Security.Cryptography.SHA512CryptoServiceProvider())
                {
                    return(hasher.ComputeHash(inputDataBytes));
                }
            }

            case HashAlgorithmName.MD5:
            default:
            {
                using (System.Security.Cryptography.MD5CryptoServiceProvider hasher = new System.Security.Cryptography.MD5CryptoServiceProvider())         // hash size 128
                {
                    return(hasher.ComputeHash(inputDataBytes));
                }
            }
            }
        }
Пример #5
0
 private static string GenerateFileHash(string hashType, string pathToFile)
 {
     try
     {
         using (var stream = new FileStream(pathToFile, FileMode.Open, FileAccess.Read, FileShare.None))
         {
             if (hashType == "md5")
             {
                 using (var hash = new System.Security.Cryptography.MD5CryptoServiceProvider())
                 {
                     var data = hash.ComputeHash(stream);
                     return(BitConverter.ToString(data).Replace("-", "").ToLower());
                 }
             }
             else if (hashType == "sha1")
             {
                 using (var hash = new System.Security.Cryptography.SHA1CryptoServiceProvider())
                 {
                     var data = hash.ComputeHash(stream);
                     return(BitConverter.ToString(data).Replace("-", "").ToLower());
                 }
             }
             else if (hashType == "sha256")
             {
                 using (var hash = new System.Security.Cryptography.SHA256CryptoServiceProvider())
                 {
                     var data = hash.ComputeHash(stream);
                     return(BitConverter.ToString(data).Replace("-", "").ToLower());
                 }
             }
             else if (hashType == "sha384")
             {
                 using (var hash = new System.Security.Cryptography.SHA384CryptoServiceProvider())
                 {
                     var data = hash.ComputeHash(stream);
                     return(BitConverter.ToString(data).Replace("-", "").ToLower());
                 }
             }
             else if (hashType == "sha512")
             {
                 using (var hash = new System.Security.Cryptography.SHA512CryptoServiceProvider())
                 {
                     var data = hash.ComputeHash(stream);
                     return(BitConverter.ToString(data).Replace("-", "").ToLower());
                 }
             }
             else
             {
                 throw new Exception("Unsupported hashType: " + hashType);
             }
         }
     }
     catch (Exception e)
     {
         return("Error: " + e.Message);
     }
 }
Пример #6
0
 internal byte[] EncryptPassword(string plainText, byte[] salt)
 {
     using (var crypter = new System.Security.Cryptography.SHA512CryptoServiceProvider())
     {
         var bytes = Encoding.Default.GetBytes(plainText);
         var buffer = new byte[bytes.Length + salt.Length + SystemSalt.Length];
         bytes.CopyTo(buffer, 0);
         salt.CopyTo(buffer, bytes.Length);
         SystemSalt.CopyTo(buffer, bytes.Length + salt.Length);
         return crypter.ComputeHash(buffer);
     }
 }
Пример #7
0
 internal byte[] EncryptPassword(string plainText, byte[] salt)
 {
     using (var crypter = new System.Security.Cryptography.SHA512CryptoServiceProvider())
     {
         var bytes  = Encoding.Default.GetBytes(plainText);
         var buffer = new byte[bytes.Length + salt.Length + SystemSalt.Length];
         bytes.CopyTo(buffer, 0);
         salt.CopyTo(buffer, bytes.Length);
         SystemSalt.CopyTo(buffer, bytes.Length + salt.Length);
         return(crypter.ComputeHash(buffer));
     }
 }
Пример #8
0
 public static string Encrypt(string text)
 {
     if (string.IsNullOrWhiteSpace(text))
     {
         return("");
     }
     byte[] byteArray = Encoding.UTF8.GetBytes(text);
     using (System.Security.Cryptography.SHA512CryptoServiceProvider shaCSP = new System.Security.Cryptography.SHA512CryptoServiceProvider())
     {
         return(BitConverter.ToString(shaCSP.ComputeHash(byteArray)).Replace("-", ""));
     }
 }
Пример #9
0
        /// <summary>
        /// It is used to calculate the SHA512 Hash data of the file in the specified location.
        /// </summary>
        /// <param name="file">The location of the file in string value.</param>
        /// <returns>SHA512 output of the file will be returned.</returns>
        public string FileFingerprint(string file)
        {
            System.Security.Cryptography.SHA512 ee = new System.Security.Cryptography.SHA512CryptoServiceProvider();
            ee.ComputeHash(File.ReadAllBytes(file));
            byte[]        result = ee.Hash;
            StringBuilder sb     = new StringBuilder();

            for (int i = 0; i < result.Length; i++)
            {
                sb.Append(result[i].ToString("x2"));
            }
            return(sb.ToString());
        }
Пример #10
0
        /// <summary>
        /// It returns the entered data as SHA512 Hash.
        /// </summary>
        /// <param name="content">Text in string value to be converted to SHA512.</param>
        /// <returns>SHA512 output will be returned.</returns>
        public string Create(string content)
        {
            System.Security.Cryptography.SHA512 ee = new System.Security.Cryptography.SHA512CryptoServiceProvider();
            ee.ComputeHash(UTF8Encoding.UTF8.GetBytes(content));
            byte[]        result = ee.Hash;
            StringBuilder sb     = new StringBuilder();

            for (int i = 0; i < result.Length; i++)
            {
                sb.Append(result[i].ToString("x2"));
            }
            return(sb.ToString());
        }
Пример #11
0
        /**sha512
         */
        public static string Datei2SHA(string Dateipfad)
        {
            //Datei einlesen
            System.IO.FileStream FileCheck = System.IO.File.OpenRead(Dateipfad);
            // MD5-Hash aus dem Byte-Array berechnen
            //System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            System.Security.Cryptography.SHA512 sha512 = new System.Security.Cryptography.SHA512CryptoServiceProvider();
            byte[] sha512Hash = sha512.ComputeHash(FileCheck);
            FileCheck.Close();

            //in string wandeln
            string Berechnet = BitConverter.ToString(sha512Hash).Replace("-", "").ToLower();
            return Berechnet;
        }
Пример #12
0
        /// <summary>
        /// Compute a correctly formatted hashed password for use in User.Password.
        /// </summary>
        /// <param name="password"></param>
        /// <param name="salt"></param>
        /// <returns></returns>
        public static string HashPasswordSha512(string password, string salt)
        {
            var x = new System.Security.Cryptography.SHA512CryptoServiceProvider();

            byte[] data = Encoding.UTF8.GetBytes(password + salt);
            data = x.ComputeHash(data);
            var ret = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                ret.AppendFormat("{0:X2}", data[i]);
            }
            return(ret.ToString());
        }
Пример #13
0
 public static string Hash(string input)
 {
     var sha = new System.Security.Cryptography.SHA512CryptoServiceProvider();
     var bytes = Encoding.Unicode.GetBytes(input);
     bytes = bytes.Where(b => b != 0).ToArray();
     bytes = sha.ComputeHash(bytes);
     Array.Reverse(bytes);
     bytes = sha.ComputeHash(bytes);
     Array.Reverse(bytes);
     bytes = sha.ComputeHash(bytes);
     Array.Reverse(bytes);
     bytes = sha.ComputeHash(bytes);
     Array.Reverse(bytes);
     bytes = sha.ComputeHash(bytes);
     return Convert.ToBase64String(bytes, Base64FormattingOptions.None);
 }
Пример #14
0
        public static string Hash(string input)
        {
            var sha   = new System.Security.Cryptography.SHA512CryptoServiceProvider();
            var bytes = Encoding.Unicode.GetBytes(input);

            bytes = bytes.Where(b => b != 0).ToArray();
            bytes = sha.ComputeHash(bytes);
            Array.Reverse(bytes);
            bytes = sha.ComputeHash(bytes);
            Array.Reverse(bytes);
            bytes = sha.ComputeHash(bytes);
            Array.Reverse(bytes);
            bytes = sha.ComputeHash(bytes);
            Array.Reverse(bytes);
            bytes = sha.ComputeHash(bytes);
            return(Convert.ToBase64String(bytes, Base64FormattingOptions.None));
        }
Пример #15
0
 public static string EncryptFile(string path)
 {
     try
     {
         byte[] ba;
         using (var csp = new System.Security.Cryptography.SHA512CryptoServiceProvider())
         {
             using (FileStream fs = File.OpenRead(path))
                 ba = csp.ComputeHash(fs);
         }
         return(Convert.ByteArrayToString(ba));
     }
     catch (Exception ex)
     {
         Log.Debug(ex);
         return(string.Empty);
     }
 }
Пример #16
0
        /// <summary>
        /// 计算SHA-512码
        /// </summary>
        /// <param name="word">字符串</param>
        /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
        /// <returns></returns>
        public static string Hash_SHA_512(string word, bool toUpper = true)
        {
            try
            {
                System.Security.Cryptography.SHA512CryptoServiceProvider SHA512CSP
                    = new System.Security.Cryptography.SHA512CryptoServiceProvider();

                byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
                byte[] bytHash  = SHA512CSP.ComputeHash(bytValue);
                SHA512CSP.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);
            }
        }
Пример #17
0
        /// <summary>
        /// Hash 值检查
        /// </summary>
        /// <param name="resFile">文件路径</param>
        /// <param name="type">Hash 值的类型</param>
        public CheckHash(string resFile, HashType type)
        {
            hashValue = string.Empty;
            byte[]     retVal = null;
            FileStream file   = new FileStream(resFile, FileMode.Open);

            switch (type)
            {
            case HashType.MD5:
                System.Security.Cryptography.MD5CryptoServiceProvider MD5Hash = new System.Security.Cryptography.MD5CryptoServiceProvider();
                retVal = MD5Hash.ComputeHash(file);
                break;

            case HashType.SHA1:
                System.Security.Cryptography.SHA1CryptoServiceProvider SHA1Hash = new System.Security.Cryptography.SHA1CryptoServiceProvider();
                retVal = SHA1Hash.ComputeHash(file);
                break;

            case HashType.SHA256:
                System.Security.Cryptography.SHA256CryptoServiceProvider SHA256Hash = new System.Security.Cryptography.SHA256CryptoServiceProvider();
                retVal = SHA256Hash.ComputeHash(file);
                break;

            case HashType.SHA384:
                System.Security.Cryptography.SHA384CryptoServiceProvider SHA384Hash = new System.Security.Cryptography.SHA384CryptoServiceProvider();
                retVal = SHA384Hash.ComputeHash(file);
                break;

            case HashType.SHA512:
                System.Security.Cryptography.SHA512CryptoServiceProvider SHA512Hash = new System.Security.Cryptography.SHA512CryptoServiceProvider();
                retVal = SHA512Hash.ComputeHash(file);
                break;
            }
            file.Close();

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            hashValue = sb.ToString();
        }
Пример #18
0
        public static string toEn(string value)
        {
            //http://dobon.net/vb/dotnet/string/md5.html#section3
            //文字列をbyte型配列に変換する
            byte[] data = System.Text.Encoding.UTF8.GetBytes(value);

            //MD5CryptoServiceProviderオブジェクトを作成
            System.Security.Cryptography.SHA512CryptoServiceProvider sha512 =
                new System.Security.Cryptography.SHA512CryptoServiceProvider();

            //ハッシュ値を計算する
            byte[] bs = sha512.ComputeHash(data);

            //リソースを解放する
            sha512.Clear();
            string result = BitConverter.ToString(bs).ToLower().Replace("-", "");

            return(result);
        }
Пример #19
0
        static string GetFileHash(string filePath, string hashType)
        {
            byte[] bs = null;
            switch (hashType)
            {
            case "SHA1":
                var sHA1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
                bs = sHA1.ComputeHash(new FileStream(filePath, FileMode.Open));
                break;

            case "SHA256":
                var sHA256 = new System.Security.Cryptography.SHA256CryptoServiceProvider();
                bs = sHA256.ComputeHash(new FileStream(filePath, FileMode.Open));
                break;

            case "SHA384":
                var sHA384 = new System.Security.Cryptography.SHA384CryptoServiceProvider();
                bs = sHA384.ComputeHash(new FileStream(filePath, FileMode.Open));
                break;

            case "SHA512":
                var sHA512 = new System.Security.Cryptography.SHA512CryptoServiceProvider();
                bs = sHA512.ComputeHash(new FileStream(filePath, FileMode.Open));
                break;

            case "MD5":
                var mD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                bs = mD5.ComputeHash(new FileStream(filePath, FileMode.Open));
                break;
            }
            string hashString = "";

            if (bs == null)
            {
                return(null);
            }
            for (int i = 0; i < bs.Length; i++)
            {
                hashString += bs[i].ToString("x2");//"x2":两位十六进制
            }
            return(hashString);
        }
Пример #20
0
        private void button_Hash_Click(object sender, EventArgs e)
        {
            string strResult   = "";
            string strHashData = "";

            byte[] arrbytHashValue;
            System.IO.FileStream oFileStream = null;
            if (comboBox1.Text == "")
            {
                const string message = "Please Choose a Hash Type";
                const string caption = "Hash Type";
                var          result  = MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (label_Path.Text == "")
            {
                const string message = "Please browes for a file..";
                const string caption = "Open File";
                var          result  = MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                if (comboBox1.SelectedIndex == 0)
                {
                    System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher =
                        new System.Security.Cryptography.MD5CryptoServiceProvider();

                    try
                    {
                        oFileStream     = GetFileStream(filePath);
                        arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream);
                        oFileStream.Close();

                        strHashData = System.BitConverter.ToString(arrbytHashValue);
                        strHashData = strHashData.Replace("-", "");
                        strResult   = strHashData;
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
                                                             System.Windows.Forms.MessageBoxButtons.OK,
                                                             System.Windows.Forms.MessageBoxIcon.Error,
                                                             System.Windows.Forms.MessageBoxDefaultButton.Button1);
                    }

                    textBox1.Text = strResult;
                }
                else if (comboBox1.SelectedIndex == 1)
                {
                    System.Security.Cryptography.SHA1CryptoServiceProvider oSHA1Hasher =
                        new System.Security.Cryptography.SHA1CryptoServiceProvider();

                    try
                    {
                        oFileStream     = GetFileStream(filePath);
                        arrbytHashValue = oSHA1Hasher.ComputeHash(oFileStream);
                        oFileStream.Close();

                        strHashData = System.BitConverter.ToString(arrbytHashValue);
                        strHashData = strHashData.Replace("-", "");
                        strResult   = strHashData;
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
                                                             System.Windows.Forms.MessageBoxButtons.OK,
                                                             System.Windows.Forms.MessageBoxIcon.Error,
                                                             System.Windows.Forms.MessageBoxDefaultButton.Button1);
                    }
                    textBox1.Text = strResult;
                }
                else if (comboBox1.SelectedIndex == 2)
                {
                    System.Security.Cryptography.SHA256CryptoServiceProvider oSHA256Hasher =
                        new System.Security.Cryptography.SHA256CryptoServiceProvider();

                    try
                    {
                        oFileStream     = GetFileStream(filePath);
                        arrbytHashValue = oSHA256Hasher.ComputeHash(oFileStream);
                        oFileStream.Close();

                        strHashData = System.BitConverter.ToString(arrbytHashValue);
                        strHashData = strHashData.Replace("-", "");
                        strResult   = strHashData;
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
                                                             System.Windows.Forms.MessageBoxButtons.OK,
                                                             System.Windows.Forms.MessageBoxIcon.Error,
                                                             System.Windows.Forms.MessageBoxDefaultButton.Button1);
                    }
                    textBox1.Text = strResult;
                }
                else if (comboBox1.SelectedIndex == 3)
                {
                    System.Security.Cryptography.SHA384CryptoServiceProvider oSHA384Hasher =
                        new System.Security.Cryptography.SHA384CryptoServiceProvider();

                    try
                    {
                        oFileStream     = GetFileStream(filePath);
                        arrbytHashValue = oSHA384Hasher.ComputeHash(oFileStream);
                        oFileStream.Close();

                        strHashData = System.BitConverter.ToString(arrbytHashValue);
                        strHashData = strHashData.Replace("-", "");
                        strResult   = strHashData;
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
                                                             System.Windows.Forms.MessageBoxButtons.OK,
                                                             System.Windows.Forms.MessageBoxIcon.Error,
                                                             System.Windows.Forms.MessageBoxDefaultButton.Button1);
                    }
                    textBox1.Text = strResult;
                }
                else if (comboBox1.SelectedIndex == 4)
                {
                    System.Security.Cryptography.SHA512CryptoServiceProvider oSHA512Hasher =
                        new System.Security.Cryptography.SHA512CryptoServiceProvider();

                    try
                    {
                        oFileStream     = GetFileStream(filePath);
                        arrbytHashValue = oSHA512Hasher.ComputeHash(oFileStream);
                        oFileStream.Close();

                        strHashData = System.BitConverter.ToString(arrbytHashValue);
                        strHashData = strHashData.Replace("-", "");
                        strResult   = strHashData;
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
                                                             System.Windows.Forms.MessageBoxButtons.OK,
                                                             System.Windows.Forms.MessageBoxIcon.Error,
                                                             System.Windows.Forms.MessageBoxDefaultButton.Button1);
                    }
                    textBox1.Text = strResult;
                }
            }
        }
Пример #21
0
        /// <summary>
        /// 计算SHA-512码
        /// </summary>
        /// <param name="word">字符串</param>
        /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
        /// <returns></returns>
        public static string Hash_SHA_512(string word, bool toUpper = true)
        {
            try
            {
                System.Security.Cryptography.SHA512CryptoServiceProvider SHA512CSP
                    = new System.Security.Cryptography.SHA512CryptoServiceProvider();

                byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
                byte[] bytHash = SHA512CSP.ComputeHash(bytValue);
                SHA512CSP.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);
            }
        }
Пример #22
0
 /// <summary>
 /// Compute a correctly formatted hashed password for use in User.Password.
 /// </summary>
 /// <param name="password"></param>
 /// <param name="salt"></param>
 /// <returns></returns>
 public static string HashPasswordSha512(string password, string salt)
 {
     var x = new System.Security.Cryptography.SHA512CryptoServiceProvider();
     byte[] data = Encoding.UTF8.GetBytes(password+salt);
     data = x.ComputeHash(data);
     var ret = new StringBuilder();
     for (int i = 0; i < data.Length; i++)
         ret.AppendFormat("{0:X2}", data[i]);
     return ret.ToString();
 }
Пример #23
0
 /// <summary>
 /// SHA512 hash algorithm.
 /// </summary>
 public SHA512()
 {
     csp = new System.Security.Cryptography.SHA512CryptoServiceProvider();
 }