master
袁野 6 months ago
parent 81d4a7f90a
commit 97fd3d1ae6
  1. 42
      service/src/main/java/com/hfkj/common/utils/WxUtils.java

@ -413,49 +413,7 @@ public class WxUtils {
} }
} }
/**
* 用SHA1算法生成安全签名
* @param data 数据
* @return 安全签名
* @throws Exception
*/
public static String getSHA1(final Map<String, Object> data) throws Exception
{
try {
Set<String> keySet = data.keySet();
String[] keyArray = keySet.toArray(new String[keySet.size()]);
Arrays.sort(keyArray);
StringBuilder sb = new StringBuilder();
for (String k : keyArray) {
if (k.equals(WXPayConstants.FIELD_SIGN) || k.equals("apiKey")) {
continue;
}
if (data.get(k) != null) // 参数值为空,则不参与签名
{
sb.append(k).append("=").append(data.get(k)).append("&");
}
}
String str = sb.substring(0 , sb.length()-1);
// 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) {
e.printStackTrace();
throw new Exception(String.format("Invalid sign_type: %s"));
}
}
} }

Loading…
Cancel
Save