@Test public void generateRandomSecretKey() throws Exception { final Set<String> keys = IntStream.range(0, 10000).mapToObj(i -> { return SecureTokenCodec.generateUrlSafeKey(); }).collect(toSet()); assertEquals(10000, keys.size()); assertEquals(10000, keys.stream() .map(SecureTokenCodec::convertUrlSafeKeyToBytes) .filter(bytes -> bytes.length == 16) .count()); }
public static String generateUrlSafeKey() { final byte[] bytes = new byte[16]; LazyHolder.SECURE_RANDOM.nextBytes(bytes); return Encryptor.URL_SAFE_BASE64_ENCODER.encodeToString(bytes); }
SecureTokenCodec { public static String generateUrlSafeKey() { final byte[] bytes = new byte[16]; LazyHolder.SECURE_RANDOM.nextBytes(bytes); return Encryptor.URL_SAFE_BASE64_ENCODER.encodeToString(bytes); } }
SecureTokenCodec { public static String generateUrlSafeKey() { final byte[] bytes = new byte[16]; LazyHolder.SECURE_RANDOM.nextBytes(bytes); return Encryptor.URL_SAFE_BASE64_ENCODER.encodeToString(bytes); } private  SecureTokenCodec(final byte[] macKey, final byte[] secretKey); }
SecureTokenCodec { public static String generateUrlSafeKey() { final byte[] bytes = new byte[16]; LazyHolder.SECURE_RANDOM.nextBytes(bytes); return Encryptor.URL_SAFE_BASE64_ENCODER.encodeToString(bytes); } private  SecureTokenCodec(final byte[] macKey, final byte[] secretKey); static byte[] convertUrlSafeKeyToBytes(String generatedUrlSafeKey); static SecureTokenCodec create(final byte[] macKey, final byte[] secretKeyIn16Bytes); static String generateUrlSafeKey(); static void main(String[] args); String encode(final long expireTime, final List<byte[]> fields); String encodeString(long expireTime, String... fields); List<byte[]> tryDecode(final String targetToken); List<String> tryDecodeAsString(String targetToken); boolean validateToken(final String targetToken, final List<byte[]> knownFields); }
SecureTokenCodec { public static String generateUrlSafeKey() { final byte[] bytes = new byte[16]; LazyHolder.SECURE_RANDOM.nextBytes(bytes); return Encryptor.URL_SAFE_BASE64_ENCODER.encodeToString(bytes); } private  SecureTokenCodec(final byte[] macKey, final byte[] secretKey); static byte[] convertUrlSafeKeyToBytes(String generatedUrlSafeKey); static SecureTokenCodec create(final byte[] macKey, final byte[] secretKeyIn16Bytes); static String generateUrlSafeKey(); static void main(String[] args); String encode(final long expireTime, final List<byte[]> fields); String encodeString(long expireTime, String... fields); List<byte[]> tryDecode(final String targetToken); List<String> tryDecodeAsString(String targetToken); boolean validateToken(final String targetToken, final List<byte[]> knownFields);  }