生产签名进行 Base64 编码
第1步:構造簽名內容,簽名內容為 head 所有內容和 body 所有內容拼接起來。考慮到XML節點的無序性,在拼裝時按照節點名稱字母進行正序排列(即從a到z),舉例:首字母相同的,按照處于第2位的進行排序;以此類推。且組成的簽名內容中不能包含空格、換行等字符。
第2步:生產簽名值sign,使用 HMAC-SHA1 加密算法,將第1步中的內容進行加密。
第3步:對加密后的內容進行 Base64 編碼。
?public static String genHMAC(String data) {
?? ??? ?String key = "9fKmCAhF6FGIXSE8ZG46gsVBA5bIHuvl";
?? ??? ?byte[] result = null;
?? ??? ?try {
?? ??? ??? ?SecretKeySpec signinKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
?? ??? ??? ?Mac mac = Mac.getInstance("HmacSHA1");
?? ??? ??? ?mac.init(signinKey);
?? ??? ??? ?byte[] rawHmac = mac.doFinal(data.getBytes());
?? ??? ??? ?result = Base64.encodeBase64(rawHmac);
?? ??? ?} catch (NoSuchAlgorithmException e) {
?? ??? ??? ?System.err.println(e.getMessage());
?? ??? ?} catch (InvalidKeyException e) {
?? ??? ??? ?System.err.println(e.getMessage());
?? ??? ?}
?? ??? ?if (null != result) {
?? ??? ??? ?return new String(result);
?? ??? ?} else {
?? ??? ??? ?return null;
?? ??? ?}
?? ?}
總結
以上是生活随笔為你收集整理的生产签名进行 Base64 编码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 容联云通讯的学习笔记一
- 下一篇: js处理微信amr格式音频