Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WonderBeat/713bd4516f2074da5b0b6d1c16c02a5f to your computer and use it in GitHub Desktop.
Save WonderBeat/713bd4516f2074da5b0b6d1c16c02a5f to your computer and use it in GitHub Desktop.
import com.google.common.base.Preconditions;
interface TokenGenerator {
enum VerificationResult { OK, INVALID }
byte[] generate();
VerificationResult verify(byte[] token);
}
public class CharTokenGenerator implements TokenGenerator {
private final String charset;
private final int length;
private final RNGenerator generator;
private final int checksum;
public CharTokenGenerator(String charset, int length, RNGenerator generator) {
this(charset, length, generator, 0);
}
public CharTokenGenerator(String charset, int length, RNGenerator generator, int checksum) {
Preconditions.checkArgument(charset.length() % 256 == 0);
Preconditions.checkArgument(length > 0);
Preconditions.checkArgument(checksum >= 0);
this.charset = charset;
this.length = length;
this.generator = generator;
this.checksum = checksum;
}
@Override
public byte[] generate() {
//...
}
@Override
public VerificationResult verify(byte[] token) {
//..
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment