在数据库中存储 SHA512 密码哈希值

发布于 2024-08-13 23:06:39 字数 481 浏览 5 评论 0原文

在我的 ASP.NET Web 应用程序中,我使用 SHA512 对用户密码进行哈希处理。

尽管进行了很多搜索和谷歌搜索,但我不清楚应该如何将它们存储在数据库中(SQL2005) - 下面的代码显示了我如何将哈希创建为字符串的基础知识,并且我当前将其插入到将数据库写入 Char(88) 列,因为这似乎是一致创建的长度

将其保存为字符串是最好的方法,如果是这样,它在 SHA512 上将始终是 88 个字符(正如我在 上看到的一些奇怪的东西)谷歌)?

 Dim byteInput As Byte() = Encoding.UTF8.GetBytes(sSalt & sInput)
 Dim hash As HashAlgorithm = New SHA512Managed()
 Dim sInsertToDatabase As String =  Convert.ToBase64String(hash.ComputeHash(byteInput))

In my ASP.NET web app I'm hashing my user passwords with SHA512.

Despite much SO'ing and Googling I'm unclear how I should be storing them in the database (SQL2005) - the code below shows the basics of how I'm creating the hash as a string and I'm currently inserting it into the database into a Char(88) column as that seems to be the length created consistently

Is holding it as a String the best way to do it, if so will it always be 88 chars on a SHA512 (as I have seen some bizarre stuff on Google)?

 Dim byteInput As Byte() = Encoding.UTF8.GetBytes(sSalt & sInput)
 Dim hash As HashAlgorithm = New SHA512Managed()
 Dim sInsertToDatabase As String =  Convert.ToBase64String(hash.ComputeHash(byteInput))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水般的淡然安静女子 2024-08-20 23:06:39

SHA512 输出 512 位,即 64 字节。如果您愿意,可以将这 64 个字节存储在二进制列中。

如果您想在应用程序外部处理哈希,则存储 Base64 字符串会更方便,就像您现在所做的那样。 Base64 大约增加了 33% 的恒定开销,因此您可以预期字符串始终为 88 个字符。

也就是说,ASP.NET 有一个内置的相当全面的身份验证系统,你应该使用。

SHA512 outputs 512 bits, or 64 bytes. You can store those 64 bytes in a binary column, if you so wished.

If you want to handle the hash outside your application is more comfortable to store a Base64 string, as you are doing now. Base64 adds roughly a 33% of constant overhead, so you can expect the string to be always 88 chars.

That said, ASP.NET has a fairly comprehensive authentication system builtin, which you should use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文