@Test public void testGeneratePassword() { SecureRandom secureRandom = mock(SecureRandom.class); PasswordUtil passwordUtil = new PasswordUtil(secureRandom); int value = 8675309; doAnswer(invocation -> { byte[] bytes = (byte[]) invocation.getArguments()[0]; assertEquals(32, bytes.length); Arrays.fill(bytes, (byte) 0); byte[] val = ByteBuffer.allocate(Long.BYTES).putLong(value).array(); System.arraycopy(val, 0, bytes, bytes.length - val.length, val.length); return null; }).when(secureRandom).nextBytes(any(byte[].class)); byte[] expectedBytes = new byte[32]; byte[] numberBytes = BigInteger.valueOf(Integer.valueOf(value).longValue()).toByteArray(); System.arraycopy(numberBytes, 0, expectedBytes, expectedBytes.length - numberBytes.length, numberBytes.length); String expected = Base64.getEncoder().encodeToString(expectedBytes).split("=")[0]; String actual = passwordUtil.generatePassword(); assertEquals(expected, actual); }
public String generatePassword() { byte[] bytes = new byte[32]; secureRandom.nextBytes(bytes); String string = Base64.getEncoder().encodeToString(bytes); while (string.endsWith("=")) { string = string.substring(0, string.length() - 1); } return string; }
PasswordUtil { public String generatePassword() { byte[] bytes = new byte[32]; secureRandom.nextBytes(bytes); String string = Base64.getEncoder().encodeToString(bytes); while (string.endsWith("=")) { string = string.substring(0, string.length() - 1); } return string; } }
PasswordUtil { public String generatePassword() { byte[] bytes = new byte[32]; secureRandom.nextBytes(bytes); String string = Base64.getEncoder().encodeToString(bytes); while (string.endsWith("=")) { string = string.substring(0, string.length() - 1); } return string; } PasswordUtil();  PasswordUtil(SecureRandom secureRandom); }
PasswordUtil { public String generatePassword() { byte[] bytes = new byte[32]; secureRandom.nextBytes(bytes); String string = Base64.getEncoder().encodeToString(bytes); while (string.endsWith("=")) { string = string.substring(0, string.length() - 1); } return string; } PasswordUtil();  PasswordUtil(SecureRandom secureRandom); String generatePassword(); Supplier<String> passwordSupplier(); static Supplier<String> passwordSupplier(String password); static Supplier<String> passwordSupplier(String exhaustedMessage, String[] passwords); }
PasswordUtil { public String generatePassword() { byte[] bytes = new byte[32]; secureRandom.nextBytes(bytes); String string = Base64.getEncoder().encodeToString(bytes); while (string.endsWith("=")) { string = string.substring(0, string.length() - 1); } return string; } PasswordUtil();  PasswordUtil(SecureRandom secureRandom); String generatePassword(); Supplier<String> passwordSupplier(); static Supplier<String> passwordSupplier(String password); static Supplier<String> passwordSupplier(String exhaustedMessage, String[] passwords);  }