簡體   English   中英

如何在數據庫中存儲鹽化的哈希密碼

[英]How to store salted hashed password in the database

我試圖找出一種方法來將加鹽的哈希密碼存儲到我的數據庫中。 這是我第一次這樣做,所以我不確定該如何進行。

我的ASP.NET C#Web應用程序首先要求用戶注冊一個帳戶。 我有一個數據層,一個業務層,用於在數據層中調用方法,以及一個表示層,用於顯示表單控件。 目前,我正在使用純文本格式將密碼存儲在數據庫中。 讓我們在數據層中調用該方法createAccount() 用戶名,密碼和其他屬性傳遞到createAccount()方法中,以調用SQL查詢來創建Account記錄。 用戶名和密碼由用戶在注冊頁面中指定。 我找到了一個網站,該網站為我提供了一種使用哈希生成密碼的方法,如下所示:

        public static string GenerateHashWithSalt(string password, string salt)
    {
        // merge password and salt together
        string sHashWithSalt = password + salt;
        // convert this merged value to a byte array
        byte[] saltedHashBytes = Encoding.UTF8.GetBytes(sHashWithSalt);
        // use hash algorithm to compute the hash
        System.Security.Cryptography.HashAlgorithm algorithm = new System.Security.Cryptography.SHA256Managed();
        // convert merged bytes to a hash as byte array
        byte[] hash = algorithm.ComputeHash(saltedHashBytes);
        // return the has as a base 64 encoded string
        return Convert.ToBase64String(hash);
    }

在注冊頁面,代碼隱藏( .cs )頁面或數據層中,該放在哪里? 然后,你們都怎么建議我將這個哈希密碼和其他屬性一起放在數據庫中? 我應該將其直接存儲為哈希值,還是先以純文本格式存儲,然后將其更新為哈希值。 謝謝!

我建議您使用現有的實現,而不要在這里滾動自己的實現。

asp.net成員數據庫非常適合此用途 ,並且開箱即用地使用了鹽化和哈希處理

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM