/** * {@inheritDoc} * * @see org.jboss.dna.common.text.TextEncoder#encode(java.lang.String) */ public String encode( String text ) { try { byte[] hash = SecureHash.getHash(algorithm, text.getBytes()); String result = Base64.encodeBytes(hash); return result.length() < maxLength ? result : result.substring(0, maxLength); } catch (NoSuchAlgorithmException e) { return text; } }
/** * Get the hash of the supplied content, using the supplied digest algorithm. * * @param algorithm the hashing function algorithm that should be used * @param file the file containing the content to be hashed; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException if the supplied algorithm could not be found * @throws IllegalArgumentException if the algorithm is null * @throws IOException if there is an error reading the file */ public static byte[] getHash( Algorithm algorithm, File file ) throws NoSuchAlgorithmException, IOException { CheckArg.isNotNull(algorithm, "algorithm"); return getHash(algorithm.digestName(), file); }
/** * Get the hash of the supplied content, using the supplied digest algorithm. * * @param algorithm the hashing function algorithm that should be used * @param content the content to be hashed; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException if the supplied algorithm could not be found * @throws IllegalArgumentException if the algorithm is null */ public static byte[] getHash( Algorithm algorithm, byte[] content ) throws NoSuchAlgorithmException { CheckArg.isNotNull(algorithm, "algorithm"); return getHash(algorithm.digestName(), content); }
/** * Get the hash of the supplied content, using the supplied digest algorithm. * * @param algorithm the hashing function algorithm that should be used * @param stream the stream containing the content to be hashed; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException if the supplied algorithm could not be found * @throws IllegalArgumentException if the algorithm is null * @throws IOException if there is an error reading the stream */ public static byte[] getHash( Algorithm algorithm, InputStream stream ) throws NoSuchAlgorithmException, IOException { CheckArg.isNotNull(algorithm, "algorithm"); return getHash(algorithm.digestName(), stream); }