Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Phương pháp CreateHashedPassword tạo ra mật khẩu muối và băm. các mật khẩu tham số là văn bản đơn giản của mật khẩu, tham số existingSalt là muối để sử dụng khi tạo ra phiên bản muối và băm. Nếu muối không tồn tại, chẳng hạn như lần đầu tiên một mật khẩu được lưu trữ, existingSalt nên được thông qua như vô giá trị, và một ngẫu nhiên muối được tạo ra. Một khi bạn có muối, nó được kết hợp với mật khẩu đồng bằng văn bản và băm bằng cách sử dụng lớp SHA512Managed. Giá trị muối sau đó được nối thêm vào. | return pwdNode.InnerText return set XmlNode pwdNode settingsDoc.SelectSingleNode Settings Password string hashedPassword CreateHashedPassword value null if pwdNode null pwdNode.InnerText hashedPassword else XmlNode settingsNode settingsDoc.SelectSingleNode Settings XmlElement pwdElem settingsDoc.CreateElement Password pwdElem.InnerText hashedPassword settingsNode.AppendChild pwdElem _ The CreateHashedPassword method creates the salted and hashed password. The password parameter is the plain text of the password the existingSalt parameter is the salt to use when creating the salted and hashed version. If no salt exists such as the first time a password is stored existingSalt should be passed as null and a random salt will be generated. Once you have the salt it is combined with the plain text password and hashed using the SHA512Managed class. The salt value is then appended to the end of the hashed value and returned. The salt is appended so that when you attempt to validate the password you know what salt was used to create the hashed value. The entire value is then base64-encoded and returned Make a hashed password. private string CreateHashedPassword string password byte existingSalt byte salt null if existingSalt null Make a salt of random size. Create a stronger hash code using RNGCryptoServiceProvider. byte random new byte 1 RNGCryptoServiceProvider rngSize new RNGCryptoServiceProvider Populate with random bytes. rngSize.GetBytes random Convert random bytes to string. Storing Data Securely 679 int size Convert.ToInt32 random Create salt array. salt new byte size Use the better random number generator to get bytes for the salt. RNGCryptoServiceProvider rngSalt new RNGCryptoServiceProvider rngSalt.GetNonZeroBytes salt else salt existingSalt Turn string into bytes. byte pwd Encoding.UTF8.GetBytes password Make storage for both password and salt. byte saltedPwd new byte pwd.Length salt.Length Add pwd bytes first. pwd.CopyTo saltedPwd 0 now add salt salt.CopyTo .