protected String passwordDigest(Long userId, String clearText, boolean salt) { if (clearText == null) { throw new ApiUsageException("Value for digesting may not be null"); } byte[] bytes = clearText.getBytes(encoding); // If salting is activated, prepend the salt. if (userId != null && salt) { byte[] saltedBytes = ByteBuffer.allocate(8).putLong(userId).array(); byte[] newValue = new byte[saltedBytes.length+bytes.length]; System.arraycopy(saltedBytes, 0, newValue, 0, saltedBytes.length); System.arraycopy(bytes, 0, newValue, saltedBytes.length, bytes.length); bytes = newValue; } String hashedText = null; ChecksumProviderFactory cpf = new ChecksumProviderFactoryImpl(); try { bytes = cpf.getProvider(ChecksumType.MD5).putBytes(bytes) .checksumAsBytes(); bytes = Base64.encodeBase64(bytes); hashedText = new String(bytes); } catch (Exception e) { log.error("Could not hash password", e); } if (hashedText == null) { throw new InternalException("Failed to obtain digest."); } return hashedText; }