Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Secure Password Programming with .NET

I want to convert a Secure Password in secure hashcode. best method?

like: SHA1,MD5 and any combination ?

string str ="Krishna";

Output:"!#$!$ASDFAS@#$%@";
like image 230
Krishna N Avatar asked Sep 03 '26 00:09

Krishna N


1 Answers

There are different ways to create a random piece of data that can be used for salting. The most common ones are:

  • Creating a random GUID by using the Guid type
  • Creating a random string of digits by using the RNGCryptoServiceProvider class

To create a new random GUID, we invoke the NewGuid method on the Guid type. Once generated, we simply append the salt to the string to be encrypted.

string saltAsString = Guid.NewGuid().ToString();

For creating a random string of digits by using the RNGCryptoServiceProvider class, we first initialize a provider and a byte array, and then invoke the GetBytes method on our provider instance.

byte[] saltInBytes = new byte[8];
RNGCryptoServiceProvider saltGenerator = new RNGCryptoServiceProvider();
saltGenerator.GetBytes(saltInBytes);
string saltAsString = Convert.ToBase64String(saltInBytes);

The following code is a modified version of the previous snippet to demonstrate salting.

public void HashText()
{
    string textToHash = "password"; 
    string saltAsString = Guid.NewGuid().ToString();
    byte[] byteRepresentation 
        = UnicodeEncoding.UTF8.GetBytes(textToHash + saltAsString);

    byte[] hashedTextInBytes = null; 
    MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider();
    hashedTextInBytes = myMD5.ComputeHash(byteRepresentation); 
    string hashedText = Convert.ToBase64String(hashedTextInBytes); 

    // will display X03MO1qnZdYdgyfeuILPmQ==
    MessageBox.Show(hashedText);
}
like image 100
3 revs, 3 users 72%Madhu Avatar answered Sep 05 '26 14:09

3 revs, 3 users 72%Madhu