有没有办法缩短 java.security.MessageDigest 生成的值? [英] Is there a way to shorten a java.security.MessageDigest generated value?

查看:45
本文介绍了有没有办法缩短 java.security.MessageDigest 生成的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用此 Java 代码生成消息摘要(用于应用中的安全功能):

If I generate a message digest (for a security feature in my app) using this Java code:

java.security.MessageDigest saltDigest = MessageDigest.getInstance("SHA-256");
saltDigest.update(UUID.randomUUID().toString().getBytes("UTF-8"));
String digest = String.valueOf(Hex.encode(saltDigest.digest()));

我最终得到一个非常长的字符串,如下所示:

I end up with a really long string like the following:

29bcf49cbd57bbc41e601b399a93218ef99c6e36bae3598b5a5a64ac66d9c254

传递 URL 不是最好的事情!

Not the nicest thing to pass on a URL!

有没有办法缩短这个时间?

Is there a way to shorten this?

推荐答案

嗯,这是 SHA-256 哈希的预期大小,对吗?你总是可以的

Well, that's the expected size of a SHA-256 hash, right? You could always do

String sha256 = yourSha256Calculation();
sha256.substring(0,10);

获得更短的字符串,但这不会是 SHA-256.你真正想要达到什么目标?

To get a shorter string, but that would not be SHA-256. What are you really trying to achieve?

SHA-256 不是最短的哈希,看看 http://www.fileformat.info/tool/hash.htm?text=hello 进行比较.

SHA-256 is not the shortest hash out there, look at http://www.fileformat.info/tool/hash.htm?text=hello for a comparison.

这篇关于有没有办法缩短 java.security.MessageDigest 生成的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