在纯 T-SQL 中生成 ASP.Net 会员密码哈希
我正在尝试在 ASP.Net 会员系统中创建默认 SHA-1 密码哈希的纯 t-sql 表示。理想情况下,我会得到这样的结果:
UserName Password GeneratedPassword
cbehrens 34098kw4D+FKJ== 34098kw4D+FKJ==
注意:那是伪造的 base-64 文本。我有正确往返的 base64_encode 和解码函数。这是我的尝试,但不起作用:
SELECT UserName, Password, dbo.base64_encode(HASHBYTES('SHA1', dbo.base64_decode(PasswordSalt) + 'test')) As TestPassword FROM aspnet_Users U JOIN aspnet_membership M ON U.UserID = M.UserID
我尝试了该主题的多种变体,但无济于事。我需要在纯 T-Sql 中执行此操作;涉及控制台应用程序或类似的东西将使工作量加倍。
因此,如果有人能够提供从 ASP.Net 会员资料中复制该密码的准确语法,我将不胜感激。
I'm attempting to create a pure t-sql representation of the default SHA-1 password hashing in the ASP.Net Membership system. Ideally, what I would get would be this:
UserName Password GeneratedPassword
cbehrens 34098kw4D+FKJ== 34098kw4D+FKJ==
Note: that's bogus base-64 text there. I've got base64_encode and decode functions that round-trip correctly. Here's my attempt, which doesn't work:
SELECT UserName, Password, dbo.base64_encode(HASHBYTES('SHA1', dbo.base64_decode(PasswordSalt) + 'test')) As TestPassword FROM aspnet_Users U JOIN aspnet_membership M ON U.UserID = M.UserID
I've tried a number of variations on the theme, to no avail. I need to do this in pure T-Sql; involving a console app or something like that will double the work.
So if anyone can supply what precisely the syntax should be to duplicate that password from the ASP.Net membership stuff, I would greatly appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我通过对这里的 C# 代码进行反向工程编写了一个哈希存储过程 ASP.NET Identity 默认密码哈希器,它是如何工作的,安全吗? 以及这里的一些出色的 PBKDF2 SQL 函数 PBKDF2 是否有 SQL 实现?
首先创建这两个函数,取自 PBKDF2有SQL实现吗?
然后
创建存储过程来生成哈希密码
最后执行存储过程使用
请注意,对于更高版本的 SQL Server,存储过程可能需要更改,因为这是专门为 2005 年编写的,我相信在更高版本中转换为 base64 是不同的。
I wrote a hashing stored proc by reverse enginering the C# code from here ASP.NET Identity default Password Hasher, how does it work and is it secure? and some fantastic PBKDF2 SQL functions from here Is there a SQL implementation of PBKDF2?
First create these two functions taken from Is there a SQL implementation of PBKDF2?
and
then create the stored proc to generate the hash password
Finally execute the stored proc using
Please note that the stored proc may need to be changed for later versions of SQL server as this was written specifically for 2005 and I belive conversion to base64 is different in later versions.
如果您运行的是 2005 或更高版本,则可以创建 CLR (.NET) UDF:
您需要在类中包含以下命名空间:
该类必须是 public。
构建 .dll,然后运行以下(每个要调用 UDF 的数据库)SQL 语句:
显然,将“NAMESPACE.CLASSNAME”替换为您的类的命名空间(如果有)和名称。您可能想弄乱输入参数和返回值的大小。
然后使用 T-SQL 调用 UDF:
对我有用:)
if you are running 2005 or higher, you can create a CLR (.NET) UDF:
you need to include the following namespaces in your class:
the class must be public.
build the .dll then run the following (per database you want to call the UDF) SQL statement:
obviously, replace 'NAMESPACE.CLASSNAME' with the namespace (if any) and name of your class. and you might want to mess with the input parameter and return value sizes.
then call the UDF with T-SQL:
works for me :)
您可以在 SQL 中创建此函数,而不是使用 CLR。在此页面上,您会发现非常好的示例:
http://svakodnevnica.com.ba/index.php?option=com_kunena&func=view&catid=4&id=4&Itemid=5&lang=en#7
PS byte[] src = Convert.FromBase64String(salt);是正确的方法...
狐狸
Instead of using CLR you can create this function in SQL. On this page you will find very nice example:
http://svakodnevnica.com.ba/index.php?option=com_kunena&func=view&catid=4&id=4&Itemid=5&lang=en#7
P.S. byte[] src = Convert.FromBase64String(salt); is correct way...
Fox
OP 请求“纯”sql - 我认为使用 CLR 是作弊;)我很固执,必须自己弄清楚,所以这就是我所做的。
注意:先备份!!
计算哈希值的函数:
我正在将会员数据库从“清晰”密码更改为更安全的哈希格式 - 像这样调用它:
即使我的数据库最初设置为“清晰”密码,盐值也是为每条记录创建的,但是,如果由于某种原因您没有盐值,您可以使用以下命令创建它们:
然后像这样使用它:
享受吧!
OP requested "pure" sql - I think using CLR is cheating ;) I was stubborn and had to figure it out for myself so here's what I did.
NOTE: Make a backup first!!
Function to calculate the hashes:
I was changing a Membership database from "clear" passwords to the more secure hashed format - call it like this:
Even with my database originally set to "clear" passwords, the salt values were created with each record, however, if for some reason you don't have salt values you can create them with this:
Then use it like this:
Enjoy!
根据 this SO post, 这是他们用来编码/散列您的密码/盐的过程。
我可能是错的,但看起来您错过了获取密码和盐字节的步骤。您可以尝试添加它并看看它是否有效?
According to this SO post, this is the process they use to encode/hash your password/salt.
I could be wrong but it looks like you are missing the step where you get the bytes for the password and salt. Can you try adding that and see if it works?