BAEL-2501 add a new section in SHA-256 article (#6271)
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.hashing;
|
||||
|
||||
public class DigestAlgorithms {
|
||||
|
||||
public static final String SHA3_256 = "SHA3-256";
|
||||
public static final String SHA_256 = "SHA-256";
|
||||
public static final String KECCAK_256 = "Keccak-256";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.hashing;
|
||||
|
||||
import org.bouncycastle.jcajce.provider.digest.Keccak;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.Security;
|
||||
|
||||
import static com.baeldung.hashing.DigestAlgorithms.KECCAK_256;
|
||||
import static com.baeldung.hashing.SHACommonUtils.bytesToHex;
|
||||
|
||||
public class Keccak256Hashing {
|
||||
|
||||
public static String hashWithJavaMessageDigest(final String originalString) throws NoSuchAlgorithmException {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
final MessageDigest digest = MessageDigest.getInstance(KECCAK_256);
|
||||
final byte[] encodedhash = digest.digest(originalString.getBytes(StandardCharsets.UTF_8));
|
||||
return bytesToHex(encodedhash);
|
||||
}
|
||||
|
||||
public static String hashWithBouncyCastle(final String originalString) {
|
||||
Keccak.Digest256 digest256 = new Keccak.Digest256();
|
||||
byte[] hashbytes = digest256.digest(originalString.getBytes(StandardCharsets.UTF_8));
|
||||
return new String(Hex.encode(hashbytes));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,15 +8,18 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import static com.baeldung.hashing.DigestAlgorithms.SHA_256;
|
||||
import static com.baeldung.hashing.SHACommonUtils.bytesToHex;
|
||||
|
||||
public class SHA256Hashing {
|
||||
|
||||
public static String HashWithJavaMessageDigest(final String originalString) throws NoSuchAlgorithmException {
|
||||
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
final MessageDigest digest = MessageDigest.getInstance(SHA_256);
|
||||
final byte[] encodedhash = digest.digest(originalString.getBytes(StandardCharsets.UTF_8));
|
||||
return bytesToHex(encodedhash);
|
||||
}
|
||||
|
||||
public static String HashWithGuava(final String originalString) {
|
||||
public static String hashWithGuava(final String originalString) {
|
||||
final String sha256hex = Hashing.sha256().hashString(originalString, StandardCharsets.UTF_8).toString();
|
||||
return sha256hex;
|
||||
}
|
||||
@@ -27,20 +30,10 @@ public class SHA256Hashing {
|
||||
}
|
||||
|
||||
public static String HashWithBouncyCastle(final String originalString) throws NoSuchAlgorithmException {
|
||||
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
final MessageDigest digest = MessageDigest.getInstance(SHA_256);
|
||||
final byte[] hash = digest.digest(originalString.getBytes(StandardCharsets.UTF_8));
|
||||
final String sha256hex = new String(Hex.encode(hash));
|
||||
return sha256hex;
|
||||
}
|
||||
|
||||
private static String bytesToHex(byte[] hash) {
|
||||
StringBuffer hexString = new StringBuffer();
|
||||
for (int i = 0; i < hash.length; i++) {
|
||||
String hex = Integer.toHexString(0xff & hash[i]);
|
||||
if (hex.length() == 1)
|
||||
hexString.append('0');
|
||||
hexString.append(hex);
|
||||
}
|
||||
return hexString.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.hashing;
|
||||
|
||||
import com.google.common.hash.Hashing;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.bouncycastle.crypto.digests.SHA3Digest;
|
||||
import org.bouncycastle.jcajce.provider.digest.SHA3;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.Security;
|
||||
|
||||
import static com.baeldung.hashing.DigestAlgorithms.SHA3_256;
|
||||
import static com.baeldung.hashing.SHACommonUtils.bytesToHex;
|
||||
|
||||
public class SHA3Hashing {
|
||||
|
||||
/* works with JDK9+ only */
|
||||
public static String hashWithJavaMessageDigestJDK9(final String originalString) throws NoSuchAlgorithmException {
|
||||
final MessageDigest digest = MessageDigest.getInstance(SHA3_256);
|
||||
final byte[] hashbytes = digest.digest(originalString.getBytes(StandardCharsets.UTF_8));
|
||||
return bytesToHex(hashbytes);
|
||||
}
|
||||
|
||||
public static String hashWithJavaMessageDigest(final String originalString) throws NoSuchAlgorithmException {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
final MessageDigest digest = MessageDigest.getInstance(SHA3_256);
|
||||
final byte[] hashbytes = digest.digest(originalString.getBytes(StandardCharsets.UTF_8));
|
||||
return bytesToHex(hashbytes);
|
||||
}
|
||||
|
||||
/* works with JDK9+ only */
|
||||
public static String hashWithApacheCommonsJDK9(final String originalString) {
|
||||
return new DigestUtils(SHA3_256).digestAsHex(originalString);
|
||||
}
|
||||
|
||||
public static String hashWithBouncyCastle(final String originalString) {
|
||||
SHA3.Digest256 digest256 = new SHA3.Digest256();
|
||||
byte[] hashbytes = digest256.digest(originalString.getBytes(StandardCharsets.UTF_8));
|
||||
return new String(Hex.encode(hashbytes));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.hashing;
|
||||
|
||||
class SHACommonUtils {
|
||||
|
||||
public static String bytesToHex(byte[] hash) {
|
||||
StringBuffer hexString = new StringBuffer();
|
||||
for (byte h : hash) {
|
||||
String hex = Integer.toHexString(0xff & h);
|
||||
if (hex.length() == 1)
|
||||
hexString.append('0');
|
||||
hexString.append(hex);
|
||||
}
|
||||
return hexString.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user