Java SHA1 sha1(byte[] input)

Here you can find the source of sha1(byte[] input)

Description

Generate SHA-1 as hex string (all lower-case).

License

Open Source License

Parameter

Parameter Description
input Input as bytes.

Return

Hex string.

Declaration

public static String sha1(byte[] input) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

    /**//from   w ww.ja v  a 2  s . c  om
     * Generate SHA-1 as hex string (all lower-case).
     * 
     * @param input
     *            Input as bytes.
     * @return Hex string.
     */
    public static String sha1(byte[] input) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        md.update(input);
        byte[] digest = md.digest();
        return toHexString(digest);
    }

    /**
     * Convert bytes to hex string (all lower-case).
     * 
     * @param b
     *            Input bytes.
     * @return Hex string.
     */
    public static String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length << 2);
        for (byte x : b) {
            int hi = (x & 0xf0) >> 4;
            int lo = x & 0x0f;
            sb.append(HEX_CHARS[hi]);
            sb.append(HEX_CHARS[lo]);
        }
        return sb.toString().trim();
    }
}

Related

  1. sha1(byte[] data)
  2. sha1(byte[] data)
  3. sha1(byte[] data)
  4. SHA1(byte[] input)
  5. sha1(byte[] input)
  6. sha1(byte[] input)
  7. sha1(byte[] message)
  8. SHA1(byte[] src)
  9. SHA1(ByteBuffer buf, int offset, int size)