分类 OpenSSL 下的文章

openssl dgst -list
Supported digests:
-blake2b512 -blake2s256 -md4
-md5 -md5-sha1 -ripemd
-ripemd160 -rmd160 -sha1
-sha224 -sha256 -sha3-224
-sha3-256 -sha3-384 -sha3-512
-sha384 -sha512 -sha512-224
-sha512-256 -shake128 -shake256
-sm3 -ssl3-md5 -ssl3-sha1
-whirlpool

用法:
openssl dgst [-digest]
或者
openssl digest
参数:
-c 参数是以冒号分隔HexString.
-hex 是输出16进制字符串,默认就是这个
-binary 是输出二进制的结果
-d 调试信息,没什么用
-out filename 将计算结果输出到文件,默认输出控制台

官方手册地址:https://www.openssl.org/docs/man1.1.1/man1/dgst.html

示例:

echo -n "https://const.net.cn" > openssl_test.txt

openssl blake2s256  openssl_test.txt 

BLAKE2s256(openssl_test.txt)= 1d550a31cf7034f24aac69abb8430474610522a8fc844ef4584d364789890feb

openssl blake2b512  openssl_test.txt 

BLAKE2b512(openssl_test.txt)= 701fd7a8c7104bd71497d3f4d9cbaa82b392aa132dbddc02e323243a60363bb9dbd3d4ae597f9ec862695d52ab85066df22d9c4aca04df11fd9dfe4e2542ab3e

openssl md4  openssl_test.txt 

MD4(openssl_test.txt)= d6dd1d2a616b6a480c6ce417b517734b

openssl md5  openssl_test.txt 

MD5(openssl_test.txt)= 682d2c63236af6e721794b2988fc1d44

openssl md5-sha1  openssl_test.txt 

MD5-SHA1(openssl_test.txt)= 682d2c63236af6e721794b2988fc1d44520fc9bd671e47fd9bd8961d88baacb4d356494a

openssl ripemd  openssl_test.txt 

RIPEMD160(openssl_test.txt)= b4967924b4475514dba56090e82656d710e92d1c

openssl ripemd160  openssl_test.txt 

RIPEMD160(openssl_test.txt)= b4967924b4475514dba56090e82656d710e92d1c

openssl rmd160  openssl_test.txt 

RIPEMD160(openssl_test.txt)= b4967924b4475514dba56090e82656d710e92d1c

openssl sha1  openssl_test.txt 

SHA1(openssl_test.txt)= 520fc9bd671e47fd9bd8961d88baacb4d356494a

openssl sha224  openssl_test.txt 

SHA224(openssl_test.txt)= 4a4c7f39fe288a3125e1095cc940d13efd18a61839611b0ac5865738

openssl sha256  openssl_test.txt 

SHA256(openssl_test.txt)= 8564929a323c97aa80d5be728fc2b7c53241926d138decb4d30f8cae1e36750f

openssl sha3-224  openssl_test.txt 

SHA3-224(openssl_test.txt)= 3f91daa869ff38c9e289d8b20bfb30f35cd24b1ade2d60e117b6f705

openssl sha3-256  openssl_test.txt 

SHA3-256(openssl_test.txt)= 2d2120298f04f9ccf45eb104a1a832b72dc36e92df83697ddf41381937f8a9d7

openssl sha3-384  openssl_test.txt 

SHA3-384(openssl_test.txt)= 9a5de69d2997788656d11b8dbe3007e74c4c116aa4759495e684d6a6cda9adff29e815235fcf110b799ab62ccaa0c029

openssl sha3-512  openssl_test.txt 

SHA3-512(openssl_test.txt)= bcaeaeace4c0f640325b76cda96ad8e527eedacb256afc42d3e8d68842fc694a3355d034c31faa8f8c8b00a869215a3342883f94a06503eb0c350382ae5f430e

openssl sha384  openssl_test.txt 

SHA384(openssl_test.txt)= b13f0e9e1b0a3230346eae998b4e58721afd42926625b3de532c4f13e7d70b62ab51497078de8ef394f34356da39e7bc

openssl sha512  openssl_test.txt 

SHA512(openssl_test.txt)= 8513a9c64d9f897546b9eff11a03182f000dee5df2d3c48dc24a4ac7840ec2e90eae1b99cd05f125e28ca28328b6fda9e287f725164b7c380844071c3a53a025

openssl sha512-224  openssl_test.txt 

SHA512-224(openssl_test.txt)= 1a1b23c9a9b105ef19f0bb5d95077c2fe1995f0fd90729ebc9afe42c

openssl sha512-256  openssl_test.txt

SHA512-256(openssl_test.txt)= 6eefb9b7ea272dd7374749eeeb59b3bf399652918032810e214cd7d86e9e52c6

openssl shake128  openssl_test.txt 

SHAKE128(openssl_test.txt)= e3aeecee8d193be5c58ba3df620abb03

openssl shake256  openssl_test.txt 

SHAKE256(openssl_test.txt)= 1acf0858f828d2fc6c429561c96061c73d200929ef1d05b925d19d71d2030ecb

openssl sm3  openssl_test.txt 

SM3(openssl_test.txt)= bb1318898097df76a8a992ec2ac3dac85914db06dae0851bebd4a5de9a13369f

openssl ssl3-md5  openssl_test.txt 

MD5(openssl_test.txt)= 682d2c63236af6e721794b2988fc1d44

openssl ssl3-sha1  openssl_test.txt 

SHA1(openssl_test.txt)= 520fc9bd671e47fd9bd8961d88baacb4d356494a

openssl whirlpool  openssl_test.txt 

whirlpool(openssl_test.txt)= a57446d6ff2e44fa7a72d7c6739960e6c17f9ea38a8d4032ba163ebe3c0f6d95b7e96c3dbcede63f3e7632462643d3dd10c3272e139bb1cca1873dfc2b86ab3f

BLAKE和BLAKE2是基于丹尼尔·J·伯恩斯坦ChaCha流密码的密码散列函数。与SHA-2一样,有两种不同字大小的变体。BLAKE-256和BLAKE-224使用32位字,分别产生256位和224位的摘要大小,而BLAKE-512和BLAKE-384分别使用64位字,产生512位和384位的摘要大小。在64位的x64和ARM体系结构上运行时,BLAKE2b比SHA-3,SHA-2,SHA-1和MD5更快。BLAKE2的安全性提供类似于SHA-3,优于SHA-2:免疫长度扩展攻击,来自随机预言机的无差异性等。BLAKE的改进版本BLAKE2于2012年12月21日宣布推出。BLAKE3于2020年1月9日宣布推出。

openssl dgst -list

Supported digests:
-blake2b512 -blake2s256 -md4
-md5 -md5-sha1 -ripemd
-ripemd160 -rmd160 -sha1
-sha224 -sha256 -sha3-224
-sha3-256 -sha3-384 -sha3-512
-sha384 -sha512 -sha512-224
-sha512-256 -shake128 -shake256
-sm3 -ssl3-md5 -ssl3-sha1
-whirlpool

openssl dgst -blake2b512 可以简写为 openssl blake2b512,openssl BLAKE2b512

echo -n "https://const.net.cn" > openssl_test.txt 
openssl dgst -blake2b512 openssl_test.txt 

BLAKE2b512(openssl_test.txt)= 701fd7a8c7104bd71497d3f4d9cbaa82b392aa132dbddc02e323243a60363bb9dbd3d4ae597f9ec862695d52ab85066df22d9c4aca04df11fd9dfe4e2542ab3e

openssl dgst -blake2b512 -c openssl_test.txt 

BLAKE2b512(openssl_test.txt)= 70:1f:d7:a8:c7:10:4b:d7:14:97:d3:f4:d9:cb:aa:82:b3:92:aa:13:2d:bd:dc:02:e3:23:24:3a:60:36:3b:b9:db:d3:d4:ae:59:7f:9e:c8:62:69:5d:52:ab:85:06:6d:f2:2d:9c:4a:ca:04:df:11:fd:9d:fe:4e:25:42:ab:3e

openssl dgst -blake2b512 -r openssl_test.txt 

701fd7a8c7104bd71497d3f4d9cbaa82b392aa132dbddc02e323243a60363bb9dbd3d4ae597f9ec862695d52ab85066df22d9c4aca04df11fd9dfe4e2542ab3e *openssl_test.txt

echo -n "https://const.net.cn" | openssl dgst -blake2b512 

(stdin)= 701fd7a8c7104bd71497d3f4d9cbaa82b392aa132dbddc02e323243a60363bb9dbd3d4ae597f9ec862695d52ab85066df22d9c4aca04df11fd9dfe4e2542ab3e

openssl blake2b512 openssl_test.txt 

BLAKE2b512(openssl_test.txt)= 701fd7a8c7104bd71497d3f4d9cbaa82b392aa132dbddc02e323243a60363bb9dbd3d4ae597f9ec862695d52ab85066df22d9c4aca04df11fd9dfe4e2542ab3e

openssl blake2b512 *

BLAKE2b512(openssl_test.md5)= 72cc4d0b342d407a0063b1c5a647f7385c81c8864e52ccb10665bc00742b00e6157826b69bd81c890f7b4545b0f6634f18f97231109980b995a1f333746587d8
BLAKE2b512(openssl_test.txt)= 701fd7a8c7104bd71497d3f4d9cbaa82b392aa132dbddc02e323243a60363bb9dbd3d4ae597f9ec862695d52ab85066df22d9c4aca04df11fd9dfe4e2542ab3e

openssl BLAKE2b512 *

BLAKE2b512(openssl_test.md5)= 72cc4d0b342d407a0063b1c5a647f7385c81c8864e52ccb10665bc00742b00e6157826b69bd81c890f7b4545b0f6634f18f97231109980b995a1f333746587d8
BLAKE2b512(openssl_test.txt)= 701fd7a8c7104bd71497d3f4d9cbaa82b392aa132dbddc02e323243a60363bb9dbd3d4ae597f9ec862695d52ab85066df22d9c4aca04df11fd9dfe4e2542ab3e

echo -n "https://const.net.cn" > openssl_test.txt
echo -n "https://const.net.cn" | openssl dgst -md5 

(stdin)= 682d2c63236af6e721794b2988fc1d44

md5sum openssl_test.txt 

682d2c63236af6e721794b2988fc1d44 openssl_test.txt

openssl dgst -md5 openssl_test.txt 

MD5(openssl_test.txt)= 682d2c63236af6e721794b2988fc1d44

openssl dgst -md5 -c openssl_test.txt 

MD5(openssl_test.txt)= 68:2d:2c:63:23:6a:f6:e7:21:79:4b:29:88:fc:1d:44

openssl dgst -md5 -r openssl_test.txt 

682d2c63236af6e721794b2988fc1d44 *openssl_test.txt

openssl dgst -md5 -r *.txt 

682d2c63236af6e721794b2988fc1d44 *openssl_test.txt

openssl dgst -md5 -r *

682d2c63236af6e721794b2988fc1d44 *openssl_test.txt

openssl dgst -md5 -r * > openssl_test.md5
cat openssl_test.md5 

682d2c63236af6e721794b2988fc1d44 *openssl_test.txt

openssl dgst -md5 -c  *

MD5(openssl_test.md5)= f8:ca:f5:9a:35:44:19:c3:95:17:c4:19:47:40:8b:d2
MD5(openssl_test.txt)= 68:2d:2c:63:23:6a:f6:e7:21:79:4b:29:88:fc:1d:44

openssl md5 -c *

MD5(openssl_test.md5)= f8:ca:f5:9a:35:44:19:c3:95:17:c4:19:47:40:8b:d2
MD5(openssl_test.txt)= 68:2d:2c:63:23:6a:f6:e7:21:79:4b:29:88:fc:1d:44

在使用openssl list 发现了gost

openssl list -digest-commands
blake2b512        blake2s256        gost              md4               
md5               rmd160            sha1              sha224            
sha256            sha3-224          sha3-256          sha3-384          
sha3-512          sha384            sha512            sha512-224        
sha512-256        shake128          shake256          sm3       

测试结果:

echo "" | openssl sha256
(stdin)= 01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b
echo "" | openssl gost
(stdin)= 01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b
echo "https://const.net.cn/" | openssl gost
(stdin)= d1865af543db74fbaae644002ebf838b4ff22b09fd9b8452813736dc1239964b
echo "https://const.net.cn/" | openssl sha256
(stdin)= d1865af543db74fbaae644002ebf838b4ff22b09fd9b8452813736dc1239964b

发现gost hash算法与sha256一样。。。
GOST R 34.11-2012是新的俄罗斯哈希函数标准。从2013年1月开始,GOST R 34.11-2012哈希函数已成为新的俄罗斯联邦标准哈希函数,取代了旧的GOST R 34.11-97。GOST R散列函数可以将最多2 512位的任何消息压缩为256位和512位,分别称为GOST R-256和GOST R-512。

在OpenSSL的obj_mac.h中

#define SN_id_GostR3411_2012_256                "md_gost12_256"
#define LN_id_GostR3411_2012_256                "GOST R 34.11-2012 with 256 bit hash"
#define NID_id_GostR3411_2012_256               982
#define OBJ_id_GostR3411_2012_256               OBJ_id_tc26_digest,2L

#define SN_id_GostR3411_2012_512                "md_gost12_512"
#define LN_id_GostR3411_2012_512                "GOST R 34.11-2012 with 512 bit hash"
#define NID_id_GostR3411_2012_512               983
#define OBJ_id_GostR3411_2012_512               OBJ_id_tc26_digest,3L

In computer science and cryptography, Whirlpool (sometimes styled WHIRLPOOL) is a cryptographic hash function. It was designed by Vincent Rijmen (co-creator of the Advanced Encryption Standard) and Paulo S. L. M. Barreto, who first described it in 2000.

代码:

package main

/*
#cgo CFLAGS: -I ./include
#cgo LDFLAGS: -L ./lib -lcrypto -ldl
#include <stdlib.h>
#include <openssl/evp.h>
*/
import "C"

import (
    "fmt"
    "os"
    "unsafe"
)

func main() {
    strdigestname := "whirlpool"
    fmt.Printf("go OpenSSL cgo %s demo/example.\n", strdigestname)
    strdata := "https://const.net.cn/"
    digestname := []byte(strdigestname)
    md := C.EVP_get_digestbyname((*C.char)(unsafe.Pointer(&digestname[0])))
    if md == nil {
        fmt.Printf("Unknown message digest %s\n", strdigestname)
        os.Exit(1)
    }
    md_value := make([]byte, 128)
    md_len := 0
    data := []byte(strdata)
    mdctx := C.EVP_MD_CTX_new()
    C.EVP_DigestInit(mdctx, md)
    C.EVP_DigestUpdate(mdctx, unsafe.Pointer(&data[0]), C.size_t(len(data)))
    C.EVP_DigestFinal_ex(mdctx, (*C.uchar)(unsafe.Pointer(&md_value[0])), (*C.uint)(unsafe.Pointer(&md_len)))
    C.EVP_MD_CTX_free(mdctx)

    fmt.Printf("message digest=%x %s message digest len=%d\n", md_value[0:md_len], strdigestname, md_len)
}

输出:

go run .
go OpenSSL cgo whirlpool demo/example.
message digest=3b547fd3dc3ba2d24e90cb8c7f3c3a3c2350f9f040b8d18f6da0733c0e047f16017427bcccad6928a4851e8bb302707ac30bb2cdce0c995e3353ea3efbeda621 whirlpool message digest len=64
echo -n "https://const.net.cn/" |openssl dgst -whirlpool
(stdin)= 3b547fd3dc3ba2d24e90cb8c7f3c3a3c2350f9f040b8d18f6da0733c0e047f16017427bcccad6928a4851e8bb302707ac30bb2cdce0c995e3353ea3efbeda621