Runt
2023-02-22 ea6ce17bf3272259295adccbad85583079b5bac0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.auto.lyric.util;
 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
/**
 * Created by Runt (qingingrunt2010@qq.com) on 2022/5/29.
 */
public class SHA1UTIL {
 
    public static String getSHA(String info) {
        byte[] bytesSHA = null;
        try {
 
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
 
            messageDigest.update(info.getBytes());
 
            bytesSHA = messageDigest.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        String strSHA = byteToHex(bytesSHA);
        return strSHA;
    }
 
 
    private static String byteToHex(byte[] bytes) {
        String hs = "";
        String temp;
        for (byte b : bytes) {
            temp = (Integer.toHexString(b & 0XFF));
            if (temp.length() == 1) {
                hs = hs + "0" + temp;
            } else {
                hs = hs + temp;
            }
        }
        return hs;
    }
 
    /**
     * MD5加密之方法二
     * @explain java实现
     * @param str
     *            待加密字符串
     * @return 16进制加密字符串
     */
    public static String MD5(String str) {
        // 加密后的16进制字符串
        String hexStr = "";
        try {
            // 此 MessageDigest 类为应用程序提供信息摘要算法的功能
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            // 转换为MD5码
            byte[] digest = md5.digest(str.getBytes("utf-8"));
            hexStr = byteToHex(digest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hexStr;
    }
 
}