mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-17 20:40:25 +08:00
168 lines
5.3 KiB
Java
168 lines
5.3 KiB
Java
package minsecurity.crypto;
|
|
|
|
import edu.pku.commons.codec.digest.DigestUtils;
|
|
import edu.pku.commons.codec.digest.HmacAlgorithms;
|
|
import edu.pku.commons.codec.digest.HmacUtils;
|
|
import org.bouncycastle.crypto.digests.SM3Digest;
|
|
import org.bouncycastle.crypto.macs.HMac;
|
|
import org.bouncycastle.crypto.params.KeyParameter;
|
|
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
/*
|
|
* @Author: hongyu guo
|
|
* @Description: 封装常用的hash算法,包括sm3、sha256、sha512、md5, 大量应用apache commons-codec库
|
|
* @Version: 1.0.0
|
|
* @Date: 15:10 2021/03/04
|
|
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
|
*/
|
|
public class HashAlgo {
|
|
|
|
///////////////
|
|
///// SM3 /////
|
|
///////////////
|
|
public static byte[] sm3(byte[] content){
|
|
SM3Digest digest = new SM3Digest();
|
|
digest.update(content, 0, content.length);
|
|
byte[] hash = new byte[digest.getDigestSize()];
|
|
digest.doFinal(hash, 0);
|
|
return hash;
|
|
}
|
|
|
|
public static byte[] sm3(String content){
|
|
return sm3(content.getBytes(StandardCharsets.UTF_8));
|
|
}
|
|
|
|
public static byte[] sm3(String content, Charset charset){
|
|
return sm3(content.getBytes(charset));
|
|
}
|
|
|
|
////////////////////
|
|
///// SM3-HMAC /////
|
|
////////////////////
|
|
public static byte[] hmacSM3(byte[] content, byte[] key){
|
|
KeyParameter keyParameter = new KeyParameter(key);
|
|
SM3Digest digest = new SM3Digest();
|
|
HMac mac = new HMac(digest);
|
|
mac.init(keyParameter);
|
|
mac.update(content, 0, content.length);
|
|
byte[] result = new byte[mac.getMacSize()];
|
|
mac.doFinal(result, 0);
|
|
return result;
|
|
}
|
|
|
|
public static byte[] hmacSM3(String content, byte[] key){
|
|
return hmacSM3(content.getBytes(StandardCharsets.UTF_8),key);
|
|
}
|
|
|
|
public static byte[] hmacSM3(String content, byte[] key, Charset charset){
|
|
return hmacSM3(content.getBytes(charset),key);
|
|
}
|
|
|
|
///////////////
|
|
///// MD5 /////
|
|
///////////////
|
|
public static byte[] md5(String content, Charset charset){
|
|
byte[] bytesOfContent = content.getBytes(charset);
|
|
return md5(bytesOfContent);
|
|
}
|
|
|
|
public static byte[] md5(String content){
|
|
return DigestUtils.md5(content);
|
|
}
|
|
|
|
public static byte[] md5(byte[] content){
|
|
|
|
return DigestUtils.md5(content);
|
|
}
|
|
|
|
////////////////
|
|
//// SHA256 ////
|
|
////////////////
|
|
public static byte[] sha256(byte[] content){
|
|
return DigestUtils.sha256(content);
|
|
}
|
|
|
|
public static byte[] sha256(String content){
|
|
return DigestUtils.sha256(content);
|
|
}
|
|
|
|
public static byte[] sha256(String content, Charset charset){
|
|
byte[] bytesOfContent = content.getBytes(charset);
|
|
return DigestUtils.sha256(bytesOfContent);
|
|
}
|
|
|
|
////////////////
|
|
//// SHA512 ////
|
|
////////////////
|
|
public static byte[] sha512(byte[] content){
|
|
return DigestUtils.sha512(content);
|
|
}
|
|
|
|
public static byte[] sha512(String content){
|
|
return DigestUtils.sha512(content);
|
|
}
|
|
|
|
public static byte[] sha512(String content, Charset charset){
|
|
byte[] bytesOfContent = content.getBytes(charset);
|
|
return DigestUtils.sha512(bytesOfContent);
|
|
}
|
|
|
|
//////////////////
|
|
//// HMAC-MD5 ////
|
|
//////////////////
|
|
public static byte[] hmacMD5(String content, byte[] key){
|
|
HmacUtils hmacMd5Util = new HmacUtils(HmacAlgorithms.HMAC_MD5,key);
|
|
return hmacMd5Util.hmac(content);
|
|
}
|
|
|
|
public static byte[] hmacMD5(byte[] content, byte[] key){
|
|
HmacUtils hmacMd5Util = new HmacUtils(HmacAlgorithms.HMAC_MD5,key);
|
|
return hmacMd5Util.hmac(content);
|
|
}
|
|
|
|
public static byte[] hmacMD5(String content, byte[] key, Charset charset){
|
|
HmacUtils hmacMd5Util = new HmacUtils(HmacAlgorithms.HMAC_MD5,key);
|
|
return hmacMd5Util.hmac(content.getBytes(charset));
|
|
}
|
|
|
|
|
|
////////////////////
|
|
//// SHA256-MD5 ////
|
|
////////////////////
|
|
public static byte[] hmacSHA256(String content, byte[] key){
|
|
HmacUtils hmacMd5Util = new HmacUtils(HmacAlgorithms.HMAC_SHA_256,key);
|
|
return hmacMd5Util.hmac(content);
|
|
}
|
|
|
|
public static byte[] hmacSHA256(byte[] content, byte[] key){
|
|
HmacUtils hmacMd5Util = new HmacUtils(HmacAlgorithms.HMAC_SHA_256,key);
|
|
return hmacMd5Util.hmac(content);
|
|
}
|
|
|
|
public static byte[] hmacSHA256(String content, byte[] key, Charset charset){
|
|
HmacUtils hmacMd5Util = new HmacUtils(HmacAlgorithms.HMAC_SHA_256,key);
|
|
return hmacMd5Util.hmac(content.getBytes(charset));
|
|
}
|
|
|
|
////////////////////
|
|
//// SHA512-MD5 ////
|
|
////////////////////
|
|
public static byte[] hmacSHA512(String content, byte[] key){
|
|
HmacUtils hmacMd5Util = new HmacUtils(HmacAlgorithms.HMAC_SHA_512,key);
|
|
return hmacMd5Util.hmac(content);
|
|
}
|
|
|
|
public static byte[] hmacSHA512(byte[] content, byte[] key){
|
|
HmacUtils hmacMd5Util = new HmacUtils(HmacAlgorithms.HMAC_SHA_512,key);
|
|
return hmacMd5Util.hmac(content);
|
|
}
|
|
|
|
public static byte[] hmacSHA512(String content, byte[] key, Charset charset){
|
|
HmacUtils hmacMd5Util = new HmacUtils(HmacAlgorithms.HMAC_SHA_512,key);
|
|
return hmacMd5Util.hmac(content.getBytes(charset));
|
|
}
|
|
}
|