7

I am looking into creating a custom members login system (for learning) and I haven't been able to figure out the C# command to generate an encrypted hash.

Is there a certain namespace I need to import or anything like that?

Mathieu
  • 4,449
  • 7
  • 41
  • 60
quakkels
  • 11,676
  • 24
  • 92
  • 149

4 Answers4

19

Using the namespace System.Security.Cryptography:

MD5 md5 = new MD5CryptoServiceProvider();
Byte[] originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
Byte[]  encodedBytes = md5.ComputeHash(originalBytes);

return BitConverter.ToString(encodedBytes);

or FormsAuthentication.HashPasswordForStoringInConfigFile method

Gregoire
  • 24,219
  • 6
  • 46
  • 73
4

For my part, I purpose this function i use to get the gravatar picture profil:

you can use it like you want

public string getGravatarPicture()
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        Byte[] originalBytes = ASCIIEncoding.Default.GetBytes(email.ToLower());
        Byte[] encodedBytes = md5.ComputeHash(originalBytes);

        string hash = BitConverter.ToString(encodedBytes).Replace("-", "").ToLower();
        return "http://www.gravatar.com/avatar/"+hash+"?d=mm";
    }
Osin
  • 485
  • 1
  • 4
  • 12
3

Well, first of all an encryption hash is a contradiction. Like a vegetarian steak. You can use encryption, or you can hash them (and you should hash them), but hashing is not encryption.

Look up a class starting with Md5 ;) Or Sha1 - those are hash algoryithms. It is all there in .NET (System.Security.Cryptography namespace).

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • 1
    Hashing of often called "one-way encryption" – Charles Burns Apr 03 '15 at 19:10
  • 1
    @CharlesBurns But it shouldn't be, because its not encryption. There is no such thing as "one way vegan steak" – DeltaTango Jul 04 '18 at 21:58
  • 1
    @DeltaTango: You are right, technically hashing isn't encryption despite word usage because it's hard/impossible-ish to reverse. Still, usage writes the dictionary, and I lean towards more of a compression analogy than a steak analogy. AES is "lossless encryption", like PNG for images, whereas SHA is "lossy" like JPG, compressing the information down to a yes(probably)/no answer. Of course, JPG is useless if it "compresses" a photo down to a single black/white pixel. :) – Charles Burns Jul 04 '18 at 22:17
  • 1
    Also encryption can be used for hashing, after a fashion. Use the data as its own key. This is where the analogy above really starts to break apart, though perhaps less so than steak (perhaps, "using steak to feed the plants which end up as a vegetarian steak". That hurt a little to type. – Charles Burns Jul 04 '18 at 22:21
2

I prefer having my hash all in one concatenated string. I borrowed this to build my hash:

public static string MD5Hash(string itemToHash)
{
    return string.Join("", MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(itemToHash)).Select(s => s.ToString("x2")));
}
CIA
  • 302
  • 1
  • 5
  • 16