13
13
import javax .crypto .Mac ;
14
14
import javax .crypto .spec .SecretKeySpec ;
15
15
16
- import static com .blankj .utilcode .utils .ConvertUtils .bytes2HexString ;
17
- import static com .blankj .utilcode .utils .ConvertUtils .hexString2Bytes ;
18
-
19
16
/**
20
17
* <pre>
21
18
* author: Blankj
@@ -816,4 +813,66 @@ public static byte[] desTemplate(byte[] data, byte[] key, String algorithm, Stri
816
813
return null ;
817
814
}
818
815
}
816
+
817
+ private static final char hexDigits [] = {'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' };
818
+
819
+ /**
820
+ * byteArr转hexString
821
+ * <p>例如:</p>
822
+ * bytes2HexString(new byte[] { 0, (byte) 0xa8 }) returns 00A8
823
+ *
824
+ * @param bytes 字节数组
825
+ * @return 16进制大写字符串
826
+ */
827
+ private static String bytes2HexString (byte [] bytes ) {
828
+ if (bytes == null ) return null ;
829
+ int len = bytes .length ;
830
+ if (len <= 0 ) return null ;
831
+ char [] ret = new char [len << 1 ];
832
+ for (int i = 0 , j = 0 ; i < len ; i ++) {
833
+ ret [j ++] = hexDigits [bytes [i ] >>> 4 & 0x0f ];
834
+ ret [j ++] = hexDigits [bytes [i ] & 0x0f ];
835
+ }
836
+ return new String (ret );
837
+ }
838
+
839
+
840
+ /**
841
+ * hexString转byteArr
842
+ * <p>例如:</p>
843
+ * hexString2Bytes("00A8") returns { 0, (byte) 0xA8 }
844
+ *
845
+ * @param hexString 十六进制字符串
846
+ * @return 字节数组
847
+ */
848
+ private static byte [] hexString2Bytes (String hexString ) {
849
+ if (StringUtils .isSpace (hexString )) return null ;
850
+ int len = hexString .length ();
851
+ if (len % 2 != 0 ) {
852
+ hexString = "0" + hexString ;
853
+ len = len + 1 ;
854
+ }
855
+ char [] hexBytes = hexString .toUpperCase ().toCharArray ();
856
+ byte [] ret = new byte [len >> 1 ];
857
+ for (int i = 0 ; i < len ; i += 2 ) {
858
+ ret [i >> 1 ] = (byte ) (hex2Dec (hexBytes [i ]) << 4 | hex2Dec (hexBytes [i + 1 ]));
859
+ }
860
+ return ret ;
861
+ }
862
+
863
+ /**
864
+ * hexChar转int
865
+ *
866
+ * @param hexChar hex单个字节
867
+ * @return 0..15
868
+ */
869
+ private static int hex2Dec (char hexChar ) {
870
+ if (hexChar >= '0' && hexChar <= '9' ) {
871
+ return hexChar - '0' ;
872
+ } else if (hexChar >= 'A' && hexChar <= 'F' ) {
873
+ return hexChar - 'A' + 10 ;
874
+ } else {
875
+ throw new IllegalArgumentException ();
876
+ }
877
+ }
819
878
}
0 commit comments