Last active
June 20, 2019 16:12
-
-
Save clementi/2ab36ef74c1064ddb16f0ff7e8529dba to your computer and use it in GitHub Desktop.
Secure Token Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.jeffreypratt.edu.security; | |
public class App { | |
public static void main(String[] args) { | |
var count = 1; | |
if (args.length > 0) { | |
count = Integer.parseInt(args[0]); | |
} | |
try { | |
var provider = new SecureTokenProvider(TokenAlphabet.FULL, 20); | |
provider.stream() | |
.limit(count) | |
.forEach(System.out::println); | |
} catch (CouldNotCreateProviderException e) { | |
System.out.println(e.getMessage()); | |
System.exit(1); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.jeffreypratt.edu.security; | |
public class CouldNotCreateProviderException extends Exception { | |
public CouldNotCreateProviderException(Throwable t) { | |
super(String.format("Could not create provider: %s", t.getMessage()), t); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.jeffreypratt.edu.security; | |
import java.security.SecureRandom; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.stream.Stream; | |
public class SecureTokenProvider { | |
private final String alphabet; | |
private final SecureRandom random; | |
private final int length; | |
private static final int DEFAULT_LENGTH = 16; | |
private static final String DEFAULT_ALPHABET = TokenAlphabet.ASCII_LETTERS; | |
public SecureTokenProvider() throws CouldNotCreateProviderException { | |
this(DEFAULT_ALPHABET); | |
} | |
public SecureTokenProvider(String alphabet) throws CouldNotCreateProviderException { | |
this(alphabet, DEFAULT_LENGTH); | |
} | |
public SecureTokenProvider(int length) throws CouldNotCreateProviderException { | |
this(DEFAULT_ALPHABET, length); | |
} | |
public SecureTokenProvider(String alphabet, int length) throws CouldNotCreateProviderException { | |
try { | |
this.alphabet = alphabet; | |
this.length = length; | |
this.random = SecureRandom.getInstanceStrong(); | |
} catch (NoSuchAlgorithmException e) { | |
throw new CouldNotCreateProviderException(e); | |
} | |
} | |
SecureTokenProvider(String alphabet, SecureRandom random, int length) { | |
this.alphabet = alphabet; | |
this.random = random; | |
this.length = length; | |
} | |
public String nextToken(int length) { | |
var builder = new StringBuilder(length); | |
for (int i = 0; i < length; i++) { | |
int index = random.nextInt(alphabet.length()); | |
builder.append(alphabet.charAt(index)); | |
} | |
return builder.toString(); | |
} | |
public Stream<String> stream() { | |
return Stream.generate(() -> nextToken(length)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.jeffreypratt.edu.security; | |
public class TokenAlphabet { | |
public static final String ASCII_LOWERCASE = "abcdefghijklmnopqrstuvwxyz"; | |
public static final String ASCII_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
public static final String ASCII_LETTERS = ASCII_UPPERCASE + ASCII_LOWERCASE; | |
public static final String DIGITS = "0123456789"; | |
public static final String SYMBOLS = "`~!@#$%^&*()_+-={}|[]\\:\";',./<>?"; | |
public static final String FULL = ASCII_UPPERCASE + ASCII_LOWERCASE + DIGITS + SYMBOLS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment