From 24d2ecfa33352efc54f432d702b3c5544c80ae54 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sun, 23 Jan 2022 16:17:44 +0100 Subject: [PATCH] generate random password in RandomUtil --- .../pandacube/lib/core/util/RandomUtil.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/RandomUtil.java b/Core/src/main/java/fr/pandacube/lib/core/util/RandomUtil.java index 5a5aa16..7dd5552 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/RandomUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/RandomUtil.java @@ -24,6 +24,10 @@ public class RandomUtil { return (arr == null || arr.isEmpty()) ? null : arr.get(rand.nextInt(arr.size())); } + public static char stringChar(String arr) { + return (arr == null || arr.isEmpty()) ? '\0' : arr.charAt(rand.nextInt(arr.length())); + } + /** * Returns a random value from a set. * @@ -68,5 +72,31 @@ public class RandomUtil { } return frequencies.length - 1; } + + + + + + + public static final String PASSWORD_CHARSET_LATIN_LOWERCASE = "abcdefghijklmnopqrstuvwxyz"; + public static final String PASSWORD_CHARSET_LATIN_UPPERCASE = PASSWORD_CHARSET_LATIN_LOWERCASE.toUpperCase(); + public static final String PASSWORD_CHARSET_DIGIT = "0123456789"; + public static final String PASSWORD_CHARSET_SPECIAL = "@#+*/-;:,.?!='()[]{}&\"\\"; + public static final String PASSWORD_CHARSET_NO_ANBIGUITY = "abcdefghkmnpqrstwxyzACDEFGHKLMNPQRSTWXYZ2345679"; + + public static String randomPassword(int length) { + return randomPassword(length, PASSWORD_CHARSET_NO_ANBIGUITY); + } + + public static String randomPassword(int length, String charset) { + char[] pw = new char[length]; + for (int i = 0; i < length; i++) { + pw[i] = stringChar(charset); + } + return String.valueOf(pw); + } + + + }