/*
* 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.code19.library;
import android.util.Base64;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.DigestInputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
/**
* Create by h4de5ing 2016/5/7 007
*/
public class CipherUtils {
public static String md5(String input) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(input.getBytes());
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 md5L(String input) {
try {
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(input.getBytes());
byte[] md = mdInst.digest();
StringBuilder hexString = new StringBuilder();
for (byte aMd : md) {
String shaHex = Integer.toHexString(aMd & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
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;
}
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;
}
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 "";
}
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 {
FileUtils.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();
}
}