使用 Nhibernate 存储 Sha256 哈希密码

发布于 2024-11-15 11:22:37 字数 535 浏览 3 评论 0原文

我有一个简单的 Web 应用程序,有一个登录页面,前端域有一个简单的模型:

string username {get;set;}
string password {get;set;}

但是在发送到数据域对象之前它是用 sha256 加密的,最初的数据域是:

string username {get;set;}
string password {get;set;}

所以我会采取从 ui 获取密码,然后使用 Encoding.GetString() 方法对其进行加密,获取用于持久保存的字符串。然而,当我尝试保留数据时,它似乎给出了一个异常,我认为这归因于字符串中的字符无效。

环顾四周后,有些人建议将其存储为 varbinary(32) 并在数据层中使用 byte[] 密码 {get;set;},但是我无法让 Nhibernate 正确映射它。

那么谁能告诉我使用 Nhibernate 和 SqlServer 或 MySql 存储哈希密码的最佳实践。 (该应用程序支持这两种数据库)

I have a simple web app that has a login page, the front end domain has a simple model with:

string username {get;set;}
string password {get;set;}

However it is encrypted with sha256 before it is sent over to the data domain object, originally the data domain was:

string username {get;set;}
string password {get;set;}

So I would take the password from the ui, encrypt it then using the Encoding.GetString() method, get back a string for persisting. However when I try to persist the data it just seems to give an exception, which I believe to be down to the characters in the string not being valid.

After looking around some people have recommended storing it as a varbinary(32) and using a byte[] password {get;set;} within the data layer, however I couldn't get Nhibernate to map it correctly.

So can anyone tell me the best practise on storing hashed passwords with Nhibernate and SqlServer or MySql. (The app supports both dbs)

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

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

发布评论

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

评论(1

猫烠⑼条掵仅有一顆心 2024-11-22 11:22:37

您不应该对 SHA 值使用 Encoding.GetString(),因为它会生成可能无法正确存储的文本,或者更糟糕的是,会出现异常。而是使用类似 Convert.ToBase64String 的东西。

You shouldn't use Encoding.GetString() on a SHA value since it will produce text that may not store correctly, or worse, give an exception. Rather use something like Convert.ToBase64String.

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