How to create an encryption hash code in ASP.NET MVC?

I am learning how to create a custom login system (for training), and I could not calculate the C # command to generate an encrypted hash.

Is there some namespace that I need to import or something like that?

+4
source share
4 answers

Using the System.Security.Cryptography Namespace:

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

or FormsAuthentication.HashPasswordForStoringInConfigFile Method

+16
source

For my part, I assign this function, which I use to get the gravatar profile profile:

you can use it however 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"; } 
+3
source

Well, firstly, hash encryption is a contradiction. Like a vegetarian steak. You can use encryption, or you can use them (and you must use them), but hashing is not encryption.

Look at the class starting with Md5;) Or Sha1 are hash algorithms. This is all in .NET (the System.Security.Cryptography namespace).

+2
source

I prefer to have my hash in one concatenated string. I borrowed this to build a hash:

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

Source: https://habr.com/ru/post/1309346/


All Articles