/** * 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 the digest * @param encoding Character encoding of the string to digest */ public final static String Digest(String credentials, String algorithm, String encoding) { try { // Obtain a new message digest with "digest" encryption MessageDigest md = (MessageDigest) MessageDigest.getInstance(algorithm).clone(); // encode the credentials // Should use the digestEncoding, but that's not a static field if (encoding == null) { md.update(credentials.getBytes()); } else { md.update(credentials.getBytes(encoding)); } // Digest the credentials and return as hexadecimal return (HexUtils.convert(md.digest())); } catch(Exception ex) { log.error(ex); return credentials; } }
/** * Digest the password using the specified algorithm and * convert the result to a corresponding hexadecimal string. * If exception, the plain credentials string is returned. * * <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); } } }
/** * 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 final static 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; } }
/** * Digest the password using the specified algorithm and * convert the result to a corresponding hexadecimal string. * If exception, the plain credentials string is returned. * * @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(); byte[] bytes = null; if(getDigestEncoding() == null) { bytes = credentials.getBytes(); } else { try { bytes = credentials.getBytes(getDigestEncoding()); } catch (UnsupportedEncodingException uee) { log.error("Illegal digestEncoding: " + getDigestEncoding(), uee); throw new IllegalArgumentException(uee.getMessage()); } } md.update(bytes); return (HexUtils.convert(md.digest())); } catch (Exception e) { log.error(sm.getString("realmBase.digest"), e); return (credentials); } } }