2013-02-20 74 views

回答

2

我假設你已經擁有的架構,用於存儲用戶不知道散列某種用戶配置文件表?

比方說,此表的格式如下:

PersonID   int PrimaryKey 
PersonName   nvarchar(50) 
PersonPasswordHash varchar(128) 
PersonPasswordSalt nvarchar(10) 

然後在你的.NET代碼(例如C#),你會繼續前進,執行以下操作,當你創建一個新用戶

string passwordPlain = txtPassword.Text; // This is the password entered by the user 

/* a work factor of 10 is default, but you can mention any thing from 4 to 31. 
    But keep in mind that for every increment in work factor the work increases 
    twice (its 2**log_rounds) 
*/ 
string passwordSalt = BCrypt.GenerateSalt(10); 
string passwordHash = BCrypt.HashPassword(passwordPlain, passwordSalt); 

// Now store the passwordHash and passwordSalt in the database for that user 

當您具有上述值後,將在數據庫中存儲適當的值。

當它的時間來驗證登錄,檢索從數據庫中passwordHashpasswordSalt的細節,你可以驗證如下:

string originalPasswordSalt; // retireive its value from the DB 
string originalPasswordHash; // retireive its value from the DB 
string givenPasswordPlain = txtPassword.Text; // given by the user during login 
string givenPasswordHash = BCrypt.HashPassword(givenPasswordPlain, originalPasswordSalt); 

if(givenPasswordHash.Equals(originalPasswordHash)) { // you have an valid user 
} else { // given login name or password is not valid 
} 
+1

首先,我想說對不起,也許我的問題是不夠清楚。 我想知道如何在T-SQL中使用BCrypt,你介紹一些關於在T-SQL中使用BCrypt的例子嗎? – 2013-02-20 10:41:20

+2

檢查http://blog.tcs.de/using-the-bcrypt-hash-algorithm-in-ms-sql-server/ – 2013-02-20 14:00:00

+0

謝謝!這真的很有幫助。 – 2013-02-20 21:12:31