JAVA-623: Moved 3 articles from core-java-security to security-2

This commit is contained in:
sampadawagde
2020-03-28 16:48:04 +05:30
parent df9734f792
commit dd72c90ae4
17 changed files with 2 additions and 641 deletions

View File

@@ -1,22 +0,0 @@
package com.baeldung.hashing;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class Keccak256HashingUnitTest {
private static String originalValue = "abc123";
private static String hashedValue = "719accc61a9cc126830e5906f9d672d06eab6f8597287095a2c55a8b775e7016";
@Test public void testHashWithJavaMessageDigest() throws Exception {
final String currentHashedValue = Keccak256Hashing.hashWithJavaMessageDigest(originalValue);
assertEquals(hashedValue, currentHashedValue);
}
@Test public void testHashWithBouncyCastle() {
final String currentHashedValue = Keccak256Hashing.hashWithBouncyCastle(originalValue);
assertEquals(hashedValue, currentHashedValue);
}
}

View File

@@ -1,35 +0,0 @@
package com.baeldung.hashing;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SHA256HashingUnitTest {
private static String originalValue = "abc123";
private static String hashedValue = "6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090";
@Test
public void testHashWithJavaMessageDigest() throws Exception {
final String currentHashedValue = SHA256Hashing.HashWithJavaMessageDigest(originalValue);
assertEquals(hashedValue, currentHashedValue);
}
@Test
public void testHashWithGuava() throws Exception {
final String currentHashedValue = SHA256Hashing.hashWithGuava(originalValue);
assertEquals(hashedValue, currentHashedValue);
}
@Test
public void testHashWithApacheCommans() throws Exception {
final String currentHashedValue = SHA256Hashing.HashWithApacheCommons(originalValue);
assertEquals(hashedValue, currentHashedValue);
}
@Test
public void testHashWithBouncyCastle() throws Exception {
final String currentHashedValue = SHA256Hashing.HashWithBouncyCastle(originalValue);
assertEquals(hashedValue, currentHashedValue);
}
}

View File

@@ -1,38 +0,0 @@
package com.baeldung.hashing;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SHA3HashingUnitTest {
private static String originalValue = "abc123";
private static String hashedValue = "f58fa3df820114f56e1544354379820cff464c9c41cb3ca0ad0b0843c9bb67ee";
/* works with JDK9+ only */
//@Test
public void testHashWithJavaMessageDigestJDK9() throws Exception {
final String currentHashedValue = SHA3Hashing.hashWithJavaMessageDigestJDK9(originalValue);
assertEquals(hashedValue, currentHashedValue);
}
@Test
public void testHashWithJavaMessageDigest() throws Exception {
final String currentHashedValue = SHA3Hashing.hashWithJavaMessageDigest(originalValue);
assertEquals(hashedValue, currentHashedValue);
}
/* works with JDK9+ only */
//@Test
public void testHashWithApacheCommonsJDK9() {
final String currentHashedValue = SHA3Hashing.hashWithApacheCommonsJDK9(originalValue);
assertEquals(hashedValue, currentHashedValue);
}
@Test
public void testHashWithBouncyCastle() {
final String currentHashedValue = SHA3Hashing.hashWithBouncyCastle(originalValue);
assertEquals(hashedValue, currentHashedValue);
}
}

View File

