@Test(timeout = 10000) public void testGenerate() throws Exception { SimplePasswordResetTokenGenerator generator = new SimplePasswordResetTokenGenerator(); Date date = currentTime(); String generated = generator.generate("userName", "ip", date); assertThat(generated, not(isEmptyOrNullString())); long time = date.getTime(); int conflicted = 0; for (int i = 0; i < 50000; i++) { Date newDate = new Date(time); String token = generator.generate("userName", "ip", newDate); assertThat(token, not(isEmptyOrNullString())); if (generated.equalsIgnoreCase(token)) { conflicted++; } } assertThat(conflicted, lessThan(10)); }
@Override public String generate(String userName, String ip, Date date) { hasLength(userName); notNull(date); String randomString = RandomStringUtils.randomAlphanumeric(32); try { String s = randomString + userName + date.getTime() + ip; byte[] bytes = s.getBytes("UTF-8"); byte[] generated = digest.digest(bytes); return new String(hex(generated)); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("No such encoding", e); } }
SimplePasswordResetTokenGenerator implements PasswordResetTokenGenerator { @Override public String generate(String userName, String ip, Date date) { hasLength(userName); notNull(date); String randomString = RandomStringUtils.randomAlphanumeric(32); try { String s = randomString + userName + date.getTime() + ip; byte[] bytes = s.getBytes("UTF-8"); byte[] generated = digest.digest(bytes); return new String(hex(generated)); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("No such encoding", e); } } }
SimplePasswordResetTokenGenerator implements PasswordResetTokenGenerator { @Override public String generate(String userName, String ip, Date date) { hasLength(userName); notNull(date); String randomString = RandomStringUtils.randomAlphanumeric(32); try { String s = randomString + userName + date.getTime() + ip; byte[] bytes = s.getBytes("UTF-8"); byte[] generated = digest.digest(bytes); return new String(hex(generated)); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("No such encoding", e); } } SimplePasswordResetTokenGenerator(); }
SimplePasswordResetTokenGenerator implements PasswordResetTokenGenerator { @Override public String generate(String userName, String ip, Date date) { hasLength(userName); notNull(date); String randomString = RandomStringUtils.randomAlphanumeric(32); try { String s = randomString + userName + date.getTime() + ip; byte[] bytes = s.getBytes("UTF-8"); byte[] generated = digest.digest(bytes); return new String(hex(generated)); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("No such encoding", e); } } SimplePasswordResetTokenGenerator(); @Override String generate(String userName, String ip, Date date); }
SimplePasswordResetTokenGenerator implements PasswordResetTokenGenerator { @Override public String generate(String userName, String ip, Date date) { hasLength(userName); notNull(date); String randomString = RandomStringUtils.randomAlphanumeric(32); try { String s = randomString + userName + date.getTime() + ip; byte[] bytes = s.getBytes("UTF-8"); byte[] generated = digest.digest(bytes); return new String(hex(generated)); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("No such encoding", e); } } SimplePasswordResetTokenGenerator(); @Override String generate(String userName, String ip, Date date);  }