问题
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