如何在 Java 中使用 sha256 散列一些字符串?

编程入门 行业动态 更新时间:2024-10-20 04:10:40
本文介绍了如何在 Java 中使用 sha256 散列一些字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在 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 散列一些字符串?

本文发布于:2023-10-27 11:13:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何在   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!