static void Main(string[] args) { String textIn = null; Console.Write("Entra text: "); while (textIn == null) { textIn = Console.ReadLine(); } // Convertimos el string a una array de bytes byte[] bytesIn = UTF8Encoding.UTF8.GetBytes(textIn); // Instanciar classe para hacer el hash SHA512Managed SHA512 = new SHA512Managed(); // Calcular hash byte[] hashResult = SHA512.ComputeHash(bytesIn); // Si queremos mostrar el hash por pantalla o guardarlo en un archivo de texto hace falta convertirlo a un string String textOut = BitConverter.ToString(hashResult, 0); Console.WriteLine("Hash {0}", textOut); Console.ReadKey(); // Eliminamos la clase creada para liberar memoria SHA512.Dispose(); }
public void ZerosToSave() { SaveFile file = new SaveFile("test", new byte[SaveFile.ParametersLengthInBytes], new byte[SaveFile.MapLengthInBytes]); SHA512 sha512 = new SHA512Managed(); var exceptedHash = sha512.ComputeHash(new byte[SaveFile.InfoLengthInBytes]); sha512.Dispose(); foreach (var param in file.ParametersInfo) { Assert.AreEqual(0, param); } foreach (var tile in file.MapInfo) { Assert.AreEqual(0, tile); } foreach (var b in file.FullInfo) { Assert.AreEqual(0, b); } for (int i = 0; i < 64; ++i) { Assert.AreEqual(exceptedHash[i], file.Hash[i]); } File.Delete(file.Path); }
public static string ComputeHash(string text, string salt, HashAlgo hashType) { byte[] bytes = ASCIIEncoding.UTF8.GetBytes(text + salt); byte[] hash = null; switch (hashType) { case HashAlgo.SHA256: SHA256Managed sha256 = new SHA256Managed(); hash = sha256.ComputeHash(bytes); sha256.Dispose(); break; case HashAlgo.SHA384: SHA384Managed sha384 = new SHA384Managed(); hash = sha384.ComputeHash(bytes); sha384.Dispose(); break; case HashAlgo.SHA512: SHA512Managed sha512 = new SHA512Managed(); hash = sha512.ComputeHash(bytes); sha512.Dispose(); break; } return(Convert.ToBase64String(hash)); }
static void Main(String[] args) { //Creem una string de text, on guardarem l'arxiu de text que volem utilitzar String Text = null; String Text = System.IO.File.ReadAllText(@"C:\Users\Oscar\Documents\GitHub\Activitat-Hash\TextProvaHash"); //Convertim l'String del text en un array de bytes byte[] ArrayBytes = UTF8Encoding.UTF8.GetBytes(Text); //Instanciem classe que ens permet calcular el hash SHA512Managed SHA512 = new SHA512Managed(); //Calculem el hash de l'array generat anteriorment byte[] ArrayHash = SHA512.ComputeHash(ArrayBytes); //Tornem a transofrmar l'array a String per tal de poder guardar-lo en un arxiu String HashResultat = BitConverter.ToString(ArrayHash, 0); //Generem l'arxiu de text que contindrà el hash resultant System.IO.File.WriteAllText(@"C:\Users\Oscar\Documents\GitHub\Activitat-Hash\HashResultat.txt", text); //En cas de voler mostrar-lo per pantalla //Console.WriteLine("Hash {0}", HashResultat); //Console.ReadKey(); //Eliminem la instancia SHA512.Dispose(); }
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(); }
//Calculem el hash. 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); String textOut = Convert.ToBase64String(hashResult); // Eliminem la classe instanciada. SHA512.Dispose(); return(textOut); } catch (Exception) { Console.WriteLine("Error calculant el hash"); Console.ReadKey(true); return(null); } }
private string returnSHA512String(string strTexto) { SHA512 sha = new SHA512Managed(); byte[] bEncoding; byte[] bHash; string strHashCode = ""; try { bEncoding = Encoding.UTF8.GetBytes(String.Concat("K0n$0rc10", strTexto, "onl1n3")); bHash = sha.ComputeHash(bEncoding); foreach (byte b in bHash) { strHashCode += String.Format("{0:x2}", b); } } catch (Exception ex) { throw new Exception(ex.Message); } sha.Dispose(); return(strHashCode); }
private static string SHA512Text(Encoding encoding, string content) { if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException("content is null"); } string strRet = string.Empty; SHA512 sha = null; try { sha = new SHA512Managed(); byte[] buffContent = encoding.GetBytes(content); byte[] buff = sha.ComputeHash(buffContent); strRet = FormatByteArray(buff); } catch { throw; } finally { if (null != sha) { sha.Dispose(); sha = null; } } return(strRet); }
public static string SHA512(Stream stream) { if (null == stream) { throw new ArgumentNullException("stream is null"); } string strRet = string.Empty; SHA512 sha = null; try { sha = new SHA512Managed(); stream.Seek(0, SeekOrigin.Begin); byte[] buff = sha.ComputeHash(stream); strRet = FormatByteArray(buff); } catch { throw; } finally { if (null != sha) { sha.Dispose(); sha = null; } } return(strRet); }
public static string GetSha512String(byte[] buffer) { SHA512 sha512 = new SHA512Managed(); byte[] retVal = sha512.ComputeHash(buffer); //计算指定Stream 对象的哈希值 sha512.Dispose(); return(BitConverter.ToString(retVal).Replace("-", "")); }
private static byte[] GetComputedHash(string password, byte[] salt) { byte[] saltedPass = Encoding.UTF8.GetBytes(password).Concat(salt).ToArray(); HashAlgorithm hashProvider = new SHA512Managed(); byte[] hashedPass = hashProvider.ComputeHash(saltedPass); hashProvider.Dispose(); return(hashedPass); }
/// <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); }
/// <summary> /// Encodes a string with the SHA-512 hash function /// </summary> public static string ToSHA512(this string input) { using (var cryptoProvider = new SHA512Managed()) { var hash = cryptoProvider.ComputeHash(Encoding.UTF8.GetBytes(input)); cryptoProvider.Dispose(); return(string.Join("", hash.Select(b => b.ToString("x2")).ToArray())); } }
protected virtual Byte[] hashProcess(Byte[] dataIn) { var hp = new SHA512Managed(); var toret = hp.ComputeHash(dataIn); hp.Dispose(); hp = null; return(toret); }
void SHA512() { SHA512 shaM = new SHA512Managed(); result = shaM.ComputeHash(data); ConsoleResult("SHA512"); shaM.Dispose(); }
byte[] hashFileData(string fileToHash) { byte[] bytesIn = Encoding.UTF8.GetBytes(getDataFromFilePath(fileToHash)); SHA512Managed SHA512 = new SHA512Managed(); byte[] hashResult = SHA512.ComputeHash(bytesIn); SHA512.Dispose(); return(hashResult); }
public override void updateHash() { var toret = new Byte[64]; using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite, 10485760)) { var hp = new SHA512Managed(); toret = hp.ComputeHash(fs); hp.Dispose(); hp = null; } hash = BitConverter.ToString(toret, 0, toret.Length).Replace("-", String.Empty); }
public static byte[] CreateSHA512(byte[] data) { byte[] result; SHA512 shaM = new SHA512Managed(); result = shaM.ComputeHash(data); shaM.Dispose(); return(result); }
/// <summary> /// /// </summary> /// <param name="buffer"></param> /// <returns></returns> public static string GetSha512String(byte[] buffer) { if (buffer == null) { throw new ArgumentNullException("buffer"); } SHA512 sha512 = new SHA512Managed(); byte[] retVal = sha512.ComputeHash(buffer); //计算指定Stream 对象的哈希值 sha512.Dispose(); return(BitConverter.ToString(retVal).Replace("-", "")); }
/// <summary> /// Overridden to dispose the internal <see cref="SHA512Managed"/> instance. /// </summary> /// <param name="disposing">True on actual disposing, false when called by the GC.</param> protected override void Dispose(bool disposing) { if (disposing && _sha512 != null) { _sha512.Dispose(); _sha512 = null; if (!_leaveOpen) { _reader?.Dispose(); _writer?.Dispose(); } } }
public string ComputeHash(string plainText, SupportedHash hash, byte[] salt) { byte[] plainData = Encoding.UTF8.GetBytes(plainText); byte[] plainDataWithSalt = new byte[plainData.Length + salt.Length]; for (int x = 0; x < plainData.Length; x++) { plainDataWithSalt[x] = plainData[x]; } for (int n = 0; n < salt.Length; n++) { plainDataWithSalt[plainData.Length + n] = salt[n]; } byte[] hashValue = null; switch (hash) { case SupportedHash.SHA256: SHA256Managed sha = new SHA256Managed(); hashValue = sha.ComputeHash(plainDataWithSalt); sha.Dispose(); break; case SupportedHash.SHA384: SHA384Managed sha1 = new SHA384Managed(); hashValue = sha1.ComputeHash(plainDataWithSalt); sha1.Dispose(); break; case SupportedHash.SHA512: SHA512Managed sha2 = new SHA512Managed(); hashValue = sha2.ComputeHash(plainDataWithSalt); sha2.Dispose(); break; } byte[] result = new byte[hashValue.Length + salt.Length]; for (int x = 0; x < hashValue.Length; x++) { result[x] = hashValue[x]; } for (int n = 0; n < salt.Length; n++) { result[hashValue.Length + n] = salt[n]; } return(Convert.ToBase64String(result)); }
/// <summary> /// /// </summary> /// <param name="buffer"></param> /// <returns></returns> public static byte[] GetSha512Bytes(byte[] buffer) { if (buffer == null) { throw new ArgumentNullException("buffer"); } SHA512 sha512 = new SHA512Managed(); byte[] retVal = sha512.ComputeHash(buffer); //计算指定Stream 对象的哈希值 sha512.Dispose(); return(retVal); }
public static string GetSHA512Hash(string input) { SHA512 shaM = new SHA512Managed(); byte[] data = shaM.ComputeHash(Encoding.UTF8.GetBytes(input)); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } shaM.Dispose(); return(sBuilder.ToString()); }
/// <summary> /// SHA512加密 哈希加密一个字符串 生成128位字符串 /// </summary> /// <param name="Security"></param> /// <returns></returns> public static string SHA512String(string encodingString) { byte[] msg = Encoding.UTF8.GetBytes(encodingString); SHA512Managed Arithmetic = new SHA512Managed(); //.Net有SHA-256、SHA-384,和SHA-512并 都称为SHA-2 byte[] Value = Arithmetic.ComputeHash(msg); StringBuilder sb = new StringBuilder(); foreach (byte o in Value) { sb.Append(o.ToString("X2")); } Arithmetic.Dispose(); return(sb.ToString()); }
public static string GetSHA512Hash(string input) { SHA512 shaM = new SHA512Managed(); byte[] data = shaM.ComputeHash(Encoding.UTF8.GetBytes(input)); StringBuilder sBuilder = new StringBuilder(); foreach (byte t in data) { sBuilder.Append(t.ToString("x2")); } shaM.Dispose(); return(sBuilder.ToString()); }
public void CalculateHashes(List <string> filesList) { SHA512 hash = new SHA512Managed(); foreach (var f in filesList) { using (FileStream stream = new FileStream(f, FileMode.Open, FileAccess.Read)) HashList.Add(new Tuple <string, byte[]>(f, hash.ComputeHash(stream))); if (!hash.CanReuseTransform) { hash.Dispose(); hash = new SHA512Managed(); } } }
/// <summary> /// Obtient le hachage d'une donnée. /// </summary> /// <param name="_data">Données à hacher</param> /// <remarks> /// Hache la donnée concaténée avec le salage. /// </remarks> /// <returns>Byte : Réussite -> Donnée hachée. Echec -> Valeur null.</returns> /// <exception cref="SHA512"> /// Exception levée par l'objet SHA512. /// </exception> public static byte[] GetHash(byte[] _data) { byte[] data = _data; // Vérifie si la donnée est null, inférieure ou égale à 0 octets. if (IsDataLength_Error(data)) { return(null); } try { byte[] saltedData = new byte[_SALT_BYTE_SIZE + data.Length]; byte[] hashedData = null; // Récupére le salage. byte[] hashSalt = Convert.FromBase64String(Data_Hash.Default.Salt); bool isSaltNull = (Convert.ToBase64String(hashSalt) == string.Empty); // Vérifie le salage. if (isSaltNull) { hashSalt = GetRandomSalt(); } // Concatène la donnée avec le salage. System.Buffer.BlockCopy(hashSalt, 0, saltedData, 0, _SALT_BYTE_SIZE); System.Buffer.BlockCopy(data, 0, saltedData, _SALT_BYTE_SIZE, data.Length); using (SHA512 sha512Hash = new SHA512Managed()) { // Hache le résultat de la concaténation. hashedData = sha512Hash.ComputeHash(saltedData); sha512Hash.Clear(); sha512Hash.Dispose(); } return(hashedData); } catch { System.Windows.Forms.MessageBox.Show(Data_Hash.Default.GetHash); return(null); } }
public static bool CheckSHA512(byte[] data, byte[] control) { byte[] result; SHA512 shaM = new SHA512Managed(); result = shaM.ComputeHash(data); shaM.Dispose(); for (int k = 0; k < result.Length; k++) { if (result[k] != control[k]) { return(false); } } return(true); }
/// <summary> /// SHA512加密 哈希加密一个字符串 生成128位字符串 /// </summary> /// <param name="Security"></param> /// <returns></returns> public static string SHA512FileString(string filePathName) { SHA512Managed sha512 = new SHA512Managed(); byte[] bt; using (FileStream fs = new FileStream(filePathName, FileMode.Open, FileAccess.Read)) { bt = sha512.ComputeHash(fs); } StringBuilder sb = new StringBuilder(); //foreach (byte o in Value) //{ // Security += (int)o + "O"; //} foreach (byte o in bt) { sb.Append(o.ToString("X2")); } sha512.Dispose(); return(sb.ToString()); }
public void SaveRandomInfo() { byte[] parameters = new byte[SaveFile.ParametersLengthInBytes]; byte[] map = new byte[SaveFile.MapLengthInBytes]; Random r = new Random(); r.NextBytes(parameters); r.NextBytes(map); byte[] info = parameters.Concat(map).ToArray(); SaveFile file = new SaveFile("test", parameters, map); SHA512 sha512 = new SHA512Managed(); var exceptedHash = sha512.ComputeHash(info); sha512.Dispose(); for (int i = 0; i < SaveFile.ParametersLengthInBytes; ++i) { Assert.AreEqual(parameters[i], file.ParametersInfo[i]); } for (int i = 0; i < SaveFile.MapLengthInBytes; ++i) { Assert.AreEqual(map[i], file.MapInfo[i]); } for (int i = 0; i < SaveFile.InfoLengthInBytes; ++i) { Assert.AreEqual(info[i], file.FullInfo[i]); } for (int i = 0; i < 64; ++i) { Assert.AreEqual(exceptedHash[i], file.Hash[i]); } File.Delete(file.Path); }