如何在 Java 中使用 sha256 散列一些字符串?有人知道有什么免费的图书馆吗?
How can I hash some string with sha256 in Java? Does anybody know of any free library for this?
推荐答案SHA-256 不是编码"- 这是一种单向哈希.
SHA-256 isn't an "encoding" - it's a one-way hash.
您基本上会将字符串转换为字节(例如使用 text.getBytes(StandardCharsets.UTF_8)),然后对字节进行哈希处理.请注意,散列的结果也是任意的二进制数据,如果你想用字符串表示它,你应该使用 base64 或十六进制...不要尝试使用 String(byte[], String) 构造函数.
You'd basically convert the string into bytes (e.g. using text.getBytes(StandardCharsets.UTF_8)) and then hash the bytes. Note that the result of the hash would also be arbitrary binary data, and if you want to represent that in a string, you should use base64 or hex... don't try to use the String(byte[], String) constructor.
例如
MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(text.getBytes(StandardCharsets.UTF_8));更多推荐
如何在 Java 中使用 sha256 散列一些字符串?
发布评论