MD5与SHA1的摘要算法耗时对比

原创
2022/05/14 10:56
阅读数 1.2K

背景:

项目中,有个需求是:需要对一个对象判断是否有属性变动,既页面上是否有修改过此对象的数据。因为此对象很大、属性很多,且层级超过了4层,所以采用的技术方案是,将页面请求的对象生成摘要reqIndex,再将db中的对象生成dbIndex,之后对比reqIndex和dbIndex是否相同来判断有没有变动。

过程:

因为对象层级很深,每个层级都需要对比,所以过程中可能需要多次生成摘要index,为了提升性能,所有对md5和sha1算法做耗时对比,算出耗时较低的算法

对比结果

sha1算法耗时要比md5算法低

对比过程:

环境:mac m1 8G

测试结果输出中的耗时单位:毫秒

测试内容:ASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwqASDQWEVSAFASDAasdasfwq


内容长度:2200, md5和sha1分别进行:10次的耗时:
md5: 90
sha1: 4
内容长度:2200, md5和sha1分别进行:10次的耗时:
md5: 88
sha1: 5

内容长度:2200, md5和sha1分别进行:100次的耗时:
md5: 109
sha1: 7
内容长度:2200, md5和sha1分别进行:100次的耗时:
md5: 103
sha1: 6

内容长度:2200, md5和sha1分别进行:1000次的耗时:
md5: 129
sha1: 8
内容长度:2200, md5和sha1分别进行:1000次的耗时:
md5: 118
sha1: 10

测试内容:MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比MD5与SHA1的摘要算法耗时对比

内容长度:1700, md5和sha1分别进行:10次的耗时:
md5: 82
sha1: 5
内容长度:1700, md5和sha1分别进行:10次的耗时:
md5: 91
sha1: 5

内容长度:1700, md5和sha1分别进行:100次的耗时:
md5: 99
sha1: 6
内容长度:1700, md5和sha1分别进行:100次的耗时:
md5: 89
sha1: 5

内容长度:1700, md5和sha1分别进行:1000次的耗时:
md5: 78
sha1: 4
内容长度:1700, md5和sha1分别进行:1000次的耗时:
md5: 92
sha1: 7

源码:

	public static void main(String[] args) {
        String subContent = "MD5与SHA1的摘要算法耗时对比";
        String content = generateTestContent(100, subContent);
        System.out.println(content);
        int count = 1000;
        System.out.println("内容长度:" + content.length() + ", md5和sha1分别进行:" + count + "次的耗时:");
        md5(content, 10);
        sha1(content, 10);
    }

    static String generateTestContent(int chars, String subContent) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < chars; i++) {
            stringBuilder.append(subContent);
        }
        return stringBuilder.toString();
    }


    static void md5(String content, int count) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            DigestUtil.md5(content);
        }
        long castTime = System.currentTimeMillis() - startTime;
        System.out.println("md5: " + castTime);
    }

    static void sha1(String content, int count) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            DigestUtil.sha1(content);
        }
        long castTime = System.currentTimeMillis() - startTime;
        System.out.println("sha1: " + castTime);
    }

依赖包:

<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-all</artifactId>
	<version>5.8.0</version>
</dependency>
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部