From 9303e33f363ffc32cca1c5cce2b53ddff2e5a526 Mon Sep 17 00:00:00 2001 From: firaja Date: Tue, 18 Oct 2022 14:02:33 +0200 Subject: [PATCH] #83: added banner --- pom.xml | 7 ++++ src/main/java/com/password4j/Hash.java | 26 ++------------ src/main/java/com/password4j/Password.java | 9 +++++ .../java/com/password4j/PropertyReader.java | 4 +-- src/main/java/com/password4j/Utils.java | 36 +++++++++++++++++++ src/test/com/password4j/HashTest.java | 3 ++ src/test/com/password4j/PasswordTest.java | 2 +- 7 files changed, 60 insertions(+), 27 deletions(-) diff --git a/pom.xml b/pom.xml index c0754449..9f94ab91 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,13 @@ test + + org.slf4j + slf4j-simple + 2.0.3 + test + + diff --git a/src/main/java/com/password4j/Hash.java b/src/main/java/com/password4j/Hash.java index 04dcbe2d..6d75aabb 100644 --- a/src/main/java/com/password4j/Hash.java +++ b/src/main/java/com/password4j/Hash.java @@ -256,31 +256,9 @@ private static boolean areEquals(CharSequence cs1, CharSequence cs2) } else if (cs1 != null && cs2 != null) { - if (cs1.length() != cs2.length()) - { - return false; - } - else if (cs1 instanceof String && cs2 instanceof String) - { - return cs1.equals(cs2); - } - else - { - int length = cs1.length(); - for(int i = 0; i < length; ++i) - { - if (cs1.charAt(i) != cs2.charAt(i)) - { - return false; - } - } - return true; - } - } - else - { - return false; + return cs1.equals(cs2); } + return false; } @Override diff --git a/src/main/java/com/password4j/Password.java b/src/main/java/com/password4j/Password.java index d0f61e17..3aa57d32 100644 --- a/src/main/java/com/password4j/Password.java +++ b/src/main/java/com/password4j/Password.java @@ -16,6 +16,9 @@ */ package com.password4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * This class provides the two main operations on password: hash and verify. *

@@ -31,6 +34,12 @@ public class Password { + private static final Logger LOGGER = LoggerFactory.getLogger(Password.class); + + static { + Utils.printBanner(); + } + private Password() { // diff --git a/src/main/java/com/password4j/PropertyReader.java b/src/main/java/com/password4j/PropertyReader.java index 526fc38d..d514bd67 100644 --- a/src/main/java/com/password4j/PropertyReader.java +++ b/src/main/java/com/password4j/PropertyReader.java @@ -31,7 +31,7 @@ class PropertyReader private static final Logger LOG = LoggerFactory.getLogger(PropertyReader.class); - private static final String FILE_NAME = "/psw4j.properties"; + private static final String FILE_NAME = "psw4j.properties"; private static final String CONFIGURATION_KEY = "psw4j.configuration"; @@ -118,7 +118,7 @@ static void init() InputStream in; if (customPath == null || customPath.length() == 0) { - in = getResource(FILE_NAME); + in = getResource('/' + FILE_NAME); } else { diff --git a/src/main/java/com/password4j/Utils.java b/src/main/java/com/password4j/Utils.java index 24286559..04c9df3d 100644 --- a/src/main/java/com/password4j/Utils.java +++ b/src/main/java/com/password4j/Utils.java @@ -17,6 +17,7 @@ package com.password4j; + import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; @@ -25,6 +26,7 @@ import java.nio.charset.StandardCharsets; import java.security.*; import java.util.Arrays; +import java.util.List; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -552,4 +554,38 @@ static String randomPrintable(int count) return builder.toString(); } + static void printBanner() + { + if (PropertyReader.readBoolean("global.banner", true)) + { + String pbkdf2Banner; + List pbkd2s = AlgorithmFinder.getAllPBKDF2Variants(); + if (pbkd2s.size() > 0) { + pbkdf2Banner = "✅ PBKDF2-" + String.join("/", pbkd2s).replaceAll("PBKDF2WithHmac", ""); + } + else + { + pbkdf2Banner = "❌ PBKDF2 <-- not supported by " + System.getProperty("java.vm.name"); + } + + String banner ="\n"; + banner += " |\n" + + " | \033[0;1mPassword4j\033[0;0m\n" + + " + \\ .: v1.6.1 :.\n" + + " \\\\.G_.*=.\n" + + " `(H'/.\\| ✅ Argon2\n" + + " .>' (_--. ✅ scrypt\n" + + " _=/d ,^\\ ✅ bcrypt\n" + + " ~~ \\)-'-' " + pbkdf2Banner + "\n" + + " / |\n" + + " ' '"; + banner += "\n"; + banner += " ⭐ If you enjoy Password4j, please star the project at https://github.com/Password4j/password4j\n"; + banner += " \uD83D\uDC1B Report any issue at https://github.com/Password4j/password4j/issues\n"; + + System.out.println(banner); + + } + } + } diff --git a/src/test/com/password4j/HashTest.java b/src/test/com/password4j/HashTest.java index cbaa0628..9c57cc48 100644 --- a/src/test/com/password4j/HashTest.java +++ b/src/test/com/password4j/HashTest.java @@ -95,6 +95,8 @@ public void testSecFunc() String toString2 = hash2.toString(); int hc2 = hash2.hashCode(); String toString3 = hash3.toString(); + Hash hash4 = new Hash(hash3.getHashingFunction(), hash3.getResult(), hash3.getBytes(), hash3.getSalt()); + // THEN Assert.assertNotNull(toString1); @@ -102,6 +104,7 @@ public void testSecFunc() Assert.assertNotEquals(toString1, toString2); Assert.assertNotEquals(toString3, toString2); Assert.assertNotEquals(hc1, hc2); + Assert.assertNotEquals(hash4, hash3); } } diff --git a/src/test/com/password4j/PasswordTest.java b/src/test/com/password4j/PasswordTest.java index b8cc9975..98b98400 100644 --- a/src/test/com/password4j/PasswordTest.java +++ b/src/test/com/password4j/PasswordTest.java @@ -788,7 +788,7 @@ public void real() .addPepper("shared-secret") .with(bcrypt); - System.out.println(verified); + Assert.assertTrue(verified); } }