package com.supwisdom.utils | |
import java.io.InputStream | |
import java.io.UnsupportedEncodingException | |
import java.security.InvalidKeyException | |
import java.security.MessageDigest | |
import java.security.NoSuchAlgorithmException | |
import javax.crypto.Mac | |
import javax.crypto.spec.SecretKeySpec | |
/** | |
** create by zzq on 2019/7/24 | |
** @desc | |
**/ | |
object CryptUtil { | |
fun HASH256(text: String): String { | |
return algorithm2(text, "SHA-256", true) | |
} | |
fun HASH256(fis: InputStream): String { | |
return algorithm(fis, "SHA-256") | |
} | |
fun HASH256(fis: InputStream, extData: String): String { | |
return algorithm(fis, "SHA-256", extData) | |
} | |
fun HASH512(text: String): String { | |
return algorithm2(text, "SHA-512", true) | |
} | |
fun MD5(text: String): String { | |
return algorithm2(text, "MD5", false) | |
} | |
fun HMACSHA1(data: String, key: String): String { | |
try { | |
val algorithm = "HmacSHA1" | |
val mac = Mac.getInstance(algorithm) | |
val spec = SecretKeySpec(key.toByteArray(), algorithm) | |
mac.init(spec) | |
val byteHMAC = mac.doFinal(data.toByteArray()) | |
return byteHMAC.encodeHex() | |
} catch (e: InvalidKeyException) { | |
e.printStackTrace() | |
} catch (ignore: NoSuchAlgorithmException) { | |
ignore.printStackTrace() | |
} | |
return "" | |
} | |
fun HMACSHA256(data: String, key: String): String { | |
try { | |
val algorithm = "HmacSHA256" | |
val mac = Mac.getInstance(algorithm) | |
val spec = SecretKeySpec(key.toByteArray(), algorithm) | |
mac.init(spec) | |
val byteHMAC = mac.doFinal(data.toByteArray()) | |
return byteHMAC.encodeHex() | |
} catch (e: InvalidKeyException) { | |
e.printStackTrace() | |
} catch (ignore: NoSuchAlgorithmException) { | |
ignore.printStackTrace() | |
} | |
return "" | |
} | |
private fun algorithm2(text: String?, algorithm: String, isUpperCase: Boolean): String { | |
if (text != null && text.isNotEmpty()) { | |
try { | |
//创建具有指定算法名称的信息摘要 | |
val md = MessageDigest.getInstance(algorithm) | |
//使用指定的字节数组对摘要进行最后更新,然后完成摘要计算 | |
val results = md.digest(text.toByteArray(charset("UTF-8"))) | |
//将得到的字节数组变成字符串返回 | |
val resultString = results.encodeHex() | |
return if (isUpperCase) { | |
resultString.toUpperCase() | |
} else { | |
resultString.toLowerCase() | |
} | |
} catch (ex: NoSuchAlgorithmException) { | |
ex.printStackTrace() | |
} catch (ex: UnsupportedEncodingException) { | |
ex.printStackTrace() | |
} | |
} | |
return "" | |
} | |
private fun algorithm(fis: InputStream, algorithm: String): String { | |
try { | |
//拿到一个MD5转换器,如果想使用SHA-1或SHA-256,则传入SHA-1,SHA-256 | |
val md = MessageDigest.getInstance(algorithm) | |
//分多次将一个文件读入,对于大型文件而言,比较推荐这种方式,占用内存比较少。 | |
val buffer = ByteArray(1024) | |
var length: Int | |
while (true) { | |
length = fis.read(buffer, 0, 1024) | |
if (length < 0) { | |
break | |
} | |
md.update(buffer, 0, length) | |
} | |
fis.close() | |
//转换并返回包含16个元素字节数组,返回数值范围为-128到127 | |
return md.digest().encodeHex() | |
} catch (e: Exception) { | |
e.printStackTrace() | |
return "" | |
} | |
} | |
private fun algorithm(fis: InputStream, algorithm: String, extData: String): String { | |
try { | |
//拿到一个MD5转换器,如果想使用SHA-1或SHA-256,则传入SHA-1,SHA-256 | |
val md = MessageDigest.getInstance(algorithm) | |
//分多次将一个文件读入,对于大型文件而言,比较推荐这种方式,占用内存比较少。 | |
val buffer = ByteArray(1024) | |
var length: Int | |
while (true) { | |
length = fis.read(buffer, 0, 1024) | |
if (length < 0) { | |
break | |
} | |
md.update(buffer, 0, length) | |
} | |
fis.close() | |
md.update(extData.toByteArray(charset("UTF-8"))) | |
//转换并返回包含16个元素字节数组,返回数值范围为-128到127 | |
return md.digest().encodeHex() | |
} catch (e: Exception) { | |
e.printStackTrace() | |
return "" | |
} | |
} | |
} |