コード例 #1
0
  /**
   * Digest password using the algorithm especificied and convert the result to a corresponding hex
   * string. If exception, the plain credentials string is returned
   *
   * @param credentials Password or other credentials to use in authenticating this username
   * @param algorithm Algorithm used to do th digest
   */
  public static final String Digest(String credentials, String algorithm) {

    try {
      // Obtain a new message digest with "digest" encryption
      MessageDigest md = (MessageDigest) MessageDigest.getInstance(algorithm).clone();
      // encode the credentials
      md.update(credentials.getBytes());
      // Digest the credentials and return as hexadecimal
      return (HexUtils.convert(md.digest()));
    } catch (Exception ex) {
      ex.printStackTrace();
      return credentials;
    }
  }
コード例 #2
0
  /**
   * Digest the password using the specified algorithm and convert the result to a corresponding
   * hexadecimal string. If exception, the plain credentials string is returned.
   *
   * <p><strong>IMPLEMENTATION NOTE</strong> - This implementation is synchronized because it reuses
   * the MessageDigest instance. This should be faster than cloning the instance on every request.
   *
   * @param credentials Password or other credentials to use in authenticating this username
   */
  protected String digest(String credentials) {

    // If no MessageDigest instance is specified, return unchanged
    if (hasMessageDigest() == false) return (credentials);

    // Digest the user credentials and return as hexadecimal
    synchronized (this) {
      try {
        md.reset();
        md.update(credentials.getBytes());
        return (HexUtils.convert(md.digest()));
      } catch (Exception e) {
        log(sm.getString("realmBase.digest"), e);
        return (credentials);
      }
    }
  }