SQL Server HASHBYTES('sha1',@userpwd)等价于C# [英] Equivalent of SQL Server HASHBYTES('sha1', @userpwd ) in C#
问题描述
我试图找到这个函数的等价物 HASHBYTES('生成的值为
HASHBYTES('sha1','Glenw00d @ 3','sha1',password)
in C#code。
зmG>TëÏåÈÇOó26¥
C#是否具有与此T-SQL函数相同的功能?
UPDATE:
I试图使用此代码:
public static class SHA1Util
{
///< summary>
///计算字符串的散列,编码为UTF8
///< / summary>
///< param name =s>要被哈希的字符串< / param>
///<返回> 40个字符的十六进制字符串< /返回>
public static string SHA1HashStringForUTF8String(string s)
{
byte [] bytes = Encoding.UTF8.GetBytes(s);
var sha1 = SHA1.Create();
byte [] hashBytes = sha1.ComputeHash(bytes);
返回HexStringFromBytes(hashBytes);
}
///< summary>
///将字节数组转换为十六进制数字字符串
///< / summary>
///< param name =bytes>字节数组< / param>
///<返回>十六进制数字字符串< /返回>
public static string HexStringFromBytes(byte [] bytes)
{
var sb = new StringBuilder();
foreach(字节b以字节为单位)
{
var hex = b.ToString(x2);
sb.Append(hex);
}
return sb.ToString();
}
}
但它会返回不同的结果。
var hashString = SHA1Util.SHA1HashStringForUTF8String(Glenw00d @@ 3); //结果为d0b76d473e945417ebcf18e5c893c74ff33236a5
$ c $除非您更改默认的内部编码,否则通常SQL Server会使用Windows-1252作为 VARCHAR()
。 private static readonly Encoding Encoding1252 = Encoding.GetEncoding(1252);
///< summary>
///编码为Windows-1252
的字符串计算散列值///< / summary>
///< param name =s>要被哈希的字符串< / param>
///<返回> 40个字符的十六进制字符串< /返回>
public static string SHA1HashStringForDefaultString(string s)
{
byte [] bytes = Encoding1252.GetBytes(s);
发现问题 :
你正在哈希两个不同的字符串
HASHBYTES('sha1',Glenw00d @ 3)
和
SHA1Util.SHA1HashStringForUTF8String(Glenw00d @@ 3)
查看 @
/ @@
?
现在,对于编码,我会说这是Windows-1252。但您可以使用
选择TABLE_NAME,COLUMN_NAME,Columns.COLLATION_NAME来自INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='Yourtable'
如果列的排序规则包含 CP1
那么它就是Windows-1252 。
对于输出...如果您想要一个字符串,请将返回值替换为:
return Encoding1252.GetString (HASHBYTES);
对于 Glenw00d @@ 3
: 0LdtRz6UVBfrzxjlyJPHT / MyNqU =
如果您想要一个十六进制字符串,请保持原样返回,
对于 Glenw00d @@ 3
哈希值为: d0b76d473e945417ebcf18e5c893c74ff33236a5
如果你想要它base64:
return Convert.ToBase64String(hashBytes);
对于 Glenw00d @@ 3
: 0LdtRz6UVBfrzxjlyJPHT / MyNqU =
I am trying to migrate some stored procedures to C# code.
I am trying to find what is equivalent for this function HASHBYTES('sha1', password)
in C# code.
Generated values of HASHBYTES('sha1', "Glenw00d@3")
in T-SQL is "зmG>"TëÏåÈ"ÇOó26¥"
Does C# have an equivalent of this T-SQL function or not?
UPDATE:
I tried to use this code:
public static class SHA1Util
{
/// <summary>
/// Compute hash for string encoded as UTF8
/// </summary>
/// <param name="s">String to be hashed</param>
/// <returns>40-character hex string</returns>
public static string SHA1HashStringForUTF8String(string s)
{
byte[] bytes = Encoding.UTF8.GetBytes(s);
var sha1 = SHA1.Create();
byte[] hashBytes = sha1.ComputeHash(bytes);
return HexStringFromBytes(hashBytes);
}
/// <summary>
/// Convert an array of bytes to a string of hex digits
/// </summary>
/// <param name="bytes">array of bytes</param>
/// <returns>String of hex digits</returns>
public static string HexStringFromBytes(byte[] bytes)
{
var sb = new StringBuilder();
foreach (byte b in bytes)
{
var hex = b.ToString("x2");
sb.Append(hex);
}
return sb.ToString();
}
}
but it returns different results.
var hashString = SHA1Util.SHA1HashStringForUTF8String("Glenw00d@@3");//result for this was d0b76d473e945417ebcf18e5c893c74ff33236a5
解决方案 Unless you change the default endoding, normally SQL Server uses Windows-1252 for VARCHAR()
.
private static readonly Encoding Encoding1252 = Encoding.GetEncoding(1252);
/// <summary>
/// Compute hash for string encoded as Windows-1252
/// </summary>
/// <param name="s">String to be hashed</param>
/// <returns>40-character hex string</returns>
public static string SHA1HashStringForDefaultString(string s)
{
byte[] bytes = Encoding1252.GetBytes(s);
Found the problem:
You are hashing two different strings
HASHBYTES('sha1', "Glenw00d@3")
and
SHA1Util.SHA1HashStringForUTF8String("Glenw00d@@3")
See the @
/@@
?
Now, for the encoding, I'll say that it is Windows-1252. But you can check with
Select TABLE_NAME, COLUMN_NAME, Columns.COLLATION_NAME From INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Yourtable'
If the collation for your column contains the CP1
then it is Windows-1252.
For the output... If you want a string, replace the return with:
return Encoding1252.GetString(hashBytes);
For Glenw00d@@3
the hash is: 0LdtRz6UVBfrzxjlyJPHT/MyNqU=
if you want an hex string, leave the return as is,
For Glenw00d@@3
the hash is: d0b76d473e945417ebcf18e5c893c74ff33236a5
if you want it base64:
return Convert.ToBase64String(hashBytes);
For Glenw00d@@3
the hash is: 0LdtRz6UVBfrzxjlyJPHT/MyNqU=
这篇关于SQL Server HASHBYTES('sha1',@userpwd)等价于C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!