All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.unmz.java.util.security.SHA1Utils Maven / Gradle / Ivy

There is a newer version: 1.0.26
Show newest version
package net.unmz.java.util.security;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

/**
 * Project Name:
 * 功能描述:
 *
 * @author [email protected]
 * @version 1.0
 * @date 2020/3/18 11:41
 * @since JDK 1.8
 */
public class SHA1Utils {

    //Sha1签名
    public static String getSha1(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};

        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes(StandardCharsets.UTF_8));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy