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-224"
    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-224 demo/example.
message digest=e328606cc1eb2999a2d3f959883c78d6fe4d24d0701eda4d052393ec sha3-224 message digest len=28
echo -n "https://const.net.cn/" |openssl dgst -sha3-224
(stdin)= e328606cc1eb2999a2d3f959883c78d6fe4d24d0701eda4d052393ec

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-256"
    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-256 demo/example.
message digest=8381f50d5d006a28ef44a4e3e8eefc4a8b3c8374f4796a44d5592c0caf48c55f sha3-256 message digest len=32
echo -n "https://const.net.cn/" |openssl dgst -sha3-256
(stdin)= 8381f50d5d006a28ef44a4e3e8eefc4a8b3c8374f4796a44d5592c0caf48c55f

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-384"
    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-384 demo/example.
message digest=3e67e0647f667d67d5e15b4bb7088cd8f6464ba343e90e0ae09541700de98b90c92a55ec6c013f99dea1ea30a47dd6d3 sha3-384 message digest len=48
echo -n "https://const.net.cn/" |openssl dgst -sha3-384
(stdin)= 3e67e0647f667d67d5e15b4bb7088cd8f6464ba343e90e0ae09541700de98b90c92a55ec6c013f99dea1ea30a47dd6d3

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

SHA-2,名称来自于安全散列算法2(英语:Secure Hash Algorithm 2)的缩写,一种密码散列函数算法标准,由美国国家安全局研发,由美国国家标准与技术研究院(NIST)在2001年发布。属于SHA算法之一,是SHA-1的后继者。其下又可再分为六个不同的算法标准,包括了:SHA-224、SHA-256、SHA-384、SHA-512、SHA-512/224、SHA-512/256。
代码:

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 := "sha384"
    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 sha384 demo/example.
message digest=a3021b050f08d069cdda94368141778e2c4a7722faf25b04d64c90da32929e498bd2170880212bbdb0d2babb5219b5ff sha384 message digest len=48
echo -n "https://const.net.cn/" |openssl dgst -sha384
(stdin)= a3021b050f08d069cdda94368141778e2c4a7722faf25b04d64c90da32929e498bd2170880212bbdb0d2babb5219b5ff