/** * Returns a hash value for the specified text. * * @param text The text value to hash. * * @return The Hash value for the specified test or <code>null</code> if an error occurred. * * @throws UnsupportedOperationException If the MD5 algorithm is not available for that platform. */ public static String hash(String text) throws UnsupportedOperationException { MessageDigest md = getAlgorithm(); md.update(text.getBytes(StandardCharsets.UTF_8), 0, text.length()); byte[] bytes = md.digest(); return toHex(bytes); }
/** * Returns a hash value for the specified file content. * * <p>Implementation note: this method loads the entire file using NIO. * * @param file The file to read * @return The MD5 checksum value as a string. * * @throws IOException If the file does not exist or an error occurred while reading the file. * @throws UnsupportedOperationException If the MD5 algorithm is not available for that platform. */ public static String hash(File file) throws IOException, UnsupportedOperationException { MessageDigest md = getAlgorithm(); FileInputStream fis = new FileInputStream(file); FileChannel in = fis.getChannel(); try { MappedByteBuffer buffer = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size()); md.update(buffer); byte[] bytes = md.digest(); return toHex(bytes); } finally { fis.close(); } }