SHA-3第三代安全散列算法(Secure Hash Algorithm 3),之前名为Keccak算法,设计者宣称在 Intel Core 2 的CPU上面,此算法的性能是12.6比特每时钟周期(cycles per byte)。
SHA-3 在2015年8月5日由 NIST 通过 FIPS 202 正式发表。

代码:

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 := "sha3-512"
    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 sha3-512 demo/example.
message digest=540bc484763d923575db2aec61fab3502524225f127270301d1cf7f572bf17cd252241322c9df8875df159e732f344cd2cb83991bfc1f3dfbe43c1887f366380 sha3-512 message digest len=64
echo -n "https://const.net.cn/" |openssl dgst -sha3-512
(stdin)= 540bc484763d923575db2aec61fab3502524225f127270301d1cf7f572bf17cd252241322c9df8875df159e732f344cd2cb83991bfc1f3dfbe43c1887f366380
本文链接地址:https://const.net.cn/83.html

标签: sha3

添加新评论