protected final Dispose ( bool disposing ) : void | ||
disposing | bool | |
Результат | void |
/// <summary> /// Retorna el hash en format string de l'string paràmetre /// </summary> /// <param name="textIn"> és el text del qual volem calcular el hash</param> /// <returns> retorna un string amb el hash resultat o null si hi ha error</returns> static string CalculaHash(string textIn) { try { // Convertim l'string a un array de bytes byte[] bytesIn = UTF8Encoding.UTF8.GetBytes(textIn); // Instanciar classe per fer hash SHA512Managed SHA512 = new SHA512Managed(); // Calcular hash byte[] hashResult = SHA512.ComputeHash(bytesIn); // Si volem mostrar el hash per pantalla o guardar-lo en un arxiu de text // cal convertir-lo a un string String textOut = BitConverter.ToString(hashResult, 0); // Eliminem la classe instanciada SHA512.Dispose(); return textOut; } catch (Exception) { Console.WriteLine("Error calculant el hash"); Console.ReadKey(true); return null; } }
/// <summary> /// This will return the 512 bit SHA-2 hash. /// </summary> /// <param name="myString"></param> /// <returns></returns> public static byte[] GetSHA2_512(this string myString) { var data = myString.ToBytes(); SHA512 shaM = new SHA512Managed(); var result = shaM.ComputeHash(data); shaM.Dispose(); return result; }
} // END public String GetHash(String ValueToHash, String ValueLocale, String SaltToUse) /// <summary> /// This function takes a value you want hashed, uses SHA-512 for strength, and includes a locale extender such as a computer name, /// and a salt value that has no intrinsic meaning. The value returned is the hash string in Base 64. /// </summary> /// <param name="ValueToHash">This is the value, such as a password. It will usually be the same over a number of instances on multiple machines.</param> /// <param name="ValueLocale">This is a value specific to the machine, such as machine name. It should be deterministic on every use.</param> /// <param name="SaltToUse">This is a value that extends the hash for security.</param> /// <returns>The value returned is the hash string in Base 64</returns> public String GetHash(String ValueToHash, String ValueLocale, String SaltToUse) { DateTime dtmMethodStart = DateTime.Now; String ReturnValue = ""; SHA512Managed Hasher = null; try { Hasher = new System.Security.Cryptography.SHA512Managed(); Byte[] Text2Hash = System.Text.Encoding.UTF8.GetBytes(String.Concat(ValueToHash.Trim(), ValueLocale.ToUpper(), SaltToUse.Trim())); Byte[] PlateOfHash = Hasher.ComputeHash(Text2Hash); ReturnValue = Convert.ToBase64String(PlateOfHash); } catch (Exception exUnhandled) { exUnhandled.Data.Add("ValueToHash", ValueToHash ?? ""); exUnhandled.Data.Add("ValueLocale", ValueLocale ?? ""); exUnhandled.Data.Add("SaltToUse", SaltToUse ?? ""); throw; } // END catch finally { if (Hasher != null) { Hasher.Clear(); Hasher.Dispose(); Hasher = null; } } return(ReturnValue); } // END public String GetHash(String ValueToHash, String ValueLocale, String SaltToUse)
public static string CreatePasswordHash(string _password, string _salt) { string saltAndPwd = String.Concat(_password, _salt); SHA512 sha512 = new System.Security.Cryptography.SHA512Managed(); byte[] sha512Bytes = System.Text.Encoding.Default.GetBytes(saltAndPwd); byte[] cryString = sha512.ComputeHash(sha512Bytes); string hashedPwd = string.Empty; for (int i = 0; i < cryString.Length; i++) { hashedPwd += cryString[i].ToString("X"); } sha512.Clear(); sha512.Dispose(); return hashedPwd; }
public static string CreatePasswordHash(string _password, string _salt) { string saltAndPwd = String.Concat(_password, _salt); SHA512 sha512 = new System.Security.Cryptography.SHA512Managed(); byte[] sha512Bytes = System.Text.Encoding.Default.GetBytes(saltAndPwd); byte[] cryString = sha512.ComputeHash(sha512Bytes); string hashedPwd = string.Empty; for (int i = 0; i < cryString.Length; i++) { hashedPwd += cryString[i].ToString("X"); } sha512.Clear(); sha512.Dispose(); return(hashedPwd); }
static void Main(string[] args) { String textIn = null; Console.Write("Entra text: "); while (textIn==null) textIn = Console.ReadLine(); // Convertim l'string a un array de bytes byte[] bytesIn = UTF8Encoding.UTF8.GetBytes(textIn); // Instanciar classe per fer hash SHA512Managed SHA512 = new SHA512Managed(); // Calcular hash byte[] hashResult = SHA512.ComputeHash(bytesIn); // Si volem mostrar el hash per pantalla o guardar-lo en un arxiu de text // cal convertir-lo a un string String textOut = BitConverter.ToString(hashResult, 0); Console.WriteLine("Hash {0}", textOut); Console.ReadKey(); // Eliminem la classe instanciada SHA512.Dispose(); }
} // END public CryptoStream GetEncryptedStreamObject(Stream objInnerStream) /// <summary> /// This function takes a value you want hashed and hashes it uses SHA-512 for strength. /// The value returned is the hash string in Base 64. /// </summary> /// <param name="strStringToHash">This is the value, such as a password. It will usually be the same over a number of instances on multiple machines.</param> /// <returns>The value returned is the hash string in Base 64</returns> public String GetHash(string strStringToHash) { String ReturnValue = ""; SHA512Managed Hasher = null; try { Hasher = new System.Security.Cryptography.SHA512Managed(); Byte[] Text2Hash = System.Text.Encoding.UTF8.GetBytes(strStringToHash); Byte[] PlateOfHash = Hasher.ComputeHash(Text2Hash); ReturnValue = Convert.ToBase64String(PlateOfHash); } catch (Exception exUnhandled) { exUnhandled.Data.Add("strStringToHash", strStringToHash ?? ""); throw; } // END catch finally { if (Hasher != null) { Hasher.Clear(); Hasher.Dispose(); Hasher = null; } } return(ReturnValue); }
public static string ComputeHash(string plaintext, Supported_HA hash, byte[] salt) { int minSaltLength = 4; int maxSaltLenght = 6; byte[] SaltBytes = null; if(salt!=null) { SaltBytes = salt; } else { Random r = new Random(); int SaltLenght = r.Next(minSaltLength, maxSaltLenght); SaltBytes = new byte[SaltLenght]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetNonZeroBytes(SaltBytes); rng.Dispose(); } byte[] plaintData = ASCIIEncoding.UTF8.GetBytes(plaintext); byte[] plainDataAndSalt = new byte[plaintData.Length + SaltBytes.Length]; for(int x=0; x<plaintData.Length;x++) { plainDataAndSalt[x] = plaintData[x]; } for (int n = 0; n < SaltBytes.Length; n++) plainDataAndSalt[plaintData.Length + n] = SaltBytes[n]; byte[] hashValue = null; switch(hash) { case Supported_HA.SHA256: SHA256Managed sha= new SHA256Managed(); hashValue= sha.ComputeHash(plainDataAndSalt); sha.Dispose(); break; case Supported_HA.SHA384: SHA384Managed sha1 = new SHA384Managed(); hashValue = sha1.ComputeHash(plainDataAndSalt); sha1.Dispose(); break; case Supported_HA.SHA512: SHA512Managed sha2 = new SHA512Managed(); hashValue = sha2.ComputeHash(plainDataAndSalt); sha2.Dispose(); break; } byte[] resuflt = new byte[hashValue.Length + SaltBytes.Length]; for (int x = 0; x < hashValue.Length; x++) resuflt[x] = hashValue[x]; for (int n = 0; n < SaltBytes.Length; n++) resuflt[hashValue.Length + n] = SaltBytes[n]; return Convert.ToBase64String(resuflt); }