Is there a way to shorten a java.security.MessageDigest generated value?
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开发者_JS百科 = 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?
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.
精彩评论