diff --git a/Password Generator/GeneratePassword.class b/Password Generator/GeneratePassword.class new file mode 100644 index 00000000..19d552c0 Binary files /dev/null and b/Password Generator/GeneratePassword.class differ diff --git a/Password Generator/GeneratePassword.java b/Password Generator/GeneratePassword.java new file mode 100644 index 00000000..aa000a52 --- /dev/null +++ b/Password Generator/GeneratePassword.java @@ -0,0 +1,113 @@ +import java.security.SecureRandom; +import java.util.Collections; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +public class GeneratePassword { + + public static void main(String args[]) { + + String pass = generateSecurePassword(); + + System.out.println("Password generated by Custom Utility Method is:" + pass); + + } + + public static String generateSecurePassword() { + + // generate a string having 2 numbers, 2 special chars, 2 upper case letters, + // and 2 lower case letters + Stream demoPassword = Stream.concat(getRandomNumbers(2), + Stream.concat(getRandomSpecialChars(2), + Stream.concat(getRandomAlphabets(2, true), getRandomAlphabets(2, false)))); + + // create a list of Char that stores all the characters, numbers, and special + // characters + List listOfChar = demoPassword.collect(Collectors.toList()); + + // use shuffle() method of the Collections to shuffle the list elements + Collections.shuffle(listOfChar); + + // generate a random string(secure password) by using list stream() method and + // collect() method + String password = listOfChar.stream() + .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append) + .toString(); + + // return RandomStringGenerator password to the main() method + return password; + } + + // create getRandomSpecialChars() method that returns a Stream of special chars + // of the specified length + public static Stream getRandomSpecialChars(int length) { + + Stream specialCharsStream; + + // create instance of SecureRandom + Random random = new SecureRandom(); + + // use ints() method of random to get IntStream of special chars of the + // specified length + IntStream specialChars = random.ints(length, 33, 45); + specialCharsStream = specialChars.mapToObj(data -> (char) data); + + // return stream to the main() method + return specialCharsStream; + } + + // create getRandomNumbers() method that returns a Stream of numbers of the + // specified length + public static Stream getRandomNumbers(int length) { + + Stream numberStream; + + // create instance of SecureRandom + Random random = new SecureRandom(); + + // use ints() method of random to get IntStream of number of the specified + // length + IntStream numberIntStream = random.ints(length, 48, 57); + numberStream = numberIntStream.mapToObj(data -> (char) data); + + // return number stream to main() method + return numberStream; + } + + // create getRandomAlphabets() method that returns either a stream of upper case + // chars or stream of lower case chars + // of the specified length based on the boolean variable check + public static Stream getRandomAlphabets(int length, boolean check) { + + Stream lowerUpperStream; + + // for lower case stream + if (check == true) { + // create instance of SecureRandom + Random random = new SecureRandom(); + + // use ints() method of random to get IntStream of lower case letters of the + // specified length + IntStream lCaseStream = random.ints(length, 'a', 'z'); + lowerUpperStream = lCaseStream.mapToObj(data -> (char) data); + } + // for upper case stream + else { + // create instance of SecureRandom + Random random = new SecureRandom(); + + // use ints() method of random to get IntStream of upper case letters of the + // specified length + IntStream uCaseStream = random.ints(length, 'A', 'Z'); + lowerUpperStream = uCaseStream.mapToObj(data -> (char) data); + } + + // return lowerUpperStream to main() method + return lowerUpperStream; + + } + +} \ No newline at end of file