private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static final int HEX_RIGHT_SHIFT_COEFFICIENT = 4;
private static final int HEX_HIGH_BITS_BITWISE_FLAG = 0x0f;
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPasswordEncoderMD5.class);
private final String encodingAlgorithm="MD5";
private String characterEncoding="UTF-8";
@Override
public String encode(final String password) {
if (password == null) {
return null;
}
if (StringUtils.isBlank(this.encodingAlgorithm)) {
LOGGER.warn("No encoding algorithm is defined. Password cannot be encoded; Returning null");
return null;
}
try {
final MessageDigest messageDigest = MessageDigest.getInstance(this.encodingAlgorithm);
final String encodingCharToUse = StringUtils.isNotBlank(this.characterEncoding)
? this.characterEncoding : Charset.defaultCharset().name();
LOGGER.warn("Using {} as the character encoding algorithm to update the digest", encodingCharToUse);
messageDigest.update(password.getBytes(encodingCharToUse));
/**
* Takes the raw bytes from the digest and formats them correct.
*
* @param bytes the raw bytes from the digest.
* @return the formatted bytes.
*/
private static String getFormattedText(final byte[] bytes) {
final StringBuilder buf = new StringBuilder(bytes.length * 2);