@@ -1,75 +0,0 @@
package com.baeldung.java.md5;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
public class JavaMD5UnitTest {
String filename = "src/test/resources/test_md5.txt";
String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3";
String hash = "35454B055CC325EA1AF2126E27707052";
String password = "ILoveJava";
@Test
public void givenPassword_whenHashing_thenVerifying() throws NoSuchAlgorithmException {
String hash = "35454B055CC325EA1AF2126E27707052";
String password = "ILoveJava";
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte[] digest = md.digest();
String myHash = DatatypeConverter.printHexBinary(digest).toUpperCase();
assertThat(myHash.equals(hash)).isTrue();
}
@Test
public void givenFile_generatingChecksum_thenVerifying() throws NoSuchAlgorithmException, IOException {
String filename = "src/test/resources/test_md5.txt";
String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3";
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(Files.readAllBytes(Paths.get(filename)));
byte[] digest = md.digest();
String myChecksum = DatatypeConverter.printHexBinary(digest).toUpperCase();
assertThat(myChecksum.equals(checksum)).isTrue();
}
@Test
public void givenPassword_whenHashingUsingCommons_thenVerifying() {
String hash = "35454B055CC325EA1AF2126E27707052";
String password = "ILoveJava";
String md5Hex = DigestUtils.md5Hex(password).toUpperCase();
assertThat(md5Hex.equals(hash)).isTrue();
}
@Test
public void givenFile_whenChecksumUsingGuava_thenVerifying() throws IOException {
String filename = "src/test/resources/test_md5.txt";
String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3";
HashCode hash = com.google.common.io.Files.hash(new File(filename), Hashing.md5());
String myChecksum = hash.toString().toUpperCase();
assertThat(myChecksum.equals(checksum)).isTrue();
}
}

View File

@@ -1,41 +0,0 @@
package com.baeldung.passwordhashing;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class PBKDF2HasherUnitTest {
private PBKDF2Hasher mPBKDF2Hasher;
@Before
public void setUp() throws Exception {
mPBKDF2Hasher = new PBKDF2Hasher();
}
@Test
public void givenCorrectMessageAndHash_whenAuthenticated_checkAuthenticationSucceeds() throws Exception {
String message1 = "password123";
String hash1 = mPBKDF2Hasher.hash(message1.toCharArray());
assertTrue(mPBKDF2Hasher.checkPassword(message1.toCharArray(), hash1));
}
@Test
public void givenWrongMessage_whenAuthenticated_checkAuthenticationFails() throws Exception {
String message1 = "password123";
String hash1 = mPBKDF2Hasher.hash(message1.toCharArray());
String wrongPasswordAttempt = "IamWrong";
assertFalse(mPBKDF2Hasher.checkPassword(wrongPasswordAttempt.toCharArray(), hash1));
}
}

View File

@@ -1,70 +0,0 @@
package com.baeldung.passwordhashing;
import org.junit.Before;
import org.junit.Test;
import java.security.SecureRandom;
import static org.junit.Assert.*;
/**
* Created by PhysicsSam on 06-Sep-18.
*/
public class SHA512HasherUnitTest {
private SHA512Hasher hasher;
private SecureRandom secureRandom;
@Before
public void setUp() throws Exception {
hasher = new SHA512Hasher();
secureRandom = new SecureRandom();
}
@Test
public void givenSamePasswordAndSalt_whenHashed_checkResultingHashesAreEqual() throws Exception {
byte[] salt = new byte[16];
secureRandom.nextBytes(salt);
String hash1 = hasher.hash("password", salt);
String hash2 = hasher.hash("password", salt);
assertEquals(hash1, hash2);
}
@Test
public void givenSamePasswordAndDifferentSalt_whenHashed_checkResultingHashesNotEqual() throws Exception {
byte[] salt = new byte[16];
secureRandom.nextBytes(salt);
String hash1 = hasher.hash("password", salt);
//generate a second salt
byte[] secondSalt = new byte[16];
String hash2 = hasher.hash("password", secondSalt);
assertNotEquals(hash1, hash2);
}
@Test
public void givenPredefinedHash_whenCorrectAttemptGiven_checkAuthenticationSucceeds() throws Exception {
byte[] salt = new byte[16];
secureRandom.nextBytes(salt);
String originalHash = hasher.hash("password123", salt);
assertTrue(hasher.checkPassword(originalHash, "password123", salt));
}
@Test
public void givenPredefinedHash_whenIncorrectAttemptGiven_checkAuthenticationFails() throws Exception {
byte[] salt = new byte[16];
secureRandom.nextBytes(salt);
String originalHash = hasher.hash("password123", salt);
assertFalse(hasher.checkPassword(originalHash, "password124", salt));
}
}