Java

  1. public static String bytesToHex(byte[] hash) {
  2. StringBuffer hexString = new StringBuffer();
  3. for (byte h : hash) {
  4. String hex = Integer.toHexString(0xff & h);
  5. if (hex.length() == 1)
  6. hexString.append('0');
  7. hexString.append(hex);
  8. }
  9. return hexString.toString();
  10. }
  11. @Test
  12. public void test() throws NoSuchAlgorithmException, UnsupportedEncodingException {
  13. MessageDigest salt = MessageDigest.getInstance("SHA-256");
  14. salt.update(UUID.randomUUID().toString().getBytes("UTF-8"));
  15. String digest = bytesToHex(salt.digest());
  16. System.out.println(digest);
  17. }