这里直接就用 java.security.MessageDigest 这个类进行加密
直接上代码
public class ShaUtils {
public static String shaEncode(String inStr) throws Exception {
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA");//获取SHA实例,环境不支持会报错
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
byte[] byteArray = inStr.getBytes("UTF-8");
byte[] md5Bytes = sha.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
public static void main(String args[]) throws Exception {
String str = "www.yangguangdream.com";
System.out.println("source:" + str);//source:www.yangguangdream.com
System.out.println("sha:" + shaEncode(str));//sha:64a687b0966d568fc893646492743ec268d9ecde
}
}使用
String shaStr = Sha1Utils.shaEncode(token);

微信扫码查看本文
发表评论