Browse Source

collate => EncryptUtil格式整理

JustryDeng 1 năm trước cách đây
mục cha
commit
184e5865aa

+ 62 - 60
class-winter-core/src/main/java/winter/com/ideaaedi/classwinter/util/EncryptUtil.java

@@ -29,23 +29,26 @@ import java.util.Random;
  */
 public final class EncryptUtil {
     
-    /** 盐 */
+    /**
+     * 盐
+     */
     private static final char[] SALT = {'c', 'h', 'i', '@', 'm', 'i', 'a', 'n', '#', 'y', 'a', 'o', '%', 'f', 'a', 'n', 'g', '^', 'y', 'a', 'n'};
     
-    /** AES 算法/模式/补码方式 */
-    private static final  String AES_MODE = "AES/ECB/PKCS5Padding";
+    /**
+     * AES 算法/模式/补码方式
+     */
+    private static final String AES_MODE = "AES/ECB/PKCS5Padding";
     
     /**
      * 加密
      * <p>
-     *     与{@link EncryptUtil#decrypt(byte[], char[])}成对使用
+     * 与{@link EncryptUtil#decrypt(byte[], char[])}成对使用
      * </p>
      *
-     * @param source
-     *            要加密的内容
-     * @param password
-     *            用户密钥
-     * @return  加密后的内容
+     * @param source 要加密的内容
+     * @param password 用户密钥
+     *
+     * @return 加密后的内容
      */
     public static byte[] encrypt(byte[] source, char[] password) {
         try {
@@ -59,14 +62,13 @@ public final class EncryptUtil {
     /**
      * 解密
      * <p>
-     *     与{@link EncryptUtil#encrypt(byte[], char[])}成对使用
+     * 与{@link EncryptUtil#encrypt(byte[], char[])}成对使用
      * </p>
      *
-     * @param source
-     *            要解密的内容
-     * @param password
-     *            用户密钥
-     * @return  解密后的内容
+     * @param source 要解密的内容
+     * @param password 用户密钥
+     *
+     * @return 解密后的内容
      */
     public static byte[] decrypt(byte[] source, char[] password) {
         try {
@@ -77,17 +79,16 @@ public final class EncryptUtil {
         }
     }
     
-    
     /**
      * 加密
      * <p>
-     *     与{@link EncryptUtil#decrypt(String, char[])}成对使用
+     * 与{@link EncryptUtil#decrypt(String, char[])}成对使用
      * </p>
-     * @param source
-     *            要加密的内容
-     * @param password
-     *            用户密钥
-     * @return  加密后的内容
+     *
+     * @param source 要加密的内容
+     * @param password 用户密钥
+     *
+     * @return 加密后的内容
      */
     public static String encrypt(String source, char[] password) {
         Objects.requireNonNull(source, "source cannot be null.");
@@ -97,13 +98,13 @@ public final class EncryptUtil {
     /**
      * 解密
      * <p>
-     *     与{@link EncryptUtil#encrypt(String, char[])}成对使用
+     * 与{@link EncryptUtil#encrypt(String, char[])}成对使用
      * </p>
-     * @param source
-     *            要解密的内容
-     * @param password
-     *            用户密钥
-     * @return  解密后的内容
+     *
+     * @param source 要解密的内容
+     * @param password 用户密钥
+     *
+     * @return 解密后的内容
      */
     public static String decrypt(String source, char[] password) {
         Objects.requireNonNull(source, "source cannot be null.");
@@ -113,9 +114,9 @@ public final class EncryptUtil {
     /**
      * 生成指定长度的随机字符串
      *
-     * @param length
-     *            长度
-     * @return  字符数组
+     * @param length 长度
+     *
+     * @return 字符数组
      */
     public static char[] generateCharArr(int length) {
         char[] result = new char[length];
@@ -144,18 +145,17 @@ public final class EncryptUtil {
     /**
      * 进行MD5加密
      * <p>
-     *     摘录自 <a href="https://www.jianshu.com/p/b419163272c1" />
+     * 摘录自 <a href="https://www.jianshu.com/p/b419163272c1" />
      * </p>
-     *  注:MD5加密得到的是长度固定的为126bit的二进制串(一堆0和1,一共128),为了更友好的表示结果,
-     *     一般都将128位的二进制串转换为32个16进制位或16个16进制位。(16位的结果是取32位的结果值的中间部分,即32位中第8~24位的片段)
+     * 注:MD5加密得到的是长度固定的为126bit的二进制串(一堆0和1,一共128),为了更友好的表示结果,
+     * 一般都将128位的二进制串转换为32个16进制位或16个16进制位。(16位的结果是取32位的结果值的中间部分,即32位中第8~24位的片段)
+     *
+     * @param source 要加密的内容
+     * @param abbreviation true-返回16位结果;false-返回32位结果
+     * @param upperCase true-结果大写;false-结果小写
      *
-     * @param source
-     *            要加密的内容
-     * @param abbreviation
-     *            true-返回16位结果;false-返回32位结果
-     * @param upperCase
-     *            true-结果大写;false-结果小写
      * @return MD5加密结果
+     *
      * @throws NoSuchAlgorithmException 无此算法时抛出
      */
     @SuppressWarnings("SameParameterValue")
@@ -194,11 +194,10 @@ public final class EncryptUtil {
     /**
      * AES加密
      *
-     * @param source
-     *            要加密的内容
-     * @param key
-     *            密钥
-     * @return  加密后的内容
+     * @param source 要加密的内容
+     * @param key 密钥
+     *
+     * @return 加密后的内容
      */
     private static byte[] encryptByAes(byte[] source, char[] key) {
         try {
@@ -207,7 +206,8 @@ public final class EncryptUtil {
             Cipher cipher = Cipher.getInstance(AES_MODE);
             cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
             return cipher.doFinal(source);
-        } catch (NoSuchAlgorithmException|NoSuchPaddingException|InvalidKeyException|IllegalBlockSizeException|BadPaddingException e) {
+        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException |
+                 BadPaddingException e) {
             throw new ClassWinterException(e);
         }
     }
@@ -215,11 +215,10 @@ public final class EncryptUtil {
     /**
      * AES解密
      *
-     * @param source
-     *            要解密的内容
-     * @param key
-     *            密钥
-     * @return  解密后的内容
+     * @param source 要解密的内容
+     * @param key 密钥
+     *
+     * @return 解密后的内容
      */
     private static byte[] decryptByAes(byte[] source, char[] key) {
         try {
@@ -228,7 +227,8 @@ public final class EncryptUtil {
             Cipher cipher = Cipher.getInstance(AES_MODE);
             cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
             return cipher.doFinal(source);
-        } catch (NoSuchAlgorithmException|NoSuchPaddingException|InvalidKeyException|IllegalBlockSizeException|BadPaddingException e) {
+        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException |
+                 BadPaddingException e) {
             throw new ClassWinterException(e);
         }
     }
@@ -237,14 +237,16 @@ public final class EncryptUtil {
      * 测试
      */
     public static void main(String[] args) {
-//        // => 测试一
-//        String originStr = "嘿~boy~";
-//        byte[] encryptedContent = encrypt(originStr.getBytes(StandardCharsets.UTF_8), "好想吃烧烤JustryDeng123...".toCharArray());
-//        String plaintext = new String(decrypt(encryptedContent, "好想吃烧烤JustryDeng123...".toCharArray()), StandardCharsets.UTF_8);
-//        System.err.println("原文:" + originStr);
-//        System.err.println("加密后:" + encryptedContent);
-//        ///System.err.println("加密后:" + new String(Base64.getEncoder().encode(encryptedContent)));
-//        System.err.println("解密后:" + plaintext);
+        //        // => 测试一
+        //        String originStr = "嘿~boy~";
+        //        byte[] encryptedContent = encrypt(originStr.getBytes(StandardCharsets.UTF_8), "好想吃烧烤JustryDeng123..
+        //        .".toCharArray());
+        //        String plaintext = new String(decrypt(encryptedContent, "好想吃烧烤JustryDeng123...".toCharArray()),
+        //        StandardCharsets.UTF_8);
+        //        System.err.println("原文:" + originStr);
+        //        System.err.println("加密后:" + encryptedContent);
+        //        ///System.err.println("加密后:" + new String(Base64.getEncoder().encode(encryptedContent)));
+        //        System.err.println("解密后:" + plaintext);
         
         // => 测试二
         String originStr = "嘿~boy~";