HmacSHA256摘要算法2 MACCoder


消息摘要算法分为三类:


MD(Message Digest):消息摘要


SHA(Secure Hash Algorithm):安全散列


MAC(Message Authentication Code):消息认证码


MAC算法


介绍:


HMAC(keyed-Hash Message Authentication Code):含有密钥的散列函数算法


包含了MD和SHA两个系列的消息摘要算法


HMAC只是在原有的MD和SHA算法的基础上添加了密钥。

 


import
java.nio.charset.Charset; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; /** * MAC消息摘要组件 * * */ public class MACCoder { public static byte[] HMAC_SHA(byte[] data1,byte[] data2){ final Charset UTF_8 = Charset.forName("UTF-8"); try { String secret = "0X84NSTAokzW1iWCCLFIzP1JYv1utSUT"; Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(UTF_8), "HmacSHA256"); sha256_HMAC.init(secret_key); sha256_HMAC.update(data1); sha256_HMAC.update(data2); return sha256_HMAC.doFinal(); } catch (Exception e){ e.printStackTrace(); } return null; } public static String getHmacSHA256FingerPrint(String str1,String str2) { final Charset UTF_8 = Charset.forName("UTF-8"); try { String secret = "0X84NSTAokzW1iWCCLFIzP1JYv1utSUT"; Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(UTF_8), "HmacSHA256"); sha256_HMAC.init(secret_key); sha256_HMAC.update(str1.getBytes(UTF_8)); sha256_HMAC.update(str2.getBytes(UTF_8)); byte[] arr= sha256_HMAC.doFinal(); String retval=Base64.encode(arr); return retval; } catch (Exception e){ return null; } } /** * 进行相关的摘要算法的处理展示 * @throws Exception * **/ public static void main(String[] args) throws Exception { /*final Charset UTF_8 = Charset.forName("UTF-8"); byte[] datas1="{ \"type\": \"webhook_event\", \"id\": \"3b8800b1-c416-4e65-aaf2-00b16bbadfc1\", \"created_at\": \"2019-09-30T02:27:41-07:00\", \"trigger\": \"FILE.UPLOADED\", \"webhook\": { \"id\": \"232295281\", \"type\": \"webhook\" }, \"created_by\": { \"type\": \"user\", \"id\": \"8571226476\", \"name\": \"sv ecld-tms-scom-t01\", \"login\": \"sv-ecld-tms-scan-t01@rakuten.com\" }, \"source\": { \"id\": \"532443758714\", \"type\": \"file\", \"file_version\": { \"type\": \"file_version\", \"id\": \"563691410130\", \"sha1\": \"1a9f9f000a2c1fe998510028b9a25da013fde0fd\" }, \"sequence_id\": \"0\", \"etag\": \"0\", \"sha1\": \"1a9f9f000a2c1fe998510028b9a25da013fde0fd\", \"name\": \"0301_JP_20190924205001.csv\", \"description\": \"\", \"size\": 105, \"path_collection\": { \"total_count\": 2, \"entries\": [ { \"type\": \"folder\", \"id\": \"0\", \"sequence_id\": null, \"etag\": null, \"name\": \"All Files\" }, { \"type\": \"folder\", \"id\": \"88535556629\", \"sequence_id\": \"0\", \"etag\": \"0\", \"name\": \"T01\" } ] }, \"created_at\": \"2019-09-30T02:27:41-07:00\", \"modified_at\": \"2019-09-30T02:27:41-07:00\", \"trashed_at\": null, \"purged_at\": null, \"content_created_at\": \"2019-09-30T00:34:07-07:00\", \"content_modified_at\": \"2019-09-30T00:34:07-07:00\", \"created_by\": { \"type\": \"user\", \"id\": \"8571226476\", \"name\": \"sv ecld-tms-scom-t01\", \"login\": \"sv-ecld-tms-scan-t01@rakuten.com\" }, \"modified_by\": { \"type\": \"user\", \"id\": \"8571226476\", \"name\": \"sv ecld-tms-scom-t01\", \"login\": \"sv-ecld-tms-scan-t01@rakuten.com\" }, \"owned_by\": { \"type\": \"user\", \"id\": \"8571226476\", \"name\": \"sv ecld-tms-scom-t01\", \"login\": \"sv-ecld-tms-scan-t01@rakuten.com\" }, \"shared_link\": null, \"parent\": { \"type\": \"folder\", \"id\": \"88613793662\", \"sequence_id\": \"0\", \"etag\": \"0\", \"name\": \"T01\" }, \"item_status\": \"active\" }, \"additional_info\": []}".getBytes(UTF_8); byte[] datas2="2019-12-09T02:27:42-07:00".getBytes(UTF_8); byte[] results=MACCoder.HMAC_SHA(datas1,datas2); String str=Base64.encode(results); System.out.println(str);*/ String str1="{ \"type\": \"webhook_event\", \"id\": \"3b8800b1-c416-4e65-aaf2-00b16bbadfc1\", \"created_at\": \"2019-09-30T02:27:41-07:00\", \"trigger\": \"FILE.UPLOADED\", \"webhook\": { \"id\": \"232295281\", \"type\": \"webhook\" }, \"created_by\": { \"type\": \"user\", \"id\": \"8571226476\", \"name\": \"sv ecld-tms-scom-t01\", \"login\": \"sv-ecld-tms-scan-t01@rakuten.com\" }, \"source\": { \"id\": \"532443758714\", \"type\": \"file\", \"file_version\": { \"type\": \"file_version\", \"id\": \"563691410130\", \"sha1\": \"1a9f9f000a2c1fe998510028b9a25da013fde0fd\" }, \"sequence_id\": \"0\", \"etag\": \"0\", \"sha1\": \"1a9f9f000a2c1fe998510028b9a25da013fde0fd\", \"name\": \"0301_JP_20190924205001.csv\", \"description\": \"\", \"size\": 105, \"path_collection\": { \"total_count\": 2, \"entries\": [ { \"type\": \"folder\", \"id\": \"0\", \"sequence_id\": null, \"etag\": null, \"name\": \"All Files\" }, { \"type\": \"folder\", \"id\": \"88535556629\", \"sequence_id\": \"0\", \"etag\": \"0\", \"name\": \"T01\" } ] }, \"created_at\": \"2019-09-30T02:27:41-07:00\", \"modified_at\": \"2019-09-30T02:27:41-07:00\", \"trashed_at\": null, \"purged_at\": null, \"content_created_at\": \"2019-09-30T00:34:07-07:00\", \"content_modified_at\": \"2019-09-30T00:34:07-07:00\", \"created_by\": { \"type\": \"user\", \"id\": \"8571226476\", \"name\": \"sv ecld-tms-scom-t01\", \"login\": \"sv-ecld-tms-scan-t01@rakuten.com\" }, \"modified_by\": { \"type\": \"user\", \"id\": \"8571226476\", \"name\": \"sv ecld-tms-scom-t01\", \"login\": \"sv-ecld-tms-scan-t01@rakuten.com\" }, \"owned_by\": { \"type\": \"user\", \"id\": \"8571226476\", \"name\": \"sv ecld-tms-scom-t01\", \"login\": \"sv-ecld-tms-scan-t01@rakuten.com\" }, \"shared_link\": null, \"parent\": { \"type\": \"folder\", \"id\": \"88613793662\", \"sequence_id\": \"0\", \"etag\": \"0\", \"name\": \"T01\" }, \"item_status\": \"active\" }, \"additional_info\": []}"; String str2="2019-12-09T02:27:42-07:00"; String str=getHmacSHA256FingerPrint(str1,str2); System.out.println(str); } }

 输出:

9Fck1jx9XL0LbKoJrP80qncEWvsuniI94aWRr1Fvi7I=

posted @ 2019-12-10 16:45  逆火狂飙  阅读(820)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东