常见hash算法Java实现murmurhash3、HMAC SHA1、HMAC SHA256、HMAC SHA512、SipHash24
常见hash算法
目前支持hash算法:
- murmurhash3
- HMAC SHA1
- HMAC SHA256
- HMAC SHA512
- SipHash24
HashUtil旨在提供两种hash结果:
- int、long类型(及32bit、64bit)的hash code
- base64编码的hash字符串
HashUtil工具包
public final class HashUtil {
private static Map<HashAlgorithm, HashFunction> hashFunctionStrategy;
private static Key hmacSha1Key;
private static Key hmacSha256Key;
private static Key hmacSha512Key;
private static Key hmacMd5Key;
private static Base64.Encoder encoder = Base64.getEncoder();
private HashUtil() {
}
public static long hmacMd5(@Nonnull Object arg) {
HashCode hashCode = getHashCode(arg.toString(), HashAlgorithm.HMAC_MD5);
return hashCode.bits() <= 32 ? hashCode.asInt() : hashCode.asLong();
}
public static String hmacMd5AsBae64(@Nonnull Object arg) {
HashCode hashCode = getHashCode(arg.toString(), HashAlgorithm.HMAC_MD5);
return encoder.encodeToString(hashCode.asBytes());
}
public static long murmurhash3(@Nonnull Object arg) {
HashCode hashCode = getHashCode(arg.toString(), HashAlgorithm.Murmurhash3_128);
return hashCode.bits() <= 32 ? hashCode.asInt() : hashCode.asLong();
}
public static String murmurhash3AsBase64(@Nonnull Object arg) {
HashCode hashCode = getHashCode(arg.toString(), HashAlgorithm.Murmurhash3_128);
return encoder.encodeToString(hashCode.asBytes());
}
public static long hmacSHA1(@Nonnull Object arg) {
HashCode hashCode = getHashCode(arg.toString(), HashAlgorithm.HMAC_SHA1);
return hashCode.bits() <= 32 ? hashCode.asInt() : hashCode.asLong();
}
public static String hmacSHA1AsBase64(@Nonnull Object arg) {
HashCode hashCode = getHashCode(arg.toString(), HashAlgorithm.HMAC_SHA1);
return encoder.encodeToString(hashCode.asBytes());
}
public static long hmacSHA256(@Nonnull Object arg) {
HashCode hashCode = getHashCode(arg.toString(), HashAlgorithm.HMAC_SHA256);
return hashCode.bits() <= 32 ? hashCode.asInt() : hashCode.asLong();
}
public static String hmacSHA256AsBase64(@Nonnull Object arg) {
HashCode hashCode = getHashCode(arg.toString(), HashAlgorithm.HMAC_SHA256);
return encoder.encodeToString(hashCode.asBytes());
}
public static long hmacSHA512(@Nonnull Object arg) {
HashCode hashCode = getHashCode(arg.toString(), HashAlgorithm.HMAC_SHA512);
return hashCode.bits() <= 32 ? hashCode.asInt() : hashCode.asLong();
}
public static String hmacSHA512AsBase64(@Nonnull Object arg) {
HashCode hashCode = getHashCode(arg.toString(), HashAlgorithm.HMAC_SHA512);
return encoder.encodeToString(hashCode.asBytes());
}
public static long sipHash24(@Nonnull Object arg) {
HashCode hashCode = getHashCode(arg.toString(), HashAlgorithm.SIP_HASH24);
return hashCode.bits() <= 32 ? hashCode.asInt() : hashCode.asLong();
}
public static String sipHash24AsBase64(@Nonnull Object arg) {
HashCode hashCode = getHashCode(arg.toString(), HashAlgorithm.SIP_HASH24);
return encoder.encodeToString(hashCode.asBytes());
}
private static HashCode getHashCode(String content, HashAlgorithm algorithm) {
HashFunction hashFunc = hashFunctionStrategy.get(algorithm);
return hashFunc.hashBytes(content.getBytes(StandardCharsets.UTF_8));
}
public enum HashAlgorithm {
Murmurhash3_128,
HMAC_SHA1,
HMAC_SHA256,
HMAC_SHA512,
HMAC_MD5,
SIP_HASH24
}
static {
try {
hmacMd5Key = KeyGenerator.getInstance("HmacMD5").generateKey();
hmacSha1Key = KeyGenerator.getInstance("HmacSHA1").generateKey();
hmacSha256Key = KeyGenerator.getInstance("HmacSHA256").generateKey();
hmacSha512Key = KeyGenerator.getInstance("HmacSHA512").generateKey();
} catch (NoSuchAlgorithmException ignore) {
}
}
static {
hashFunctionStrategy = ImmutableMap.<HashAlgorithm, HashFunction>builder()
.put(HashAlgorithm.Murmurhash3_128, Hashing.murmur3_128())
.put(HashAlgorithm.HMAC_MD5, Hashing.hmacMd5(hmacMd5Key))
.put(HashAlgorithm.HMAC_SHA1, Hashing.hmacSha1(hmacSha1Key))
.put(HashAlgorithm.HMAC_SHA256, Hashing.hmacSha256(hmacSha256Key))
.put(HashAlgorithm.HMAC_SHA512, Hashing.hmacSha512(hmacSha512Key))
.put(HashAlgorithm.SIP_HASH24, Hashing.sipHash24())
.build();
}
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfijgch
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13