code: private static boolean passwordsMatch(String userPassword,
                                            String ldapPassword) {
          Matcher m = LDAP_PASSWORD_PATTERN.matcher(ldapPassword);
  
          boolean match = false;
          if (m.find() && m.groupCount() == 2) {
              // if password is encoded in the LDAP, encode the password before
              // compare
              String encoding = m.group(1);
              String password = m.group(2);
              if (logger.isDebugEnabled()) {
                  logger.debug("Encoding: {}, Password: {}", encoding, password);
              }
  
              MessageDigest digest = null;
              try {
                  digest = MessageDigest.getInstance(encoding.toUpperCase());
              } catch (NoSuchAlgorithmException e) {
                  logger.error("Unsupported Algorithm used: {}", encoding);
                  logger.error(e.getMessage());
                  return false;
              }
  
              byte[] resultBytes = digest.digest(userPassword.getBytes());
              byte[] result = Base64.encodeBytesToBytes(resultBytes);
  
              String pwd = new String(password);
              String ldp = new String(result);
              match = pwd.equals(ldp);
          } else {
              // if passwords are not encoded, just do raw compare
              match = userPassword.equals(ldapPassword);
          }
  
          return match;
      }
docs: Method to compare two passwords. The method attempts to encode the user
  password based on the ldap password encoding extracted from the storage
  format (e.g. {SHA}g0bbl3d3g00ka12@#19/=).
  
  @param userPassword
  the password that the user entered
  @param ldapPassword
  the password from the ldap directory
  @return true if userPassword equals ldapPassword with respect to encoding