From 8e2ad9323311958a4e8161c011b6187980bdb104 Mon Sep 17 00:00:00 2001 From: Sameera Date: Mon, 14 Nov 2016 22:31:50 +0530 Subject: [PATCH] Adding SHA-256 hashing impl and test classes --- core-java/pom.xml | 5 ++ .../com/baeldung/hashing/SHA256Hashing.java | 51 +++++++++++++++++++ .../baeldung/hashing/SHA256HashingTest.java | 37 ++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/hashing/SHA256Hashing.java create mode 100644 core-java/src/test/java/com/baeldung/hashing/SHA256HashingTest.java diff --git a/core-java/pom.xml b/core-java/pom.xml index 8b93e238eb..17d9c2ce64 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -46,6 +46,11 @@ 3.3 + + org.bouncycastle + bcprov-jdk15on + 1.55 + diff --git a/core-java/src/main/java/com/baeldung/hashing/SHA256Hashing.java b/core-java/src/main/java/com/baeldung/hashing/SHA256Hashing.java new file mode 100644 index 0000000000..9c8fc86e7a --- /dev/null +++ b/core-java/src/main/java/com/baeldung/hashing/SHA256Hashing.java @@ -0,0 +1,51 @@ +package com.baeldung.hashing; + + +import com.google.common.hash.Hashing; +import org.apache.commons.codec.digest.DigestUtils; +import org.bouncycastle.util.encoders.Hex; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class SHA256Hashing { + + public static String HashWithJavaMessageDigest(final String originalString) + throws NoSuchAlgorithmException { + 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) { + final String sha256hex = Hashing.sha256().hashString( + originalString, StandardCharsets.UTF_8).toString(); + return sha256hex; + } + + public static String HashWithApacheCommons(final String originalString) { + final String sha256hex = DigestUtils.sha256Hex(originalString); + return sha256hex; + } + + public static String HashWithBouncyCastle(final String originalString) + throws NoSuchAlgorithmException { + 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(); + } +} diff --git a/core-java/src/test/java/com/baeldung/hashing/SHA256HashingTest.java b/core-java/src/test/java/com/baeldung/hashing/SHA256HashingTest.java new file mode 100644 index 0000000000..dc496d589b --- /dev/null +++ b/core-java/src/test/java/com/baeldung/hashing/SHA256HashingTest.java @@ -0,0 +1,37 @@ +package com.baeldung.hashing; + +import org.junit.Test; + +import static org.junit.Assert.*; + + +public class SHA256HashingTest { + + private static String originalValue = "abc123"; + private static String hashedValue = + "6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090"; + + @Test + public void testHashWithJavaMessageDigest() throws Exception { + final String currentHashedValue = SHA256Hashing.HashWithJavaMessageDigest(originalValue); + assertEquals(currentHashedValue, hashedValue); + } + + @Test + public void testHashWithGuava() throws Exception { + final String currentHashedValue = SHA256Hashing.HashWithApacheCommons(originalValue); + assertEquals(currentHashedValue, hashedValue); + } + + @Test + public void testHashWithApacheCommans() throws Exception { + final String currentHashedValue = SHA256Hashing.HashWithGuava(originalValue); + assertEquals(currentHashedValue, hashedValue); + } + + @Test + public void testHashWithBouncyCastle() throws Exception { + final String currentHashedValue = SHA256Hashing.HashWithBouncyCastle(originalValue); + assertEquals(currentHashedValue, hashedValue); + } +} \ No newline at end of file