Advertisement
koki2000

SHA-256 hash in Java

May 11th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.45 KB | None | 0 0
  1.         MessageDigest md;
  2.         try
  3.         {
  4.             md = MessageDigest.getInstance("SHA-256");
  5.             byte[] hashInBytes = md.digest(password.getBytes(StandardCharsets.UTF_8));
  6.  
  7.             // bytes to hex
  8.             StringBuilder sb = new StringBuilder();
  9.             for (byte b : hashInBytes) {
  10.                 sb.append(String.format("%02x", b));
  11.             }
  12.             System.out.println(sb.substring(0, 16));
  13.         } catch (NoSuchAlgorithmException e)
  14.         {
  15.             // TODO Auto-generated catch block
  16.             e.printStackTrace();
  17.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement