• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

常见hash算法Java实现murmurhash3、HMAC SHA1、HMAC SHA256、HMAC SHA512、SipHash24

武飞扬头像
MateMaster
帮助1

常见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
系列文章
更多 icon
同类精品
更多 icon
继续加载