Java SHA1 sha1ByString(String str)

Here you can find the source of sha1ByString(String str)

Description

SHA1 digest

License

Apache License

Parameter

Parameter Description
str a parameter

Declaration

public static String sha1ByString(String str) 

Method Source Code


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

import java.security.MessageDigest;

public class Main {
    /**/*from  w ww  .ja va2  s  .c  o  m*/
     *  SHA1 digest
     * 
     * @param str
     * @return
     */
    public static String sha1ByString(String str) {
        try {
            if (isBlank(str)) {
                return null;
            }
            MessageDigest md = java.security.MessageDigest.getInstance("SHA-1");
            md.update(str.getBytes("UTF-8"));
            byte tmp[] = md.digest();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < tmp.length; i++) {
                sb.append(String.format("%02x", tmp[i]));
            }
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * check string is empty
     * 
     * <pre>
     * StringUtil.isBlank(null)      = true
     * StringUtil.isBlank("")        = true
     * StringUtil.isBlank(" ")       = true
     * StringUtil.isBlank("bob")     = false
     * StringUtil.isBlank("  bob  ") = false
     * </pre>
     * 
     * @param str
     * 
     * @return
     */
    public static boolean isBlank(String str) {
        int length;

        if ((str == null) || ((length = str.length()) == 0)) {
            return true;
        }

        for (int i = 0; i < length; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. sha1(String value)
  2. sha12String(MessageDigest messageDigest)
  3. sha1_b64(final String text)
  4. SHA1_HEX(byte[] bytes)
  5. sha1AsBytes(String input)
  6. SHA1Checksum(String filename)
  7. sha1Digest(byte[] bytes)
  8. sha1Digest(byte[] bytes)
  9. sha1Digest(final InputStream data)