Is there a way to shorten a java.security.MessageDigest generated value?

ぃ、小莉子 提交于 2021-02-18 08:19:24

问题


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

Not the nicest thing to pass on a URL!

Is there a way to shorten this?


回答1:


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

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

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

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



来源:https://stackoverflow.com/questions/7557173/is-there-a-way-to-shorten-a-java-security-messagedigest-generated-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!