Skip to content

Instantly share code, notes, and snippets.

@jonaslindmark
Last active August 29, 2015 14:20
Show Gist options
  • Save jonaslindmark/6617ccd427b07aac5aa5 to your computer and use it in GitHub Desktop.
Save jonaslindmark/6617ccd427b07aac5aa5 to your computer and use it in GitHub Desktop.
String getRandomTwoLetterHexDigest(UUID timebased) {
checkArgument(timebased.version() == 1);
long timestamp = UUIDs.unixTimestamp(timebased);
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.putLong(timestamp);
// could add clocksequence as well
byte[] digest = DigestUtils.getSha256Digest().digest(buffer.array());
return String.valueOf(Hex.encodeHex(digest)).substring(0, 2);
}
// Examples timebased = UUID.fromString("8afafee0e29f11e49f85bbf692fcdb71") -> "bb"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment