Go 利用OpenSSL 实现whirlpool计算方法
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
本文链接地址:https://const.net.cn/93.html