Java Digest digest(String aValue)

Here you can find the source of digest(String aValue)

Description

digest

License

Apache License

Declaration

public static String digest(String aValue) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static String encodingCharset = "UTF-8";

    public static String digest(String aValue) {
        aValue = aValue.trim();//w  ww.  j  av  a 2 s  . com
        byte[] value;
        try {
            value = aValue.getBytes(encodingCharset);
        } catch (UnsupportedEncodingException e) {
            value = aValue.getBytes();
        }
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
        return toHex(md.digest(value));
    }

    public static String toHex(byte[] input) {
        if (input == null) {
            return null;
        }
        StringBuffer output = new StringBuffer(input.length * 2);
        for (int i = 0; i < input.length; i++) {
            int current = input[i] & 0xFF;
            if (current < 16) {
                output.append("0");
            }
            output.append(Integer.toString(current, 16));
        }
        return output.toString();
    }
}

Related

  1. digest(Serializable object)
  2. digest(String alg, byte[] plainByte)
  3. digest(String algorithm, byte[] bytes)
  4. digest(String algorithm, byte[] data)
  5. digest(String algorithm, String data)
  6. digest(String base)
  7. digest(String digest, byte[] data)
  8. digest(String ha1, String ha2, String nonce)
  9. digest(String input, Charset charset)