content: package com.jingewenku.abrahamcaijin.commonutil.encryption;
  
  import com.jingewenku.abrahamcaijin.commonutil.AppFileMgr;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.security.DigestInputStream;
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  
  /**
   * @Description:主要功能:加密工具类
   * @Prject: CommonUtilLibrary
   * @Package: com.jingewenku.abrahamcaijin.commonutil.encryption
   * @author: AbrahamCaiJin
   * @date: 2017年06月02日 10:05
   * @Copyright: 个人版权所有
   * @Company:
   * @version: 1.0.0
   */
  
  public class CipherUtils {
  
      /**
       * 输入流转md5
       * @param in
       * @return
       */
      public static String md5(InputStream in) {
          int bufferSize = 256 * 1024;
          DigestInputStream digestInputStream = null;
          try {
              MessageDigest messageDigest = MessageDigest.getInstance("MD5");
              digestInputStream = new DigestInputStream(in, messageDigest);
              byte[] buffer = new byte[bufferSize];
              while (digestInputStream.read(buffer) > 0) ;
              messageDigest = digestInputStream.getMessageDigest();
              byte[] resultByteArray = messageDigest.digest();
              char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
              char[] resultCharArray = new char[resultByteArray.length * 2];
              int index = 0;
              for (byte b : resultByteArray) {
                  resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
                  resultCharArray[index++] = hexDigits[b & 0xf];
              }
              return new String(resultCharArray);
          } catch (NoSuchAlgorithmException e) {
              return null;
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              try {
                  if (digestInputStream != null)
                      digestInputStream.close();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
          return null;
      }
  
      /**
       * 异或加密
       * @param str
       * @param privatekey
       * @return
       */
      public static String XorEncode(String str, String privatekey) {
          int[] snNum = new int[str.length()];
          String result = "";
          String temp = "";
          for (int i = 0, j = 0; i < str.length(); i++, j++) {
              if (j == privatekey.length())
                  j = 0;
              snNum[i] = str.charAt(i) ^ privatekey.charAt(j);
          }
          for (int k = 0; k < str.length(); k++) {
              if (snNum[k] < 10) {
                  temp = "00" + snNum[k];
              } else {
                  if (snNum[k] < 100) {
                      temp = "0" + snNum[k];
                  }
              }
              result += temp;
          }
          return result;
      }
  
      /**
       * 异或解密
       * @param str
       * @param privateKey
       * @return
       */
      public static String XorDecode(String str, String privateKey) {
          char[] snNum = new char[str.length() / 3];
          String result = "";
  
          for (int i = 0, j = 0; i < str.length() / 3; i++, j++) {
              if (j == privateKey.length())
                  j = 0;
              int n = Integer.parseInt(str.substring(i * 3, i * 3 + 3));
              snNum[i] = (char) ((char) n ^ privateKey.charAt(j));
          }
          for (int k = 0; k < str.length() / 3; k++) {
              result += snNum[k];
          }
          return result;
      }
  
      /**
       * 字符串sha1值
       * @param str
       * @return
       */
      public static String sha1(String str) {
          try {
              MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
              digest.update(str.getBytes());
              byte messageDigest[] = digest.digest();
              StringBuilder hexString = new StringBuilder();
              for (byte aMessageDigest : messageDigest) {
                  String shaHex = Integer.toHexString(aMessageDigest & 0xFF);
                  if (shaHex.length() < 2) {
                      hexString.append(0);
                  }
                  hexString.append(shaHex);
              }
              return hexString.toString();
  
          } catch (NoSuchAlgorithmException e) {
              e.printStackTrace();
          }
          return "";
      }
  
      /**
       * 文件hash校验
       * @param file
       * @return
       */
      public static String sha1(File file) {
          FileInputStream in = null;
          try {
              in = new FileInputStream(file);
              MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
              byte[] b = new byte[1024 * 1024 * 10];//10M memory
              int len;
              while ((len = in.read(b)) > 0) {
                  messageDigest.update(b, 0, len);
              }
              return byte2Hex(messageDigest.digest());
          } catch (NoSuchAlgorithmException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              AppFileMgr.closeIO(in);
          }
          return null;
      }
  
      private static String byte2Hex(byte[] b) {
          StringBuilder sb = new StringBuilder();
          for (byte aB : b) {
              String s = Integer.toHexString(aB & 0xFF);
              if (s.length() == 1) {
                  sb.append("0");
              }
              //sb.append(s.toUpperCase());
              sb.append(s);
          }
          return sb.toString();
      }
  }
  