Here you can find the source of sha1ByString(String str)
Parameter | Description |
---|---|
str | a parameter |
public static String sha1ByString(String str)
//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; } }