Skip to content

Instantly share code, notes, and snippets.

@darranl
Created May 29, 2013 14:31
Show Gist options
  • Save darranl/5670712 to your computer and use it in GitHub Desktop.
Save darranl/5670712 to your computer and use it in GitHub Desktop.
Digest Code Snippets
private byte[] createHA1(final byte[] userName, final Account account, final MessageDigest digest,
final DigestAlgorithm digestAlgorithm) throws AuthenticationException {
if (plainTextPasswords) {
char[] attribute = (char[]) account.getAttribute(Account.PLAINTEXT_PASSWORD_ATTRIBUTE);
if(attribute == null) {
return null;
}
byte[] password = new String(attribute).getBytes(UTF_8);
try {
digest.update(userName);
digest.update(COLON);
digest.update(realmBytes);
digest.update(COLON);
digest.update(password);
return HexConverter.convertToHexBytes(digest.digest());
} finally {
digest.reset();
}
} else {
byte[] preHashed = (byte[])account.getAttribute(Account.DIGEST_HA1_HASH_ATTRIBUTE_PREFIX + digestAlgorithm.getToken());
if(preHashed == null) {
return null;
}
return HexConverter.convertToHexBytes(preHashed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment