parent
cb072ca981
commit
b11d78d8a0
@ -0,0 +1,67 @@ |
||||
package com.android.base.utils.security; |
||||
|
||||
import java.io.FileInputStream; |
||||
import java.io.InputStream; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.security.MessageDigest; |
||||
import java.security.NoSuchAlgorithmException; |
||||
|
||||
public class MD5Utils { |
||||
|
||||
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
||||
|
||||
private static String toHexString(byte[] bytes) { |
||||
StringBuilder sb = new StringBuilder(bytes.length * 2); |
||||
for (byte b : bytes) { |
||||
sb.append(HEX_DIGITS[(b & 0xf0) >>> 4]); |
||||
sb.append(HEX_DIGITS[b & 0x0f]); |
||||
} |
||||
return sb.toString(); |
||||
} |
||||
|
||||
/** |
||||
* 文件加密 |
||||
*/ |
||||
public static String md5file(String filename) { |
||||
InputStream fis; |
||||
byte[] buffer = new byte[1024]; |
||||
int numRead = 0; |
||||
MessageDigest md5; |
||||
try { |
||||
fis = new FileInputStream(filename); |
||||
md5 = MessageDigest.getInstance("MD5"); |
||||
while ((numRead = fis.read(buffer)) > 0) { |
||||
md5.update(buffer, 0, numRead); |
||||
} |
||||
fis.close(); |
||||
return toHexString(md5.digest()); |
||||
} catch (Exception e) { |
||||
System.out.println("error"); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 字符串加密 |
||||
*/ |
||||
public static String md5(String string) { |
||||
byte[] hash; |
||||
try { |
||||
hash = MessageDigest.getInstance("MD5").digest(string.getBytes(StandardCharsets.UTF_8)); |
||||
} catch (NoSuchAlgorithmException e) { |
||||
throw new RuntimeException("Huh, MD5 should be supported?", e); |
||||
} |
||||
|
||||
StringBuilder hex = new StringBuilder(hash.length * 2); |
||||
|
||||
for (byte b : hash) { |
||||
if ((b & 0xFF) < 0x10) { |
||||
hex.append("0"); |
||||
} |
||||
hex.append(Integer.toHexString(b & 0xFF)); |
||||
} |
||||
|
||||
return hex.toString(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,32 @@ |
||||
package com.android.base.utils.security; |
||||
|
||||
import java.nio.charset.StandardCharsets; |
||||
import java.security.MessageDigest; |
||||
import java.security.NoSuchAlgorithmException; |
||||
|
||||
/** |
||||
* @author Ztiany |
||||
* Email: ztiany3@gmail.com |
||||
* Date : 2019-08-15 14:50 |
||||
*/ |
||||
@SuppressWarnings("WeakerAccess") |
||||
public class SHAUtils { |
||||
|
||||
private static final String SHA256 = "SHA256"; |
||||
|
||||
public static byte[] toSHA256(String content) { |
||||
return toSHA256(content.getBytes(StandardCharsets.UTF_8)); |
||||
} |
||||
|
||||
public static byte[] toSHA256(byte[] bytes) { |
||||
try { |
||||
MessageDigest md = MessageDigest.getInstance(SHA256); |
||||
md.update(bytes); |
||||
return md.digest(); |
||||
} catch (NoSuchAlgorithmException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
} |
@ -1,113 +0,0 @@ |
||||
package com.android.base.utils.security.util; |
||||
|
||||
import java.security.MessageDigest; |
||||
import java.security.NoSuchAlgorithmException; |
||||
|
||||
public class MD5Util { |
||||
|
||||
private final static String[] strDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; |
||||
|
||||
private static String byteToArrayString(byte bByte) { |
||||
int iRet = bByte; |
||||
if (iRet < 0) { |
||||
iRet += 256; |
||||
} |
||||
int iD1 = iRet / 16; |
||||
int iD2 = iRet % 16; |
||||
return strDigits[iD1] + strDigits[iD2]; |
||||
} |
||||
|
||||
private static String byteToString(byte[] bByte) { |
||||
StringBuilder sBuffer = new StringBuilder(); |
||||
for (byte aBByte : bByte) { |
||||
sBuffer.append(byteToArrayString(aBByte)); |
||||
} |
||||
return sBuffer.toString(); |
||||
} |
||||
|
||||
/** |
||||
* 32 位 MD5加密 |
||||
* |
||||
* @param str 待加密的字符串 |
||||
* @return result |
||||
*/ |
||||
public static String encrypt(String str) { |
||||
String result = null; |
||||
try { |
||||
result = str; |
||||
MessageDigest md = MessageDigest.getInstance("MD5"); |
||||
result = byteToString(md.digest(str.getBytes())); |
||||
} catch (NoSuchAlgorithmException ex) { |
||||
ex.printStackTrace(); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
|
||||
public static String encrypt(String algorithm, String str) { |
||||
try { |
||||
MessageDigest md = MessageDigest.getInstance(algorithm); |
||||
md.update(str.getBytes()); |
||||
StringBuilder sb = new StringBuilder(); |
||||
byte[] bytes = md.digest(); |
||||
for (int i = 0; i < bytes.length; i++) { |
||||
int b = bytes[i] & 0xFF; |
||||
if (b < 0x10) { |
||||
sb.append('0'); |
||||
} |
||||
sb.append(Integer.toHexString(b)); |
||||
} |
||||
return sb.toString(); |
||||
} catch (Exception e) { |
||||
return ""; |
||||
} |
||||
} |
||||
|
||||
public static String hashKeyForDisk(String key) { |
||||
String cacheKey; |
||||
try { |
||||
final MessageDigest mDigest = MessageDigest.getInstance("MD5"); |
||||
mDigest.update(key.getBytes()); |
||||
cacheKey = bytesToHexString(mDigest.digest()); |
||||
} catch (NoSuchAlgorithmException e) { |
||||
cacheKey = String.valueOf(key.hashCode()); |
||||
} |
||||
return cacheKey; |
||||
} |
||||
|
||||
private static String bytesToHexString(byte[] bytes) { |
||||
StringBuilder sb = new StringBuilder(); |
||||
for (int i = 0; i < bytes.length; i++) { |
||||
String hex = Integer.toHexString(0xFF & bytes[i]); |
||||
if (hex.length() == 1) { |
||||
sb.append('0'); |
||||
} |
||||
sb.append(hex); |
||||
} |
||||
return sb.toString(); |
||||
} |
||||
|
||||
|
||||
public static String getMd5Value(String secret) { |
||||
try { |
||||
MessageDigest bmd5 = MessageDigest.getInstance("MD5"); |
||||
bmd5.update(secret.getBytes()); |
||||
int i; |
||||
StringBuffer buf = new StringBuffer(); |
||||
byte[] b = bmd5.digest(); |
||||
for (int offset = 0; offset < b.length; offset++) { |
||||
i = b[offset]; |
||||
if (i < 0) |
||||
i += 256; |
||||
if (i < 16) |
||||
buf.append("0"); |
||||
buf.append(Integer.toHexString(i)); |
||||
} |
||||
return buf.toString(); |
||||
} catch (NoSuchAlgorithmException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return ""; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue