Esempio n. 1
0
 /** URL 编码, Encode默认为UTF-8. */
 public static String urlEncode(String part) {
   try {
     return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
   } catch (UnsupportedEncodingException e) {
     throw Exceptions.unchecked(e);
   }
 }
Esempio n. 2
0
 /** Hex解码. */
 public static byte[] decodeHex(String input) {
   try {
     return Hex.decodeHex(input.toCharArray());
   } catch (DecoderException e) {
     throw Exceptions.unchecked(e);
   }
 }
Esempio n. 3
0
 /** 生成HMAC-SHA1密钥,返回字节数组,长度为160位(20字节). HMAC-SHA1算法对密钥无特殊要求, RFC2401建议最少长度为160位(20字节). */
 public static byte[] generateHmacSha1Key() {
   try {
     KeyGenerator keyGenerator = KeyGenerator.getInstance(HMACSHA1);
     keyGenerator.init(DEFAULT_HMACSHA1_KEYSIZE);
     SecretKey secretKey = keyGenerator.generateKey();
     return secretKey.getEncoded();
   } catch (GeneralSecurityException e) {
     throw Exceptions.unchecked(e);
   }
 }
Esempio n. 4
0
 /**
  * 使用HMAC-SHA1进行消息签名, 返回字节数组,长度为20字节.
  *
  * @param input 原始输入字符数组
  * @param key HMAC-SHA1密钥
  */
 public static byte[] hmacSha1(byte[] input, byte[] key) {
   try {
     SecretKey secretKey = new SecretKeySpec(key, HMACSHA1);
     Mac mac = Mac.getInstance(HMACSHA1);
     mac.init(secretKey);
     return mac.doFinal(input);
   } catch (GeneralSecurityException e) {
     throw Exceptions.unchecked(e);
   }
 }
Esempio n. 5
0
 /** 生成AES密钥,可选长度为128,192,256位. */
 public static byte[] generateAesKey(int keysize) {
   try {
     KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
     keyGenerator.init(keysize);
     SecretKey secretKey = keyGenerator.generateKey();
     return secretKey.getEncoded();
   } catch (GeneralSecurityException e) {
     throw Exceptions.unchecked(e);
   }
 }
Esempio n. 6
0
 /**
  * 使用AES加密或解密无编码的原始字节数组, 返回无编码的字节数组结果.
  *
  * @param input 原始字节数组
  * @param key 符合AES要求的密钥
  * @param mode Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE
  */
 private static byte[] aes(byte[] input, byte[] key, int mode) {
   try {
     SecretKey secretKey = new SecretKeySpec(key, AES);
     Cipher cipher = Cipher.getInstance(AES);
     cipher.init(mode, secretKey);
     return cipher.doFinal(input);
   } catch (GeneralSecurityException e) {
     throw Exceptions.unchecked(e);
   }
 }
Esempio n. 7
0
  private static byte[] digest(InputStream input, String algorithm) throws IOException {
    try {
      MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
      int bufferLength = 8 * 1024;
      byte[] buffer = new byte[bufferLength];
      int read = input.read(buffer, 0, bufferLength);

      while (read > -1) {
        messageDigest.update(buffer, 0, read);
        read = input.read(buffer, 0, bufferLength);
      }

      return messageDigest.digest();
    } catch (GeneralSecurityException e) {
      throw Exceptions.unchecked(e);
    }
  }
Esempio n. 8
0
  /** 对字符串进行散列, 支持md5与sha1算法. */
  private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
      MessageDigest digest = MessageDigest.getInstance(algorithm);

      if (salt != null) {
        digest.update(salt);
      }

      byte[] result = digest.digest(input);

      for (int i = 1; i < iterations; i++) {
        digest.reset();
        result = digest.digest(result);
      }
      return result;
    } catch (GeneralSecurityException e) {
      throw Exceptions.unchecked(e);
    }
  }