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

com.github.xiaoyuge5201.qywx.WxSHA1 Maven / Gradle / Ivy

There is a newer version: 1.3.5
Show newest version
/**
 * 对公众平台发送给公众账号的消息加解密示例代码.
 *
 * @copyright Copyright (c) 1998-2014 Tencent Inc.
 */

// ------------------------------------------------------------------------

package com.github.xiaoyuge5201.qywx;

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

import java.security.MessageDigest;
import java.util.Arrays;

/**
 * SHA1 class
 * 

* 计算公众平台的消息签名接口. */ public class WxSHA1 { private static Logger log = LoggerFactory.getLogger(WxSHA1.class); @Value("${spring.application.name}") private static String systemName; /** * 用SHA1算法生成安全签名 * * @param token 票据 * @param timestamp 时间戳 * @param nonce 随机字符串 * @param encrypt 密文 * @return 安全签名 */ public static String getSHA1(String token, String timestamp, String nonce, String encrypt) { try { String[] array = new String[]{token, timestamp, nonce, encrypt}; StringBuffer sb = new StringBuffer(); // 字符串排序 Arrays.sort(array); for (int i = 0; i < 4; i++) { sb.append(array[i]); } String str = sb.toString(); // SHA1签名生成 MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(str.getBytes()); byte[] digest = md.digest(); StringBuffer hexstr = new StringBuffer(); String shaHex = ""; for (int i = 0; i < digest.length; i++) { shaHex = Integer.toHexString(digest[i] & 0xFF); if (shaHex.length() < 2) { hexstr.append(0); } hexstr.append(shaHex); } return hexstr.toString(); } catch (Exception e) { log.error("系统名称:" + systemName + ",信息:" + e.toString(), e); return null; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy