guava计算md5、sha1和murmurhash

13 篇文章 0 订阅
本文介绍了如何利用Guava库在Java中进行MD5、SHA1哈希计算以及MurmurHash的使用,提供了相关代码示例。
摘要由CSDN通过智能技术生成

下面是我用来计算MD5和sha1的一段使用guava的代码。

    public static void test002() throws Exception {
        String msg = StringUtils.repeat('-', 200);
        int testCount = 100000;
        long sha1Time = 0L;
        long md5Time = 0L;
        long murmur3Time = 0L;
        double sha1Average = 0D;
        double md5Average = 0D;
        double murmur3Average = 0D;

        long one = System.currentTimeMillis();
        HashFunction sha1 = Hashing.sha1();
        for (int i = 0; i < testCount; i++) {
            Stopwatch w = Stopwatch.createStarted();
            HashCode hashCode = sha1.hashString(msg + i, Charsets.UTF_8);
            String str = hashCode.toString();
            System.out.println(String.format("sh1's hashCode:%s,length:%s,it consumes:%s", str, str.length(),
                    w));
        }
        long two = System.currentTimeMillis();
        sha1Time = two - one;
        HashFunction md5 = Hashing.md5();
        for (int i = 0; i < testCount; i++) {
            Stopwatch w = Stopwatch.createStarted();
            HashCode md5HashCode = md5.hashString(msg + i, Charsets.UTF_8);
            String md5HashCodeStr = md5HashCode.toString();
            System.out.println(String.format("md5's hashCode:%s,length:%s,it consumes:%s", md5HashCodeStr,
                    md5HashCodeStr.length(), w));
        }
        long three = System.currentTimeMillis();
        md5Time = three - two;

        HashFunction murmur3 = Hashing.murmur3_32();
        Set<String> set = Sets.newHashSet();
        for (int i = 0; i < testCount; i++) {
            Stopwatch w = Stopwatch.createStarted();
            HashCode murmur3HashCode = murmur3.hashString(msg + i, Charsets.UTF_8);
            String murmur3HashCodeStr = murmur3HashCode.toString();
            System.out.println(String.format("murmur3's hashCode:%s,length:%s,it consumes:%s",
                    murmur3HashCodeStr, murmur3HashCodeStr.length(), w));
            set.add(murmur3HashCodeStr);
        }
        long four = System.currentTimeMillis();
        murmur3Time = four - three;

        sha1Average = sha1Time / testCount;
        md5Average = md5Time / testCount;
        murmur3Average = murmur3Time / testCount;

        System.out.println("set size : " + set.size());
        System.out
                .println(String
                        .format("sha1 sum time:%s seconds,average time:%s ms\nmd5 sum time:%s seconds,average time:%s ms\nmurmur3 sum time:%s seconds,average time:%s ms",
                                changeToSecond(sha1Time), sha1Average, changeToSecond(md5Time), md5Average,
                                changeToSecond(murmur3Time), murmur3Average));

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值