Java SHA1 digestSHA1(String data)

Here you can find the source of digestSHA1(String data)

Description

Digests the data with SHA-1 and returns the raw unencoded bytes.

License

Apache License

Declaration

public static String digestSHA1(String data) 

Method Source Code


//package com.java2s;
//Licensed under the Apache License, Version 2.0 (the "License");

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

public class Main {
    /**/* w  ww  . j  a  v a2 s  . c  om*/
     * "SHA-1"
     */
    public static final String SHA1 = "SHA-1";
    /**
     * The hex chars as bytes.
     */
    public static final byte[] HEXADECIMAL = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61,
            0x62, 0x63, 0x64, 0x65, 0x66 };

    /**
     * Digests the {@code data} with SHA-1 and returns the raw unencoded bytes.
     */
    public static String digestSHA1(String data) {
        return getDigestedValue(SHA1, data.getBytes());
    }

    /**
     * Digests the {@code data} with SHA-1 and the specified {@code charset} and 
     * returns the raw unencoded bytes.
     */
    public static String digestSHA1(String data, String charset) {
        try {
            return getDigestedValue(SHA1, data, charset);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Digests the {@code data} with the specified {@code type} and {@code charset}.
     */
    public static String getDigestedValue(String type, String data, String charset)
            throws UnsupportedEncodingException {
        return getDigestedValue(type, data.getBytes(charset), charset);
    }

    /**
     * Digests the {@code data} with the specified {@code type}.
     */
    public static String getDigestedValue(String type, String data) {
        return getDigestedValue(type, data.getBytes());
    }

    static String getDigestedValue(String type, byte[] data, String charset) {
        try {
            MessageDigest digest = MessageDigest.getInstance(type);
            return getHexString(digest.digest(data), charset);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    static String getDigestedValue(String type, byte[] data) {
        try {
            MessageDigest digest = MessageDigest.getInstance(type);
            return getHexString(digest.digest(data));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    static String getHexString(byte[] data) {
        return new String(getHexBytes(data));
    }

    /**
     * Returns the bytes {@code data} in hex form as string.
     */
    public static String getHexString(byte[] data, String charset) throws UnsupportedEncodingException {
        return new String(getHexBytes(data), charset);
    }

    /**
     * Returns the bytes {@code data} in hex form.
     */
    public static byte[] getHexBytes(byte[] data) {

        byte[] out = new byte[data.length * 2];
        for (int i = 0, y = 0; i < data.length; i++) {
            int l = (data[i] & 0xf0) >> 4;
            int r = data[i] & 0x0f;
            out[y++] = HEXADECIMAL[l];
            out[y++] = HEXADECIMAL[r];
        }
        return out;
    }
}

Related

  1. computeSHA1(ByteBuffer convertme, int offset, int len)
  2. computeSha1(ReadableByteChannel channel)
  3. computeSha1AsHexString(String strToHash)
  4. computeSHA1Hash(byte[] data)
  5. digestSHA1(byte[] data)
  6. digestSHA1(String login, String pass)
  7. digestSHA1(String text)
  8. digestSha1Hex(String source)
  9. doSHA1(byte[] buf)