From d1f26741bddf6f512d62c0100d42c52be8d37e76 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期六, 06 二月 2021 15:35:40 +0800 Subject: [PATCH] 工具类优化 --- utils/src/main/java/org/yeshi/utils/encrypt/HmacUtils.java | 202 +++++++++++++++++++++++++------------------------- 1 files changed, 101 insertions(+), 101 deletions(-) diff --git a/utils/src/main/java/org/yeshi/utils/encrypt/HmacUtils.java b/utils/src/main/java/org/yeshi/utils/encrypt/HmacUtils.java index 9e613d7..266ac1e 100644 --- a/utils/src/main/java/org/yeshi/utils/encrypt/HmacUtils.java +++ b/utils/src/main/java/org/yeshi/utils/encrypt/HmacUtils.java @@ -1,101 +1,101 @@ -package org.yeshi.utils.encrypt; - -import java.io.IOException; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import javax.crypto.Mac; -import javax.crypto.SecretKey; -import javax.crypto.spec.SecretKeySpec; - -/** - * hmac鍏敤鏂规硶绫� - * - * @author weihui.tang - * - */ -public class HmacUtils { - public static final String CHARSET_UTF8 = "UTF-8"; - - public static final String KEY_MAC = "HmacMD5"; - - private static final String HEXSTR = "0123456789ABCDEF"; - - private static String[] binaryArray = { "0000", "0001", "0010", "0011", - "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", - "1100", "1101", "1110", "1111" }; - - - - /** - * hmac-md5绛惧悕 - * - * @param data - * @param secret - * @return - * @throws IOException - * @throws NoSuchAlgorithmException - * @throws InvalidKeyException - */ - public static byte[] encryptHMAC(String data, String secret) - throws IOException, NoSuchAlgorithmException, InvalidKeyException { - byte[] bytes = null; - SecretKey secretKey = new SecretKeySpec(secret.getBytes(CHARSET_UTF8), - KEY_MAC); - Mac mac = Mac.getInstance(secretKey.getAlgorithm()); - mac.init(secretKey); - bytes = mac.doFinal(data.getBytes(CHARSET_UTF8)); - return bytes; - } - - /** - * 灏哹yte杞寲涓哄崄鍏繘鍒跺瓧绗︿覆 - * - * @param bytes - * @return - */ - public static String byte2hex(byte[] bytes) { - StringBuilder sign = new StringBuilder(); - for (int i = 0; i < bytes.length; i++) { - sign.append(String.valueOf(HEXSTR.charAt((bytes[i] & 0xF0) >> 4))); // 瀛楄妭楂�4浣� - sign.append(String.valueOf(HEXSTR.charAt(bytes[i] & 0x0F))); // 瀛楄妭浣�4浣� - } - return sign.toString().toUpperCase(); - } - - /** - * - * @param hexString - * @return 灏嗗崄鍏繘鍒惰浆鎹负瀛楄妭鏁扮粍 - */ - public static byte[] hexToBinary(String hexString) { - // hexString鐨勯暱搴﹀2鍙栨暣锛屼綔涓篵ytes鐨勯暱搴� - int len = hexString.length() / 2; - byte[] bytes = new byte[len]; - for (int i = 0; i < len; i++) { - // 鍙崇Щ鍥涗綅寰楀埌楂樹綅 - byte high = (byte) ((HEXSTR.indexOf(hexString.charAt(2 * i))) << 4);// 瀛楄妭楂樺洓浣� - byte low = (byte) HEXSTR.indexOf(hexString.charAt(2 * i + 1));// 瀛楄妭浣庡洓浣� - bytes[i] = (byte) (high | low);// 楂樺湴浣嶅仛鎴栬繍绠� - } - return bytes; - } - - /** - * - * @param str - * @return 杞崲涓轰簩杩涘埗瀛楃涓� - */ - public static String bytes2BinaryStr(byte[] bArray) { - String outStr = ""; - for (byte b : bArray) { - // 楂樺洓浣� - int pos = (b & 0xF0) >> 4; - outStr += binaryArray[pos]; - // 浣庡洓浣� - pos = b & 0x0F; - outStr += binaryArray[pos]; - } - return outStr; - - } -} +package org.yeshi.utils.encrypt; + +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import javax.crypto.Mac; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; + +/** + * hmac鍏敤鏂规硶绫� + * + * @author weihui.tang + * + */ +public class HmacUtils { + public static final String CHARSET_UTF8 = "UTF-8"; + + public static final String KEY_MAC = "HmacMD5"; + + private static final String HEXSTR = "0123456789ABCDEF"; + + private static String[] binaryArray = { "0000", "0001", "0010", "0011", + "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", + "1100", "1101", "1110", "1111" }; + + + + /** + * hmac-md5绛惧悕 + * + * @param data + * @param secret + * @return + * @throws IOException + * @throws NoSuchAlgorithmException + * @throws InvalidKeyException + */ + public static byte[] encryptHMAC(String data, String secret) + throws IOException, NoSuchAlgorithmException, InvalidKeyException { + byte[] bytes = null; + SecretKey secretKey = new SecretKeySpec(secret.getBytes(CHARSET_UTF8), + KEY_MAC); + Mac mac = Mac.getInstance(secretKey.getAlgorithm()); + mac.init(secretKey); + bytes = mac.doFinal(data.getBytes(CHARSET_UTF8)); + return bytes; + } + + /** + * 灏哹yte杞寲涓哄崄鍏繘鍒跺瓧绗︿覆 + * + * @param bytes + * @return + */ + public static String byte2hex(byte[] bytes) { + StringBuilder sign = new StringBuilder(); + for (int i = 0; i < bytes.length; i++) { + sign.append(String.valueOf(HEXSTR.charAt((bytes[i] & 0xF0) >> 4))); // 瀛楄妭楂�4浣� + sign.append(String.valueOf(HEXSTR.charAt(bytes[i] & 0x0F))); // 瀛楄妭浣�4浣� + } + return sign.toString().toUpperCase(); + } + + /** + * + * @param hexString + * @return 灏嗗崄鍏繘鍒惰浆鎹负瀛楄妭鏁扮粍 + */ + public static byte[] hexToBinary(String hexString) { + // hexString鐨勯暱搴﹀2鍙栨暣锛屼綔涓篵ytes鐨勯暱搴� + int len = hexString.length() / 2; + byte[] bytes = new byte[len]; + for (int i = 0; i < len; i++) { + // 鍙崇Щ鍥涗綅寰楀埌楂樹綅 + byte high = (byte) ((HEXSTR.indexOf(hexString.charAt(2 * i))) << 4);// 瀛楄妭楂樺洓浣� + byte low = (byte) HEXSTR.indexOf(hexString.charAt(2 * i + 1));// 瀛楄妭浣庡洓浣� + bytes[i] = (byte) (high | low);// 楂樺湴浣嶅仛鎴栬繍绠� + } + return bytes; + } + + /** + * + * @param str + * @return 杞崲涓轰簩杩涘埗瀛楃涓� + */ + public static String bytes2BinaryStr(byte[] bArray) { + String outStr = ""; + for (byte b : bArray) { + // 楂樺洓浣� + int pos = (b & 0xF0) >> 4; + outStr += binaryArray[pos]; + // 浣庡洓浣� + pos = b & 0x0F; + outStr += binaryArray[pos]; + } + return outStr; + + } +} -- Gitblit v1.8.0