Android Hex String Create hexDigest(String input)

Here you can find the source of hexDigest(String input)

Description

First encrypts with SHA1 and then spits the result out as a hex string

Declaration

public static String hexDigest(String input)
        throws NoSuchAlgorithmException, UnsupportedEncodingException 

Method Source Code

//package com.java2s;
import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

public class Main {
    /** First encrypts with SHA1 and then spits the result out as a hex string*/
    public static String hexDigest(String input)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        return convertToHex(dataDigest(input));
    }//from  ww  w .  j  av a2  s .c  o  m

    private static String convertToHex(byte[] in) {
        StringBuilder builder = new StringBuilder(in.length * 2);

        Formatter formatter = new Formatter(builder);
        for (byte inByte : in)
            formatter.format("%02x", inByte);

        formatter.close();

        return formatter.toString();
    }

    private static byte[] dataDigest(String in)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        if (in == null)
            return null;

        MessageDigest md = MessageDigest.getInstance("SHA-1");
        return md.digest(in.getBytes("UTF8"));
    }
}

Related

  1. pathExtension(String fileName)
  2. hexStringToBytes(String hexString)
  3. appendHex(StringBuffer stringbuffer, byte byte0)
  4. toHexString(char achar0)
  5. decodeHexStr(final String str)
  6. hexToBt64(String hex)
  7. hexToBytes(String hex)
  8. convertToHex(byte[] data)
  9. convertToHex(byte[] data)