Java 计算字符串 SHA-1 摘要的十六进制表示
我将用户密码作为 sha1 哈希存储在数据库上。
不幸的是我得到了奇怪的答案。
我将字符串存储为这样:
MessageDigest cript = MessageDigest.getInstance("SHA-1");
cript.reset();
cript.update(userPass.getBytes("utf8"));
this.password = new String(cript.digest());
我想要这样的东西 -->
aff--> “0c05aa56405c447e6678b7f3127febde5c3a9238”
而不是
aff --> �V@\D~fx����:�8
I'm storing the user password on the db as a sha1 hash.
Unfortunately I'm getting strange answers.
I'm storing the string as this:
MessageDigest cript = MessageDigest.getInstance("SHA-1");
cript.reset();
cript.update(userPass.getBytes("utf8"));
this.password = new String(cript.digest());
I wanted something like this -->
aff --> "0c05aa56405c447e6678b7f3127febde5c3a9238"
rather than
aff --> �V@\D~fx����:�8
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
使用apache通用编解码器库:
结果是0c05aa56405c447e6678b7f3127febde5c3a9238
就是这样:)
Using apache common codec library:
The result is 0c05aa56405c447e6678b7f3127febde5c3a9238
That's it :)
发生这种情况是因为 cript.digest() 返回一个字节数组,您试图将其打印为字符串。您想将其转换为可打印的十六进制字符串。
简单的解决方案:使用 Apache 的 commons-codec 库:
This is happening because cript.digest() returns a byte array, which you're trying to print out as a character String. You want to convert it to a printable Hex String.
Easy solution: Use Apache's commons-codec library:
哈希算法的一次迭代并不安全。太快了。您需要通过多次迭代哈希来执行密钥强化。
此外,您没有对密码加盐。这会给预先计算的字典(例如“彩虹表”)带来漏洞。
您可以使用 Java 运行时内置的代码,而不是尝试滚动自己的代码(或使用一些粗略的第三方膨胀软件)来正确执行此操作。有关详细信息,请参阅此答案。
正确地对密码进行哈希处理后,您将获得一个
byte[]
。将其转换为十六进制String
的一个简单方法是使用BigInteger
类:如果您想确保字符串始终有 40 个字符,您可能需要执行一些操作在左侧填充零(您可以使用 String.format() 来完成此操作。)
One iteration of a hash algorithm is not secure. It's too fast. You need to perform key strengthening by iterating the hash many times.
Furthermore, you are not salting the password. This creates a vulnerability to pre-computed dictionaries, like "rainbow tables."
Instead of trying to roll your own code (or using some sketchy third-party bloatware) to do this correctly, you can use code built-in to the Java runtime. See this answer for details.
Once you have hashed the password correctly, you'll have a
byte[]
. An easy way to convert this to a hexadecimalString
is with theBigInteger
class:If you want to make sure that your string always has 40 characters, you may need to do some padding with zeroes on the left (you could do this with
String.format()
.)如果您不想向项目添加任何额外的依赖项,您也可以使用
If you don't want to add any extra dependencies to your project, you could also use
crypt.digest() 方法返回一个 byte[]。该字节数组是正确的 SHA-1 总和,但加密哈希值通常以十六进制形式显示给人类。散列中的每个字节将产生两个十六进制数字。
要安全地将字节转换为十六进制,请使用:
此代码片段可以是用于将字符转换为十六进制:
请注意,在 Java 中使用字节非常容易出错。我会仔细检查所有内容并测试一些奇怪的情况。
您还应该考虑使用比 SHA-1 更强的东西。
http://csrc.nist.gov/groups/ST/hash/statement.html
The crypt.digest() method returns a byte[]. This byte array is the correct SHA-1 sum, but crypto hashes are typically displayed to humans in hex form. Each byte in your hash will result in two hex digits.
To safely convert a byte to hex use this:
This code snippet can be used for converting a char to hex:
Note that working with bytes in Java is very error prone. I would double check everything and test some strange cases as well.
Also you should consider using something stronger than SHA-1.
http://csrc.nist.gov/groups/ST/hash/statement.html
使用 Google Guava:
Maven:
示例:
With Google Guava:
Maven:
Sample:
在java 17中 <引入了code>HexFormat。
它允许你写:
In java 17
HexFormat
is introduced.It allows you to write:
如果你使用 Spring,它非常简单:
If you use Spring its quite simple:
不可逆存储密码涉及的不仅仅是简单的标准哈希算法。
有关详细信息,请参阅
您还可以使用 http://en.wikipedia.org/wiki/Password-authenticated_key_agreement 方法,以避免将密码以明文形式传递到服务器。
There's more than just simple standard hash algorithms involved in storing passwords nonreversible.
For more info, see e.g.
You could also use a http://en.wikipedia.org/wiki/Password-authenticated_key_agreement method to avoid passing the password in cleartext to the server at all.
摘要() 返回一个字节数组,您将使用默认编码将其转换为字符串。你想要做的是对其进行 Base64 编码。
digest() returns a byte array, which you're converting to a string using the default encoding. What you want to do is base64 encode it.
要使用 UTF-8,请执行以下操作:
要从摘要中获取 Base64 字符串,您可以执行以下操作:
由于
MessageDigest.digest()
返回一个字节数组,因此您可以将其转换使用 Apache 的 十六进制编码 转换为字符串 (更简单)。例如
To use UTF-8, do this:
And to get a Base64 String from the digest, you can do something like this:
Since
MessageDigest.digest()
returns a byte array, you can convert it to String using Apache's Hex Encoding (simpler).E.g.
将 byte[] 转换为 base64 字符串怎么样?
How about converting byte[] to base64 string?
回声-n“aff”| sha1sum 产生正确的输出(echo 默认插入换行符)
echo -n "aff" | sha1sum produce the correct output (echo inserts a newline by default)
你也可以使用这个代码(来自crackstation.net):
you can use this code too(from crackstation.net):
您需要首先对结果进行十六进制编码。 MessageDigest 返回一个“原始”哈希值,而不是人类可读的哈希值。
编辑:
@thejh 提供了应该可以工作的代码的链接。就个人而言,我建议使用 Bouncycastle 或 Apache Commons Codec 来完成这项工作。如果您想做任何其他与加密相关的操作,Bouncycastle 会很好。
You need to hex encode the result first.
MessageDigest
returns a "raw" hash, rather than a human readable one.Edit:
@thejh provided a link to code which should work. Personally, I'd suggest using either Bouncycastle or Apache Commons Codec to do the job. Bouncycastle would be good if you want to do any other crypto-related operations.