vendredi 28 octobre 2016

Genreating Hashes using .NET Core

I need to Generate Random Unique hashes to be used in password reset links in .Net Core. In ASP.NET Identity, I found this to be helpful, I am wondering if RNGCryptoServiceProvider still assures the randomness of the hashes generated.

 using (RNGCryptoServiceProvider
                                rngCsp = new RNGCryptoServiceProvider())
            {
                var data = new byte[4];
                for (int i = 0; i < 10; i++)
                {
                    //filled with an array of random numbers
                    rngCsp.GetBytes(data);
                    //this is converted into a character from A to Z
                    var randomchar = Convert.ToChar(
                        //produce a random number 
                        //between 0 and 25
                                              BitConverter.ToUInt32(data, 0) % 26
                        //Convert.ToInt32('A')==65
                                              + 65
                                     );
                    token.Append(randomchar);
                }
            }

What is the best way to achieve that using .net core and using which classes?




Aucun commentaire:

Enregistrer un commentaire