Here you can find the source of digest(String s, String algorithm)
Parameter | Description |
---|---|
s | Input String |
algorithm | Algorithm to use. Examples: SHA-1, SHA-256, MD5. |
Parameter | Description |
---|---|
NoSuchAlgorithmException | if algorithm is invalid |
public static String digest(String s, String algorithm) throws NoSuchAlgorithmException
//package com.java2s; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { /**//ww w . ja v a 2 s . c om * Computes digest of given input String. * @param s Input String * @param algorithm Algorithm to use. Examples: SHA-1, SHA-256, MD5. * @return Hex encoded digest * @throws NoSuchAlgorithmException if algorithm is invalid */ public static String digest(String s, String algorithm) throws NoSuchAlgorithmException { int i; MessageDigest md = MessageDigest.getInstance(algorithm); byte[] hash = md.digest(s.getBytes()); StringBuilder sb = new StringBuilder(2 * hash.length); for (i = 0; i < hash.length; i++) { int b = hash[i] & 0xff; if (b < 16) { sb.append('0'); } sb.append(Integer.toHexString(b)); } return sb.toString(); } }