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

com.github.xiaoyuge5201.encryption.Sha1Utils Maven / Gradle / Ivy

There is a newer version: 1.3.5
Show newest version
package com.github.xiaoyuge5201.encryption;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

/**
 * @author xiaoyuge
 */
public class Sha1Utils {
	private  static Logger log = LoggerFactory.getLogger(Sha1Utils.class);

	@Value("${spring.application.name}")
	private static String systemName;

	/**
	 * 签名
	 *
	 * @param parameters 参数
	 * @return 签名
	 */
	public static String signature(SortedMap parameters) {
		StringBuffer content = new StringBuffer();
		//所有参与传参的参数按照accsii排序(升序)
		Set es = parameters.entrySet();
		for (Object o : es) {
			Map.Entry entry = (Map.Entry) o;
			String k = (String) entry.getKey();
			Object v = entry.getValue();
			if (null != v && !"".equals(v)) {
				content.append(k + "=" + v + "&");
			}
		}
		String str = content.toString();
		str = str.substring(0, str.length()-1);
		log.info(str);
		MessageDigest md;
		String tmpStr = null;
		try {
			md = MessageDigest.getInstance("SHA-1");
			// 将三个参数字符串拼接成一个字符串进行sha1加密
			byte[] digest = md.digest(str.getBytes());
			tmpStr = byteToStr(digest);
		} catch (NoSuchAlgorithmException e) {
			log.error("系统名称:" + systemName + ",信息:" + e.toString(), e);
		}
		assert tmpStr != null;
		return tmpStr.toLowerCase();
	}

	/**
	 * SHA1加密
	 * @param str 字符串
	 * @return 加密结果
	 */
	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 (byte byte0 : md) {
				buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
				buf[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(buf);
		} catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * 将字节数组转换为十六进制字符串
	 *
	 * @param byteArray 字节数组
	 * @return 六进制字符串
	 */
	private static String byteToStr(byte[] byteArray) {
		StringBuilder strDigest = new StringBuilder();
		for (byte b : byteArray) {
			strDigest.append(byteToHexStr(b));
		}
		return strDigest.toString();
	}
	/**
	 * 将字节转换为十六进制字符串
	 *
	 * @param mByte 字节
	 * @return 十六进制字符串
	 */
	private static String byteToHexStr(byte mByte) {
		char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		char[] tempArr = new char[2];
		tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
		tempArr[1] = Digit[mByte & 0X0F];
		return new String(tempArr);
	}
	
	public static void main(String[] args){
//		log.info(getSha1("csstsari107"));
//		log.info(getSha1("csstsari"));
//		log.info(getSha1("csstsari"));
//		log.info(getSha1("wechart001oFk6wtzDMBCMb3BvWEX38pnaaHA4easemob2017"));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy