content: /*
   * Latke - 一款以 JSON 为主的 Java Web 框架
   * Copyright (c) 2009-present, b3log.org
   *
   * Latke is licensed under Mulan PSL v2.
   * You can use this software according to the terms and conditions of the Mulan PSL v2.
   * You may obtain a copy of Mulan PSL v2 at:
   *         http://license.coscl.org.cn/MulanPSL2
   * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
   * See the Mulan PSL v2 for more details.
   */
  package org.b3log.latke.util;
  
  import org.apache.commons.codec.binary.Base64;
  import org.apache.commons.codec.binary.Hex;
  import org.apache.logging.log4j.Level;
  import org.apache.logging.log4j.LogManager;
  import org.apache.logging.log4j.Logger;
  
  import javax.crypto.Cipher;
  import javax.crypto.KeyGenerator;
  import javax.crypto.Mac;
  import javax.crypto.SecretKey;
  import javax.crypto.spec.SecretKeySpec;
  import java.nio.charset.StandardCharsets;
  import java.security.SecureRandom;
  
  /**
   * Cryptology utilities.
   *
   * @author <a href="http://88250.b3log.org">Liang Ding</a>
   * @version 2.0.0.1, Jul 13, 2019
   * @since 2.3.14
   */
  public final class Crypts {
  
      /**
       * Signs the specified source string using the specified secret.
       *
       * @param source the specified source string
       * @param secret the specified secret
       * @return signed string
       */
      public static String signHmacSHA1(final String source, final String secret) {
          try {
              final Mac mac = Mac.getInstance("HmacSHA1");
              mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA1"));
              final byte[] signData = mac.doFinal(source.getBytes(StandardCharsets.UTF_8));
  
              return new String(Base64.encodeBase64(signData), StandardCharsets.UTF_8);
          } catch (final Exception e) {
              throw new RuntimeException("HMAC-SHA1 sign failed", e);
          }
      }
  
      /**
       * Logger.
       */
      private static final Logger LOGGER = LogManager.getLogger(Crypts.class);
  
      /**
       * Encrypts by AES.
       *
       * @param content the specified content to encrypt
       * @param key     the specified key
       * @return encrypted content
       * @see #decryptByAES(java.lang.String, java.lang.String)
       */
      public static String encryptByAES(final String content, final String key) {
          try {
              final KeyGenerator kgen = KeyGenerator.getInstance("AES");
              final SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
              secureRandom.setSeed(key.getBytes());
              kgen.init(128, secureRandom);
              final SecretKey secretKey = kgen.generateKey();
              final byte[] enCodeFormat = secretKey.getEncoded();
              final SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES");
              final Cipher cipher = Cipher.getInstance("AES");
              final byte[] byteContent = content.getBytes(StandardCharsets.UTF_8);
              cipher.init(Cipher.ENCRYPT_MODE, keySpec);
              final byte[] result = cipher.doFinal(byteContent);
  
              return Hex.encodeHexString(result);
          } catch (final Exception e) {
              LOGGER.log(Level.WARN, "Encrypt failed", e);
  
              return null;
          }
      }
  
      /**
       * Decrypts by AES.
       *
       * @param content the specified content to decrypt
       * @param key     the specified key
       * @return original content
       * @see #encryptByAES(java.lang.String, java.lang.String)
       */
      public static String decryptByAES(final String content, final String key) {
          try {
              final byte[] data = Hex.decodeHex(content.toCharArray());
              final KeyGenerator kgen = KeyGenerator.getInstance("AES");
              final SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
              secureRandom.setSeed(key.getBytes());
              kgen.init(128, secureRandom);
              final SecretKey secretKey = kgen.generateKey();
              final byte[] enCodeFormat = secretKey.getEncoded();
              final SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES");
              final Cipher cipher = Cipher.getInstance("AES");
              cipher.init(Cipher.DECRYPT_MODE, keySpec);
              final byte[] result = cipher.doFinal(data);
  
              return new String(result, StandardCharsets.UTF_8);
          } catch (final Exception e) {
              LOGGER.log(Level.WARN, "Decrypt failed");
  
              return null;
          }
      }
  
      private Crypts() {
      }
  }
  