Java SHA1 SHA1(byte[] convertme)

Here you can find the source of SHA1(byte[] convertme)

Description

SHA

License

Apache License

Declaration

public static String SHA1(byte[] convertme) 

Method Source Code


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

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import java.util.Formatter;

public class Main {
    public static String SHA1(byte[] convertme) {
        MessageDigest md;//from   w  ww  .  j  a v  a  2 s . c  o m
        try {
            md = MessageDigest.getInstance("SHA-1");
            return byteArray2Hex(md.digest(convertme));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String byteArray2Hex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        return formatter.toString();
    }

    public static String toString(Object... args) {
        StringBuilder s = new StringBuilder();
        for (Object arg : args) {
            if (arg != null) {
                s.append(arg);
            }
        }
        return s.toString();
    }

    public static String toString(ByteBuffer buffer) {
        /// Create a StringBuffer so that we can convert the bytes to a String
        StringBuffer response = new StringBuffer();

        // Create a CharSet that knows how to encode and decode standard text (UTF-8)
        Charset charset = Charset.forName("UTF-8");

        // Decode the buffer to a String using the CharSet and append it to our buffer
        response.append(charset.decode(buffer));
        buffer.flip();
        return response.toString();
    }
}

Related

  1. generateSHA1String(String stringToEncode)
  2. sha1(byte[] bytes)
  3. sha1(byte[] bytes)
  4. sha1(byte[] bytes)
  5. SHA1(byte[] bytes)
  6. sha1(byte[] data)
  7. sha1(byte[] data)
  8. sha1(byte[] data)
  9. sha1(byte[] data)