/** * Hashes a char array using the algorithm parametrised in the instance. * * @param newPassword * the new password to hash. * @return the password digest. * @throws NoSuchAlgorithmException * when the digest algorithm is not supported. * @throws IOException * whenever an I/O exception occurs. */ protected String digestAndEncode(char... newPassword) throws NoSuchAlgorithmException, IOException { if (getDigestAlgorithm() != null) { MessageDigest md = MessageDigest.getInstance(getDigestAlgorithm()); md.reset(); md.update(new String(newPassword).getBytes(StandardCharsets.UTF_8.name())); byte[] digest = md.digest(); return getPasswordStorePrefix() + encode(digest); } return new String(newPassword); }
/** * Encodes the password hash based on the hash encoding parameter (either * Base64, Base16). Defaults to Base64. * * @param source * the byte array (hash) to encode. * @return the encoded string. */ protected String encode(byte[] source) { String he = getHashEncoding(); if (BASE64_ENCODING.equalsIgnoreCase(he)) { return Base64.encodeBase64String(source); } if (BASE16_ENCODING.equalsIgnoreCase(he) || HEX_ENCODING.equalsIgnoreCase(he)) { return Hex.encodeHexString(source); } // defaults to Base64 return Base64.encodeBase64String(source); }
/** * {@inheritDoc} */ @Override public boolean execute(IActionHandler actionHandler, Map<String, Object> context) { Map<String, Object> actionParam = getModelConnector(context).getConnectorValue(); String typedPasswd = (String) actionParam.get(PASSWD_TYPED); String retypedPasswd = (String) actionParam.get(PASSWD_RETYPED); if (!ObjectUtils.equals(typedPasswd, retypedPasswd)) { throw new ActionBusinessException("Typed and retyped passwords are different.", "password.typed.retyped.different"); } checkPasswordValidity(typedPasswd, context); UserPrincipal principal = getApplicationSession(context).getPrincipal(); if (changePassword(principal, (String) actionParam.get(PASSWD_CURRENT), typedPasswd)) { setActionParameter(getTranslationProvider(context).getTranslation("password.change.success", getLocale(context)), context); return super.execute(actionHandler, context); } return false; }