minor formatting work
This commit is contained in:
parent
7194d8b84a
commit
f354f9e305
@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.hashing;
|
package com.baeldung.hashing;
|
||||||
|
|
||||||
|
|
||||||
import com.google.common.hash.Hashing;
|
import com.google.common.hash.Hashing;
|
||||||
import org.apache.commons.codec.digest.DigestUtils;
|
import org.apache.commons.codec.digest.DigestUtils;
|
||||||
import org.bouncycastle.util.encoders.Hex;
|
import org.bouncycastle.util.encoders.Hex;
|
||||||
@ -11,17 +10,14 @@ import java.security.NoSuchAlgorithmException;
|
|||||||
|
|
||||||
public class SHA256Hashing {
|
public class SHA256Hashing {
|
||||||
|
|
||||||
public static String HashWithJavaMessageDigest(final String originalString)
|
public static String HashWithJavaMessageDigest(final String originalString) throws NoSuchAlgorithmException {
|
||||||
throws NoSuchAlgorithmException {
|
|
||||||
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||||
final byte[] encodedhash = digest.digest(
|
final byte[] encodedhash = digest.digest(originalString.getBytes(StandardCharsets.UTF_8));
|
||||||
originalString.getBytes(StandardCharsets.UTF_8));
|
|
||||||
return bytesToHex(encodedhash);
|
return bytesToHex(encodedhash);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String HashWithGuava(final String originalString) {
|
public static String HashWithGuava(final String originalString) {
|
||||||
final String sha256hex = Hashing.sha256().hashString(
|
final String sha256hex = Hashing.sha256().hashString(originalString, StandardCharsets.UTF_8).toString();
|
||||||
originalString, StandardCharsets.UTF_8).toString();
|
|
||||||
return sha256hex;
|
return sha256hex;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,11 +26,9 @@ public class SHA256Hashing {
|
|||||||
return sha256hex;
|
return sha256hex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String HashWithBouncyCastle(final String originalString)
|
public static String HashWithBouncyCastle(final String originalString) throws NoSuchAlgorithmException {
|
||||||
throws NoSuchAlgorithmException {
|
|
||||||
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||||
final byte[] hash = digest.digest(
|
final byte[] hash = digest.digest(originalString.getBytes(StandardCharsets.UTF_8));
|
||||||
originalString.getBytes(StandardCharsets.UTF_8));
|
|
||||||
final String sha256hex = new String(Hex.encode(hash));
|
final String sha256hex = new String(Hex.encode(hash));
|
||||||
return sha256hex;
|
return sha256hex;
|
||||||
}
|
}
|
||||||
@ -43,7 +37,8 @@ public class SHA256Hashing {
|
|||||||
StringBuffer hexString = new StringBuffer();
|
StringBuffer hexString = new StringBuffer();
|
||||||
for (int i = 0; i < hash.length; i++) {
|
for (int i = 0; i < hash.length; i++) {
|
||||||
String hex = Integer.toHexString(0xff & hash[i]);
|
String hex = Integer.toHexString(0xff & hash[i]);
|
||||||
if(hex.length() == 1) hexString.append('0');
|
if (hex.length() == 1)
|
||||||
|
hexString.append('0');
|
||||||
hexString.append(hex);
|
hexString.append(hex);
|
||||||
}
|
}
|
||||||
return hexString.toString();
|
return hexString.toString();
|
||||||
|
@ -12,7 +12,7 @@ public class GenericsTest {
|
|||||||
// testing the generic method with Integer
|
// testing the generic method with Integer
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
|
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
|
||||||
Integer[] intArray = {1, 2, 3, 4, 5};
|
Integer[] intArray = { 1, 2, 3, 4, 5 };
|
||||||
List<Integer> list = Generics.fromArrayToList(intArray);
|
List<Integer> list = Generics.fromArrayToList(intArray);
|
||||||
|
|
||||||
assertThat(list, hasItems(intArray));
|
assertThat(list, hasItems(intArray));
|
||||||
@ -21,7 +21,7 @@ public class GenericsTest {
|
|||||||
// testing the generic method with String
|
// testing the generic method with String
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
|
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
|
||||||
String[] stringArray = {"hello1", "hello2", "hello3", "hello4", "hello5"};
|
String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" };
|
||||||
List<String> list = Generics.fromArrayToList(stringArray);
|
List<String> list = Generics.fromArrayToList(stringArray);
|
||||||
|
|
||||||
assertThat(list, hasItems(stringArray));
|
assertThat(list, hasItems(stringArray));
|
||||||
@ -32,7 +32,7 @@ public class GenericsTest {
|
|||||||
// extend Number it will fail to compile
|
// extend Number it will fail to compile
|
||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
|
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
|
||||||
Integer[] intArray = {1, 2, 3, 4, 5};
|
Integer[] intArray = { 1, 2, 3, 4, 5 };
|
||||||
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
|
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
|
||||||
|
|
||||||
assertThat(list, hasItems(intArray));
|
assertThat(list, hasItems(intArray));
|
||||||
|
@ -4,12 +4,10 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
|
||||||
public class SHA256HashingTest {
|
public class SHA256HashingTest {
|
||||||
|
|
||||||
private static String originalValue = "abc123";
|
private static String originalValue = "abc123";
|
||||||
private static String hashedValue =
|
private static String hashedValue = "6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090";
|
||||||
"6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090";
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHashWithJavaMessageDigest() throws Exception {
|
public void testHashWithJavaMessageDigest() throws Exception {
|
||||||
|
@ -119,8 +119,7 @@ public class StringConversionTest {
|
|||||||
int afterConvCalendarDay = 03;
|
int afterConvCalendarDay = 03;
|
||||||
Month afterConvCalendarMonth = Month.DECEMBER;
|
Month afterConvCalendarMonth = Month.DECEMBER;
|
||||||
int afterConvCalendarYear = 2007;
|
int afterConvCalendarYear = 2007;
|
||||||
LocalDateTime afterConvDate
|
LocalDateTime afterConvDate = new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str);
|
||||||
= new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str);
|
|
||||||
|
|
||||||
assertEquals(afterConvDate.getDayOfMonth(), afterConvCalendarDay);
|
assertEquals(afterConvDate.getDayOfMonth(), afterConvCalendarDay);
|
||||||
assertEquals(afterConvDate.getMonth(), afterConvCalendarMonth);
|
assertEquals(afterConvDate.getMonth(), afterConvCalendarMonth);
|
||||||
|
@ -66,6 +66,7 @@ public class AsyncEchoServer {
|
|||||||
AsyncEchoServer server = new AsyncEchoServer();
|
AsyncEchoServer server = new AsyncEchoServer();
|
||||||
server.runServer();
|
server.runServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Process start() throws IOException, InterruptedException {
|
public static Process start() throws IOException, InterruptedException {
|
||||||
String javaHome = System.getProperty("java.home");
|
String javaHome = System.getProperty("java.home");
|
||||||
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
|
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
|
||||||
|
@ -81,7 +81,6 @@ public class AsyncEchoServer2 {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new AsyncEchoServer2();
|
new AsyncEchoServer2();
|
||||||
}
|
}
|
||||||
|
@ -82,8 +82,7 @@ public class AsyncFileTest {
|
|||||||
public void givenPathAndContent_whenWritesToFileWithHandler_thenCorrect() throws IOException {
|
public void givenPathAndContent_whenWritesToFileWithHandler_thenCorrect() throws IOException {
|
||||||
String fileName = UUID.randomUUID().toString();
|
String fileName = UUID.randomUUID().toString();
|
||||||
Path path = Paths.get(fileName);
|
Path path = Paths.get(fileName);
|
||||||
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE,StandardOpenOption.DELETE_ON_CLOSE);
|
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE);
|
||||||
|
|
||||||
|
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||||
buffer.put("hello world".getBytes());
|
buffer.put("hello world".getBytes());
|
||||||
|
@ -20,7 +20,6 @@ public class JavaFileUnitTest {
|
|||||||
|
|
||||||
private static final String TEMP_DIR = "src/test/resources/temp" + UUID.randomUUID().toString();
|
private static final String TEMP_DIR = "src/test/resources/temp" + UUID.randomUUID().toString();
|
||||||
|
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setup() throws IOException {
|
public static void setup() throws IOException {
|
||||||
Files.createDirectory(Paths.get(TEMP_DIR));
|
Files.createDirectory(Paths.get(TEMP_DIR));
|
||||||
|
@ -49,10 +49,7 @@ public class Employee implements Comparable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new StringBuffer()
|
return new StringBuffer().append("(").append(getName()).append(getAge()).append(",").append(getSalary()).append(")").toString();
|
||||||
.append("(").append(getName())
|
|
||||||
.append(getAge()).append(",")
|
|
||||||
.append(getSalary()).append(")").toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user