分类 Go 下的文章

“Go是Google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言。 罗伯特·格瑞史莫、罗勃·派克及肯·汤普逊于2007年9月开始设计Go,稍后伊恩·兰斯·泰勒、拉斯·考克斯加入项目。Go是基于Inferno操作系统所开发的。”

SM3是中华人民共和国政府采用的一种密码散列函数标准,前身为SCH4杂凑算法,由国家密码管理局于2010年12月17日发布,相关标准为“GM/T 0004-2012 《SM3密码杂凑算法》”。2016年,成为中国国家密码标准(GB/T 32905-2016)。
在商用密码体系中,SM3主要用于数字签名及验证、消息认证码生成及验证、随机数生成等,其算法公开,安全性及效率与SHA-256相当。
SM3签名算法收录于ISO/IEC 10118-3:2018《信息安全技术杂凑函数第3部分:专用杂凑函数》
代码:

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

ssl3-md5其实还是md5。。。

openssl list -digest-algorithms

输出:

RSA-MD4 => MD4
RSA-MD5 => MD5
RSA-RIPEMD160 => RIPEMD160
RSA-SHA1 => SHA1
RSA-SHA1-2 => RSA-SHA1
RSA-SHA224 => SHA224
RSA-SHA256 => SHA256
RSA-SHA3-224 => SHA3-224
RSA-SHA3-256 => SHA3-256
RSA-SHA3-384 => SHA3-384
RSA-SHA3-512 => SHA3-512
RSA-SHA384 => SHA384
RSA-SHA512 => SHA512
RSA-SHA512/224 => SHA512-224
RSA-SHA512/256 => SHA512-256
RSA-SM3 => SM3
BLAKE2b512
BLAKE2s256
id-rsassa-pkcs1-v1_5-with-sha3-224 => SHA3-224
id-rsassa-pkcs1-v1_5-with-sha3-256 => SHA3-256
id-rsassa-pkcs1-v1_5-with-sha3-384 => SHA3-384
id-rsassa-pkcs1-v1_5-with-sha3-512 => SHA3-512
MD4
md4WithRSAEncryption => MD4
MD5
MD5-SHA1
md5WithRSAEncryption => MD5
ripemd => RIPEMD160
RIPEMD160
ripemd160WithRSA => RIPEMD160
rmd160 => RIPEMD160
SHA1
sha1WithRSAEncryption => SHA1
SHA224
sha224WithRSAEncryption => SHA224
SHA256
sha256WithRSAEncryption => SHA256
SHA3-224
SHA3-256
SHA3-384
SHA3-512
SHA384
sha384WithRSAEncryption => SHA384
SHA512
SHA512-224
sha512-224WithRSAEncryption => SHA512-224
SHA512-256
sha512-256WithRSAEncryption => SHA512-256
sha512WithRSAEncryption => SHA512
SHAKE128
SHAKE256
SM3
sm3WithRSAEncryption => SM3
ssl3-md5 => MD5
ssl3-sha1 => SHA1
whirlpool

代码:

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-md5"
    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-md5 demo/example.
message digest=4b655b565c09136dd867a7e523371391 ssl3-md5 message digest len=16
echo -n "https://const.net.cn/" |openssl dgst -ssl3-md5
(stdin)= 4b655b565c09136dd867a7e523371391
echo -n "https://const.net.cn/" |openssl dgst -md5
(stdin)= 4b655b565c09136dd867a7e523371391

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

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

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。现在,高级加密标准已然成为对称密钥加密中最流行的算法之一。

代码:

package main

import (
    "bytes"
    "crypto/aes"
    "crypto/cipher"
    "crypto/md5"
    "fmt"
)

func main() {
    fmt.Println("go crypto aes-128-cbc demo/example.")
    message := []byte("https://const.net.cn/")
    //指定密钥h
    key := []byte("1234567812345678")
    //加密
    cipherText := AES_CBC_Encrypt(message, key)
    fmt.Printf("加密后:%x\n", cipherText)
    fmt.Printf("MD5后为:%x\n", md5.Sum(cipherText))
    //解密
    plainText := AES_CBC_Decrypt(cipherText, key)
    fmt.Println("解密后为:", string(plainText))
}

//对明文进行填充
func Padding(plainText []byte, blockSize int) []byte {
    //计算要填充的长度
    n := blockSize - len(plainText)%blockSize
    //对原来的明文填充n个n
    temp := bytes.Repeat([]byte{byte(n)}, n)
    plainText = append(plainText, temp...)
    return plainText
}

//对密文删除填充
func UnPadding(cipherText []byte) []byte {
    //取出密文最后一个字节end
    end := cipherText[len(cipherText)-1]
    //删除填充
    cipherText = cipherText[:len(cipherText)-int(end)]
    return cipherText
}

//AES加密(CBC模式)
func AES_CBC_Encrypt(plainText []byte, key []byte) []byte {
    //指定加密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    //进行填充
    plainText = Padding(plainText, block.BlockSize())
    //指定初始向量vi,长度和block的块尺寸一致
    iv := []byte("1234567812345678")
    //指定分组模式,返回一个BlockMode接口对象
    blockMode := cipher.NewCBCEncrypter(block, iv)
    //加密连续数据库
    cipherText := make([]byte, len(plainText))
    blockMode.CryptBlocks(cipherText, plainText)
    //返回密文
    return cipherText
}

//AES解密(CBC模式)
func AES_CBC_Decrypt(cipherText []byte, key []byte) []byte {
    //指定解密算法,返回一个AES算法的Block接口对象
    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    //指定初始化向量IV,和加密的一致
    iv := []byte("1234567812345678")
    //指定分组模式,返回一个BlockMode接口对象
    blockMode := cipher.NewCBCDecrypter(block, iv)
    //解密
    plainText := make([]byte, len(cipherText))
    blockMode.CryptBlocks(plainText, cipherText)
    //删除填充
    plainText = UnPadding(plainText)
    return plainText
}

输出:

go run .
go crypto aes-128-cbc demo/example.
加密后:7c3d609dad2a0ba771088602d5c3ac8d74c4d9c6be86a925daa3964a4f567d0d
MD5后为:e209153d97b93e25ed7ec8ee05b39eb6
解密后为: https://const.net.cn/

验证:

echo -n "https://const.net.cn/" |openssl enc -aes-128-cbc -K 31323334353637383132333435363738 -iv 31323334353637383132333435363738 | hexdump -C
00000000  7c 3d 60 9d ad 2a 0b a7  71 08 86 02 d5 c3 ac 8d  ||=`..*..q.......|
00000010  74 c4 d9 c6 be 86 a9 25  da a3 96 4a 4f 56 7d 0d  |t......%...JOV}.|
00000020
echo -n "https://const.net.cn/" |openssl enc -aes-128-cbc -K 31323334353637383132333435363738 -iv 31323334353637383132333435363738 | openssl dgst -md5
(stdin)= e209153d97b93e25ed7ec8ee05b39eb6