Java SHA1 sha1(String s)

Here you can find the source of sha1(String s)

Description

Computes the SHA1 sum of the given string.

License

Open Source License

Parameter

Parameter Description
s the string

Return

the SHA1 sum

Declaration

public static String sha1(String s) 

Method Source Code


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

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

public class Main {
    /**/* ww  w.j a  v a2 s .c o m*/
     * Computes the SHA1 sum of the given string.
     *
     * @param s the string
     * @return the SHA1 sum
     */
    public static String sha1(String s) {
        try {
            return byteArrayToHexString(MessageDigest.getInstance("SHA").digest(s.getBytes("US-ASCII")));
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException nsae) {
            throw new RuntimeException(nsae);
        }
    }

    /**
     * Convert a byte array into the corresponding string.
     *
     * @param  b the byte array to convert
     * @return the string representation of this byte array
     */
    public static String byteArrayToHexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length * 2);

        for (byte aB : b) {
            int j = (aB & 0xff);

            if (j < 16) {
                sb.append('0');
            }

            sb.append(Integer.toHexString(j));
        }

        return sb.toString();
    }
}

Related

  1. sha1(String s)
  2. sha1(String s)
  3. sha1(String s)
  4. sha1(String s)
  5. sha1(String s)
  6. sha1(String src)
  7. sha1(String src)
  8. sha1(String src)
  9. sha1(String srcStr)