HmacCoder.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * FileName:HmacCoder.java
  3. * <p>
  4. * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (794890569@qq.com)</a>.
  5. * <p>
  6. * Licensed under the GNU General Public License, Version 3 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. * <p>
  10. * http://www.gnu.org/licenses/gpl-3.0.html
  11. * <p>
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. package cn.com.lzt.common.util.coder;
  20. import cn.com.lzt.common.util.security.Hex;
  21. import cn.com.lzt.common.util.security.SecurityCoder;
  22. import javax.crypto.KeyGenerator;
  23. import javax.crypto.Mac;
  24. import javax.crypto.SecretKey;
  25. import javax.crypto.spec.SecretKeySpec;
  26. /**
  27. * HMAC加密组件
  28. *
  29. * @author ShenHuaJie
  30. * @version 1.0
  31. * @since 1.0
  32. */
  33. public abstract class HmacCoder extends SecurityCoder {
  34. /**
  35. * 初始化HmacMD5密钥
  36. *
  37. * @return
  38. * @throws Exception
  39. */
  40. public static byte[] initHmacMD5Key() throws Exception {
  41. // 初始化KeyGenerator
  42. KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
  43. // 产生秘密密钥
  44. SecretKey secretKey = keyGenerator.generateKey();
  45. // 获得密钥
  46. return secretKey.getEncoded();
  47. }
  48. /**
  49. * HmacMD5加密
  50. *
  51. * @param data 待加密数据
  52. * @param key 密钥
  53. * @return byte[] 消息摘要
  54. * @throws Exception
  55. */
  56. public static byte[] encodeHmacMD5(byte[] data, byte[] key) throws Exception {
  57. // 还原密钥
  58. SecretKey secretKey = new SecretKeySpec(key, "HmacMD5");
  59. // 实例化Mac "SslMacMD5"
  60. Mac mac = Mac.getInstance("SslMacMD5");// secretKey.getAlgorithm());
  61. // 初始化Mac
  62. mac.init(secretKey);
  63. // 执行消息摘要
  64. return mac.doFinal(data);
  65. }
  66. /**
  67. * 初始化HmacSHA1密钥
  68. *
  69. * @return
  70. * @throws Exception
  71. */
  72. public static byte[] initHmacSHAKey() throws Exception {
  73. // 初始化KeyGenerator
  74. KeyGenerator keyGenerator = KeyGenerator.getInstance("HMacTiger");
  75. // 产生秘密密钥
  76. SecretKey secretKey = keyGenerator.generateKey();
  77. // 获得密钥
  78. return secretKey.getEncoded();
  79. }
  80. /**
  81. * HmacSHA1加密
  82. *
  83. * @param data 待加密数据
  84. * @param key 密钥
  85. * @return byte[] 消息摘要
  86. * @throws Exception
  87. */
  88. public static byte[] encodeHmacSHA(byte[] data, byte[] key) throws Exception {
  89. // 还原密钥
  90. SecretKey secretKey = new SecretKeySpec(key, "HMacTiger");
  91. // 实例化Mac SslMacMD5
  92. Mac mac = Mac.getInstance("SslMacMD5");// secretKey.getAlgorithm());
  93. // 初始化Mac
  94. mac.init(secretKey);
  95. // 执行消息摘要
  96. return mac.doFinal(data);
  97. }
  98. // // 根据所安装的 JCE 仲裁策略文件,返回指定转换的最大密钥长度。
  99. // public final static int getMaxAllowedKeyLength(String transformation)
  100. /**
  101. * 初始化HmacSHA256密钥
  102. *
  103. * @return
  104. * @throws Exception
  105. */
  106. public static byte[] initHmacSHA256Key() throws Exception {
  107. // 初始化KeyGenerator
  108. KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA256");
  109. // 产生秘密密钥
  110. SecretKey secretKey = keyGenerator.generateKey();
  111. // 获得密钥
  112. return secretKey.getEncoded();
  113. }
  114. /**
  115. * HmacSHA256加密
  116. *
  117. * @param data 待加密数据
  118. * @param key 密钥
  119. * @return byte[] 消息摘要
  120. * @throws Exception
  121. */
  122. public static byte[] encodeHmacSHA256(byte[] data, byte[] key) throws Exception {
  123. // 还原密钥
  124. SecretKey secretKey = new SecretKeySpec(key, "HmacSHA256");
  125. // 实例化Mac
  126. Mac mac = Mac.getInstance(secretKey.getAlgorithm());
  127. // 初始化Mac
  128. mac.init(secretKey);
  129. // 执行消息摘要
  130. return mac.doFinal(data);
  131. }
  132. /**
  133. * 初始化HmacSHA384密钥
  134. *
  135. * @return
  136. * @throws Exception
  137. */
  138. public static byte[] initHmacSHA384Key() throws Exception {
  139. // 初始化KeyGenerator
  140. KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA384");
  141. // 产生秘密密钥
  142. SecretKey secretKey = keyGenerator.generateKey();
  143. // 获得密钥
  144. return secretKey.getEncoded();
  145. }
  146. /**
  147. * HmacSHA384加密
  148. *
  149. * @param data 待加密数据
  150. * @param key 密钥
  151. * @return byte[] 消息摘要
  152. * @throws Exception
  153. */
  154. public static byte[] encodeHmacSHA384(byte[] data, byte[] key) throws Exception {
  155. // 还原密钥
  156. SecretKey secretKey = new SecretKeySpec(key, "HmacSHA384");
  157. // 实例化Mac
  158. Mac mac = Mac.getInstance(secretKey.getAlgorithm());
  159. // 初始化Mac
  160. mac.init(secretKey);
  161. // 执行消息摘要
  162. return mac.doFinal(data);
  163. }
  164. /**
  165. * 初始化HmacSHA512密钥
  166. *
  167. * @return
  168. * @throws Exception
  169. */
  170. public static byte[] initHmacSHA512Key() throws Exception {
  171. // 初始化KeyGenerator
  172. KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA512");
  173. // 产生秘密密钥
  174. SecretKey secretKey = keyGenerator.generateKey();
  175. // 获得密钥
  176. return secretKey.getEncoded();
  177. }
  178. /**
  179. * HmacSHA512加密
  180. *
  181. * @param data 待加密数据
  182. * @param key 密钥
  183. * @return byte[] 消息摘要
  184. * @throws Exception
  185. */
  186. public static byte[] encodeHmacSHA512(byte[] data, byte[] key) throws Exception {
  187. // 还原密钥
  188. SecretKey secretKey = new SecretKeySpec(key, "HmacSHA512");
  189. // 实例化Mac
  190. Mac mac = Mac.getInstance(secretKey.getAlgorithm());
  191. // 初始化Mac
  192. mac.init(secretKey);
  193. // 执行消息摘要
  194. return mac.doFinal(data);
  195. }
  196. /**
  197. * 初始化HmacMD2密钥
  198. *
  199. * @return byte[] 密钥
  200. * @throws Exception
  201. */
  202. public static byte[] initHmacMD2Key() throws Exception {
  203. // 初始化KeyGenerator
  204. KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD2");
  205. // 产生秘密密钥
  206. SecretKey secretKey = keyGenerator.generateKey();
  207. // 获得密钥
  208. return secretKey.getEncoded();
  209. }
  210. // /////////////////////////////////////////////////////////////////
  211. /**
  212. * HmacMD2消息摘要
  213. *
  214. * @param data 待做消息摘要处理的数据
  215. * @param byte[] 密钥
  216. * @return byte[] 消息摘要
  217. * @throws Exception
  218. */
  219. public static byte[] encodeHmacMD2(byte[] data, byte[] key) throws Exception {
  220. // 还原密钥
  221. SecretKey secretKey = new SecretKeySpec(key, "HmacMD2");
  222. // 实例化Mac
  223. Mac mac = Mac.getInstance(secretKey.getAlgorithm());
  224. // 初始化Mac
  225. mac.init(secretKey);
  226. // 执行消息摘要
  227. return mac.doFinal(data);
  228. }
  229. /**
  230. * HmacMD2Hex消息摘要
  231. *
  232. * @param data 待做消息摘要处理的数据
  233. * @param String 密钥
  234. * @return byte[] 消息摘要
  235. * @throws Exception
  236. */
  237. public static String encodeHmacMD2Hex(byte[] data, byte[] key) throws Exception {
  238. // 执行消息摘要
  239. byte[] b = encodeHmacMD2(data, key);
  240. // 做十六进制转换
  241. return new String(Hex.encode(b));
  242. }
  243. /**
  244. * 初始化HmacMD4密钥
  245. *
  246. * @return byte[] 密钥
  247. * @throws Exception
  248. */
  249. public static byte[] initHmacMD4Key() throws Exception {
  250. // 初始化KeyGenerator
  251. KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD4");
  252. // 产生秘密密钥
  253. SecretKey secretKey = keyGenerator.generateKey();
  254. // 获得密钥
  255. return secretKey.getEncoded();
  256. }
  257. /**
  258. * HmacMD4消息摘要
  259. *
  260. * @param data 待做消息摘要处理的数据
  261. * @param byte[] 密钥
  262. * @return byte[] 消息摘要
  263. * @throws Exception
  264. */
  265. public static byte[] encodeHmacMD4(byte[] data, byte[] key) throws Exception {
  266. // 还原密钥
  267. SecretKey secretKey = new SecretKeySpec(key, "HmacMD4");
  268. // 实例化Mac
  269. Mac mac = Mac.getInstance(secretKey.getAlgorithm());
  270. // 初始化Mac
  271. mac.init(secretKey);
  272. // 执行消息摘要
  273. return mac.doFinal(data);
  274. }
  275. /**
  276. * HmacMD4Hex消息摘要
  277. *
  278. * @param data 待做消息摘要处理的数据
  279. * @param byte[] 密钥
  280. * @return String 消息摘要
  281. * @throws Exception
  282. */
  283. public static String encodeHmacMD4Hex(byte[] data, byte[] key) throws Exception {
  284. // 执行消息摘要
  285. byte[] b = encodeHmacMD4(data, key);
  286. // 做十六进制转换
  287. return new String(Hex.encode(b));
  288. }
  289. /**
  290. * 初始化HmacSHA224密钥
  291. *
  292. * @return byte[] 密钥
  293. * @throws Exception
  294. */
  295. public static byte[] initHmacSHA224Key() throws Exception {
  296. // 初始化KeyGenerator
  297. KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA224");
  298. // 产生秘密密钥
  299. SecretKey secretKey = keyGenerator.generateKey();
  300. // 获得密钥
  301. return secretKey.getEncoded();
  302. }
  303. /**
  304. * HmacSHA224消息摘要
  305. *
  306. * @param data 待做消息摘要处理的数据
  307. * @param byte[] 密钥
  308. * @return byte[] 消息摘要
  309. * @throws Exception
  310. */
  311. public static byte[] encodeHmacSHA224(byte[] data, byte[] key) throws Exception {
  312. // 还原密钥
  313. SecretKey secretKey = new SecretKeySpec(key, "HmacSHA224");
  314. // 实例化Mac
  315. Mac mac = Mac.getInstance(secretKey.getAlgorithm());
  316. // 初始化Mac
  317. mac.init(secretKey);
  318. // 执行消息摘要
  319. return mac.doFinal(data);
  320. }
  321. /**
  322. * HmacSHA224Hex消息摘要
  323. *
  324. * @param data 待做消息摘要处理的数据
  325. * @param byte[] 密钥
  326. * @return String 消息摘要
  327. * @throws Exception
  328. */
  329. public static String encodeHmacSHA224Hex(byte[] data, byte[] key) throws Exception {
  330. // 执行消息摘要
  331. byte[] b = encodeHmacSHA224(data, key);
  332. // 做十六进制转换
  333. return new String(Hex.encode(b));
  334. }
  335. }