Java Digest digest(String s, String algorithm)

Here you can find the source of digest(String s, String algorithm)

Description

Computes digest of given input String.

License

Open Source License

Parameter

Parameter Description
s Input String
algorithm Algorithm to use. Examples: SHA-1, SHA-256, MD5.

Exception

Parameter Description
NoSuchAlgorithmException if algorithm is invalid

Return

Hex encoded digest

Declaration

public static String digest(String s, String algorithm) throws NoSuchAlgorithmException 

Method Source Code


//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();
    }
}

Related

  1. digest(String password)
  2. digest(String plain, String algorithm)
  3. digest(String planeText)
  4. digest(String provider, File file, int radix)
  5. digest(String raw, String algorithm)
  6. digest(String source)
  7. digest(String source, byte[] salt)
  8. digest(String source, String algorythm)
  9. digest(String strSource)