Posts Tagged ‘ Hashing ’

How to hash a string (Password)

If you want to hash a string (password or whatever) you can use the following hashing methods.
Generally a Hash has the advantage that there is no way back, which means if you hash a password you can not find out the password with the hash.
The only way to do that is bruteforcing which could take years (depending on the computer and on the strength of the password).

Note that the MD5 algorithm is not as save as the SHA algorithms

public static string md5encrypt(string phrase)
{
UTF8Encoding encoder = new UTF8Encoding();
MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}

public static string sha1encrypt(string phrase)
{
UTF8Encoding encoder = new UTF8Encoding();
SHA1CryptoServiceProvider sha1hasher = new SHA1CryptoServiceProvider();
byte[] hashedDataBytes = sha1hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}

public static string sha256encrypt(string phrase)
{
UTF8Encoding encoder = new UTF8Encoding();
SHA256Managed sha256hasher = new SHA256Managed();
byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}

public static string sha384encrypt(string phrase)
{
UTF8Encoding encoder = new UTF8Encoding();
SHA384Managed sha384hasher = new SHA384Managed();
byte[] hashedDataBytes = sha384hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}

public static string sha512encrypt(string phrase)
{
UTF8Encoding encoder = new UTF8Encoding();
SHA512Managed sha512hasher = new SHA512Managed();
byte[] hashedDataBytes = sha512hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}

public static string byteArrayToString(byte[] inputArray)
{
StringBuilder output = new StringBuilder(“”);
for (int i = 0; i < inputArray.Length; i++) { output.Append(inputArray[i].ToString("X2")); } return output.ToString(); } [/sourcecode]

Advertisements