在表我有一列URL
這我使用保存的網址。我正在通過使用公式(CONVERT([varbinary](20),hashbytes('SHA1',[URL]))
)計算另一列中的散列。它工作正常。如何使用C#計算hashbyte SHA1?
現在我需要在C#中類似的函數來獲得哈希,這樣我可以比較和確認類似的排犯規存在之前,我插入新行。我嘗試了幾個鏈接,但沒有運氣。
這裏是鏈接:
How do I calculate the equivalent to SQL Server (hashbytes('SHA1',[ColumnName])) in C#?
** I found this link working. All I need to do is change formula in the db. but is it possible to make it in one line
**
http://forums.asp.net/t/1782626.aspx
DECLARE @HashThis nvarchar(4000);
DECLARE @BinHash varbinary(4000);
SELECT @HashThis = CONVERT(nvarchar(4000),'[email protected]');
SELECT @BinHash = HashBytes('SHA1', @HashThis);
SELECT cast(N'' as xml).value('xs:base64Binary(xs:hexBinary(sql:variable("@BinHash")))', 'nvarchar(4000)');
在C#
string pwd = "[email protected]";
var sha1Provider = HashAlgorithm.Create("SHA1");
var binHash = sha1Provider.ComputeHash(Encoding.Unicode.GetBytes(pwd));
Console.WriteLine(Convert.ToBase64String(binHash));
我使用SQL Server 2012的排序規則的數據庫是SQL_Latin1_General_CP1_CI_AS
感謝
Paraminder
如果您將使用C#顯示您正在嘗試的內容,這將有所幫助,因此我們可以檢查該代碼是否存在錯誤。 –
這兩個鏈接都顯示如何在C#中創建一個哈希,所以你的問題是什麼? – slfan
c#代碼不給我準確的哈希,我在數據庫中有什麼。 – Parminder