Go 利用OpenSSL 实现ssl3-sha1计算方法
ssl3-sha1其实还是sha1。。。
代码:
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 := "ssl3-sha1"
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 ssl3-sha1 demo/example.
message digest=405036731104eeb5fae59f5f600f8b4771d93ac5 ssl3-sha1 message digest len=20
echo -n "https://const.net.cn/" |openssl dgst -ssl3-sha1
(stdin)= 405036731104eeb5fae59f5f600f8b4771d93ac5
echo -n "https://const.net.cn/" |openssl dgst -sha1
(stdin)= 405036731104eeb5fae59f5f600f8b4771d93ac5
本文链接地址:https://const.net.cn/92.html