zoukankan      html  css  js  c++  java
  • 不同程序语言处理加密算法的性能对比(PHP/Golang/NodeJS)

    首先上我最常用且最易用的PHP来做下测试

    <?php
    
    function test_encrypt($fun, $max) {
        $begin = microtime(TRUE);
        $pwdpre = time();
        for ($i = 1; $i <= $max; $i++) {
            $fun($pwdpre . $i);
        }
        echo $fun . ' => ' . number_format((microtime(TRUE) - $begin) * 1000, 2) . ' ms' . "
    ";
    }
    
    test_encrypt('crc32', 1000 * 1000); //165.52ms
    test_encrypt('md5', 1000 * 1000); //390.99ms
    test_encrypt('sha1', 1000 * 1000); //488.63.ms

    下面贴上很多人觉得性能不错的Golang来做下测试

    package main
    
    import (
        "crypto/md5"
        "crypto/sha1"
        "encoding/hex"
        "fmt"
        "hash/crc32"
        "strconv"
        "time"
    )
    
    func main() {
        test_encrypt("CRC32", 1000*1000) // 162.1024 ms
        test_encrypt("MD5", 1000*1000)   // 525.0696 ms
        test_encrypt("SHA1", 1000*1000)  // 586.758 ms
    }
    
    func test_encrypt(fun string, max int) {
        pwdpre := strconv.FormatInt(time.Now().Unix(), 10)
        begin := time.Now().UnixNano()
        for i := 1; i <= max; i++ {
            switch fun {
            case "CRC32":
                CRC32(pwdpre + strconv.Itoa(i))
            case "MD5":
                MD5(pwdpre + strconv.Itoa(i))
            case "SHA1":
                SHA1(pwdpre + strconv.Itoa(i))
            }
    
        }
        end := time.Now().UnixNano()
        fmt.Println(fun, "=>", float64(end-begin)/1e6, "ms")
    }
    func MD5(str string) string {
        // 取得16进制文本MD5
        h := md5.New()
        h.Write([]byte(str))
        return hex.EncodeToString(h.Sum(nil))
    }
    
    func SHA1(str string) string {
        // 取得16进制文本SHA1
        c := sha1.New()
        c.Write([]byte(str))
        return hex.EncodeToString(c.Sum(nil))
    }
    
    func CRC32(str string) uint32 {
        // 取得无符号32位整型CRC32
        return crc32.ChecksumIEEE([]byte(str))
    }
  • 相关阅读:
    弹飞绵羊
    POJ 3308
    狼抓兔子
    块状链表题*1
    块状链表
    双向链表
    Linux入职基础-1.2_U盘安装RedHat5具体步骤
    Linux入职基础-1.1_国内开源的主要镜像站
    VS.NET(C#)--2.9_HTML服务器控件案例
    VS2015按钮方法
  • 原文地址:https://www.cnblogs.com/xiangxisheng/p/14152884.html
Copyright © 2011-2022 走看看