标签 sha3 下的文章

“”

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

shake128是sha3,sha3包含SHA3-224 SHA3-256 SHA3-384 SHA3-512 SHAKE128 SHAKE256。
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 := "shake128"
    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 shake128 demo/example.
message digest=6531be44567de471e2a6daa781fd4fad shake128 message digest len=16
echo -n "https://const.net.cn/" |openssl dgst -shake128
(stdin)= 6531be44567de471e2a6daa781fd4fad

shake256是sha3,sha3包含SHA3-224 SHA3-256 SHA3-384 SHA3-512 SHAKE128 SHAKE256。
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 := "shake256"
    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 shake256 demo/example.
message digest=30eec4c2805b5faaedb4c9a87a71c8935675b4e64490305eed0ee55a89135dd6 shake256 message digest len=32
echo -n "https://const.net.cn/" |openssl dgst -shake256
(stdin)= 30eec4c2805b5faaedb4c9a87a71c8935675b4e64490305eed0ee55a89135dd6