Merge pull request #834 from sameira/master
Adding SHA-256 hashing impl and test classes
This commit is contained in:
commit
dbe099fab3
|
@ -46,6 +46,11 @@
|
||||||
<version>3.3</version>
|
<version>3.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.bouncycastle</groupId>
|
||||||
|
<artifactId>bcprov-jdk15on</artifactId>
|
||||||
|
<version>1.55</version>
|
||||||
|
</dependency>
|
||||||
<!-- web -->
|
<!-- web -->
|
||||||
|
|
||||||
<!-- marshalling -->
|
<!-- marshalling -->
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue