11package com .baeldung .hashing ;
22
3-
43import com .google .common .hash .Hashing ;
54import org .apache .commons .codec .digest .DigestUtils ;
65import org .bouncycastle .util .encoders .Hex ;
1110
1211public class SHA256Hashing {
1312
14- public static String HashWithJavaMessageDigest (final String originalString )
15- throws NoSuchAlgorithmException {
13+ public static String HashWithJavaMessageDigest (final String originalString ) throws NoSuchAlgorithmException {
1614 final MessageDigest digest = MessageDigest .getInstance ("SHA-256" );
17- final byte [] encodedhash = digest .digest (
18- originalString .getBytes (StandardCharsets .UTF_8 ));
15+ final byte [] encodedhash = digest .digest (originalString .getBytes (StandardCharsets .UTF_8 ));
1916 return bytesToHex (encodedhash );
2017 }
2118
2219 public static String HashWithGuava (final String originalString ) {
23- final String sha256hex = Hashing .sha256 ().hashString (
24- originalString , StandardCharsets .UTF_8 ).toString ();
20+ final String sha256hex = Hashing .sha256 ().hashString (originalString , StandardCharsets .UTF_8 ).toString ();
2521 return sha256hex ;
2622 }
2723
@@ -30,11 +26,9 @@ public static String HashWithApacheCommons(final String originalString) {
3026 return sha256hex ;
3127 }
3228
33- public static String HashWithBouncyCastle (final String originalString )
34- throws NoSuchAlgorithmException {
29+ public static String HashWithBouncyCastle (final String originalString ) throws NoSuchAlgorithmException {
3530 final MessageDigest digest = MessageDigest .getInstance ("SHA-256" );
36- final byte [] hash = digest .digest (
37- originalString .getBytes (StandardCharsets .UTF_8 ));
31+ final byte [] hash = digest .digest (originalString .getBytes (StandardCharsets .UTF_8 ));
3832 final String sha256hex = new String (Hex .encode (hash ));
3933 return sha256hex ;
4034 }
@@ -43,7 +37,8 @@ private static String bytesToHex(byte[] hash) {
4337 StringBuffer hexString = new StringBuffer ();
4438 for (int i = 0 ; i < hash .length ; i ++) {
4539 String hex = Integer .toHexString (0xff & hash [i ]);
46- if (hex .length () == 1 ) hexString .append ('0' );
40+ if (hex .length () == 1 )
41+ hexString .append ('0' );
4742 hexString .append (hex );
4843 }
4944 return hexString .toString ();
0 commit comments