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

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

如何使用sha256在Java中散列一些字符串?有没有人知道任何免费的图书馆?

How to hash some string with sha256 in Java ? Does anybody know any free library for this ?

推荐答案

SHA-256不是一个编码 - 它是一个单向哈希。

SHA-256 isn't an "encoding" - it's a one-way hash.

您将基本将字符串转换为字节(例如使用 text.getBytes(UTF-8))然后哈希字节。请注意,哈希的结果也将是任意的二进制数据,如果要在字符串中表示,则应使用base64或hex ... 不要尝试使用 String(byte [],String)构造函数。

You'd basically convert the string into bytes (e.g. using text.getBytes("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.

eg

MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(text.getBytes(StandardCharsets.UTF_8));

更多推荐

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

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

发布评论

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

>www.elefans.com

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