Java SHA1 sha1Hash(String s)

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

Description

sha Hash

License

Apache License

Declaration

public static String sha1Hash(String s) 

Method Source Code


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

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

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

    public static String sha1Hash(String s) {
        if (s == null) {
            return null;
        }//from  ww  w.j ava2s. com
        final MessageDigest messageDigest;
        try {
            messageDigest = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException(e);
        }
        final byte[] digest = messageDigest.digest(s.getBytes(Charset.forName("UTF-8")));
        return bytesToHex(digest);
    }

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
}

Related

  1. sha1Hash(InputStream in)
  2. sha1Hash(String content)
  3. sha1Hash(String input)
  4. sha1Hash(String input)
  5. sha1hash(String key)
  6. sha1Hash(String source)
  7. sha1Hash(String text)
  8. sha1Hash(String tohash)
  9. sha1Hash(String toHash)