Java学习笔记-XXH3哈希算法

XXH3是由Yann Collet设计的非加密哈希算法,属于XXHash系列的最新变种,专注于极速性能与低碰撞率,适用于对计算效率要求极高的场景。
极速性能
  • 在RAM速度限制下运行,小数据(如 1-128 字节)处理可达纳秒级,吞吐率高达 0.24--0.71 GB/s。
  • 大数据(如 100KB)处理可在微秒级完成,性能远超传统哈希算法(如 MD5、SHA-1)。
确定性输出
  • 所有平台(little/big endian)对相同输入生成一致的哈希值,确保跨环境兼容性。
多平台优化
  • 支持 SSE2、AVX2 等指令集加速,适配不同硬件架构以最大化性能。
使用
  • Python可以提供第三方库的支持,相关的使用也比较简单,只需要安装第三方库即可。

    • pip install xxhash
    • 简单的几个使用示例如下:
    python 复制代码
    from xxhash import xxh3_64_intdigest
    from xxhash import xxh3_128_intdigest
    from xxhash import xxh3_128_hexdigest
    	
    MAX_INT64 = sys.maxsize  # (1 << 63) - 1
    
    def xxh3_64_digest_int(*args: str) -> int:
        return xxh3_64_intdigest("".join(args))
    
    def xxh3_128_digest_int(*args: str) -> int:
        return xxh3_128_intdigest("".join(args))
    
    def xxh3_128_digest_hex(*args: str) -> int:
        return xxh3_128_hexdigest("".join(args))
  • Java同样提供了第三方库的支持,只需要引入相关的依赖包。

    • 引入依赖。开源提供的依赖还是挺多的,比如zero-allocation-hashing、hash4j等。需要注意的是hash4j依赖包后续的版本需要JDK11+。
    java 复制代码
    <!-- ZeroAllocationHashing依赖包 -->
    <dependency>
        <groupId>net.openhft</groupId>
        <artifactId>zero-allocation-hashing</artifactId>
        <version>0.16</version>
    </dependency>
    
    <dependency>
        <groupId>com.dynatrace.hash4j</groupId>
        <artifactId>hash4j</artifactId>
        <version>0.20.0</version>
    </dependency>
    • zero-allocation-hashing依赖包的几个简单使用示例如下:
    java 复制代码
    import lombok.extern.slf4j.Slf4j;
    import net.openhft.hashing.LongHashFunction;
    import net.openhft.hashing.LongTupleHashFunction;
    
    import java.math.BigInteger;
    
    /**
     *
     * @description Digest Utils
     * @author 
     */
    @Slf4j
    public class DigestUtils {
    
        /**
         * xx3 hash 64 bits
         * @param source
         * @return
         */
        public static long xx3Hash64Bits(String source) {
            return LongHashFunction.xx3().hashBytes(source.getBytes());
        }
    
        /**
         * xx3 hash 128 bits
         * @param source
         * @return
         */
        public static BigInteger xx3Hash128Bits(String source) {
            long[] hashes = LongTupleHashFunction.xx128().hashBytes(source.getBytes());
            /**
            // 处理为无符号
            BigInteger unsignedHigh = new BigInteger(1, toBytes(hashes[1]));
            BigInteger unsignedLow = new BigInteger(1, toBytes(hashes[0]));
            **/
            return new BigInteger(1, toBytes(hashes[1])).shiftLeft(64).add(new BigInteger(1, toBytes(hashes[0])));
        }
    
        /**
         * xx3 hash 128 bits digest
         * @param source
         * @return
         */
        public static String xx3Hash128BitsDigest(String source) {
            return xx3Hash128Bits(source).toString(16);
        }
    
        private static byte[] toBytes(long value) {
            byte[] bytes = new byte[8];
            for (int i = 7; i >= 0; i--) {
                bytes[i] = (byte) (value & 0xFF); // 取低8位
                value >>>= 8; // 无符号右移,高位补0
            }
            return bytes;
        }
    
    }
相关推荐
fire-flyer29 分钟前
Spring Boot 源码解析之 Logging
java·spring boot·spring·log4j·logging
CodeCraft Studio31 分钟前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中创建甘特图
python·excel·项目管理·甘特图·aspose·aspose.cells
SaleCoder2 小时前
用Python构建机器学习模型预测股票趋势:从数据到部署的实战指南
开发语言·python·机器学习·python股票预测·lstm股票模型·机器学习股票趋势
KoiHeng2 小时前
部分排序算法的Java模拟实现(复习向,非0基础)
java·算法·排序算法
cui_hao_nan5 小时前
JVM——如何对java的垃圾回收机制调优?
java·jvm
熟悉的新风景7 小时前
springboot项目或其他项目使用@Test测试项目接口配置-spring-boot-starter-test
java·spring boot·后端
心平愈三千疾7 小时前
学习秒杀系统-实现秒杀功能(商品列表,商品详情,基本秒杀功能实现,订单详情)
java·分布式·学习
玩代码7 小时前
备忘录设计模式
java·开发语言·设计模式·备忘录设计模式
BUTCHER57 小时前
Docker镜像使用
java·docker·容器
岁忧8 小时前
(nice!!!)(LeetCode 面试经典 150 题 ) 30. 串联所有单词的子串 (哈希表+字符串+滑动窗口)
java·c++·leetcode·面试·go·散列表