content: /*
   *   Copyright (C)  2016 android@19code.com
   *
   *   Licensed under the Apache License, Version 2.0 (the "License");
   *   you may not use this file except in compliance with the License.
   *   You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
   *
   *   Unless required by applicable law or agreed to in writing, software
   *   distributed under the License is distributed on an "AS IS" BASIS,
   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *   See the License for the specific language governing permissions and
   *   limitations under the License.
   */
  
  package com.rsen.github.common;
  
  import android.util.Base64;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.security.DigestInputStream;
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  
  /**
   * Create by h4de5ing 2016/5/7 007
   * checked
   */
  public class CipherUtils {
  
      public static String encode(String input) {
          try {
              MessageDigest messageDigest = MessageDigest.getInstance("MD5");
              byte[] inputByteArray = input.getBytes();
              messageDigest.update(inputByteArray);
              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;
          }
      }
  
  
      public static String encode(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;
      }
  
  
      public static String base64Encode(String str) {
          return Base64.encodeToString(str.getBytes(), Base64.DEFAULT);
      }
  
  
      public static String base64Decode(String str) {
          return Base64.decode(str.getBytes(), Base64.DEFAULT).toString();
      }
  
  
      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;
      }
  
      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;
      }
  }
  