标签 md5 下的文章

“”

crypto包搜集了常用的密码(算法)常量。
使用不同的hash 算法导入不同的包

const (
    MD4         Hash = 1 + iota // import golang.org/x/crypto/md4
    MD5                         // import crypto/md5
    SHA1                        // import crypto/sha1
    SHA224                      // import crypto/sha256
    SHA256                      // import crypto/sha256
    SHA384                      // import crypto/sha512
    SHA512                      // import crypto/sha512
    MD5SHA1                     // no implementation; MD5+SHA1 used for TLS RSA
    RIPEMD160                   // import golang.org/x/crypto/ripemd160
    SHA3_224                    // import golang.org/x/crypto/sha3
    SHA3_256                    // import golang.org/x/crypto/sha3
    SHA3_384                    // import golang.org/x/crypto/sha3
    SHA3_512                    // import golang.org/x/crypto/sha3
    SHA512_224                  // import crypto/sha512
    SHA512_256                  // import crypto/sha512
    BLAKE2s_256                 // import golang.org/x/crypto/blake2s
    BLAKE2b_256                 // import golang.org/x/crypto/blake2b
    BLAKE2b_384                 // import golang.org/x/crypto/blake2b
    BLAKE2b_512                 // import golang.org/x/crypto/blake2b

)

crypto实现md5的示例

package main

import (
    "crypto/md5"
    "fmt"
    "io"
)

func main() {
    fmt.Println("go-crypto/md5 demo.")
    str := "https://const.net.cn"

    data := []byte(str)
    has := md5.Sum(data)
    md5str1 := fmt.Sprintf("%x", has)
    fmt.Println(md5str1)

    str1 := "https://"
    str2 := "const.net.cn"
    w := md5.New()
    io.WriteString(w, str1)
    io.WriteString(w, str2)
    md5str2 := fmt.Sprintf("%x", w.Sum(nil))

    fmt.Println(md5str2)
}

输出:

go run .
go-crypto/md5 demo.
682d2c63236af6e721794b2988fc1d44
682d2c63236af6e721794b2988fc1d44
echo -n "https://const.net.cn" | md5sum 
682d2c63236af6e721794b2988fc1d44  -
echo -n "https://const.net.cn" | openssl dgst -md5
(stdin)= 682d2c63236af6e721794b2988fc1d44

md5sha1计算方法,就是简单的将数据的md5结果与sha1结果拼接起来。

package main

import (
    "crypto/md5"
    "crypto/sha1"

    "fmt"
    "io"
)

func main() {
    fmt.Println("go crypto md5sha1 demo.")
    str1 := "https://const.net.cn"
    str2 := "/"
    w := md5.New()
    io.WriteString(w, str1)
    io.WriteString(w, str2)

    w1 := sha1.New()
    io.WriteString(w1, str1)
    io.WriteString(w1, str2)

    hashstr := fmt.Sprintf("md5sha1 = %x%x", w.Sum(nil), w1.Sum(nil))
    fmt.Println(hashstr)
}

输出:

go run .
go crypto md5sha1 demo.
md5sha1 = 4b655b565c09136dd867a7e523371391405036731104eeb5fae59f5f600f8b4771d93ac5
echo -n "https://const.net.cn/" | openssl dgst -md5-sha1
(stdin)= 4b655b565c09136dd867a7e523371391405036731104eeb5fae59f5f600f8b4771d93ac5

在计算机网络上,OpenSSL是一个开放源代码的软件库包,应用程序可以使用这个包来进行安全通信,避免窃听,同时确认另一端连线者的身份。这个包广泛被应用在互联网的网页服务器上。
其主要库是以C语言所写成,实现了基本的加密功能,实现了SSL与TLS协议。OpenSSL可以运行在OpenVMS、 Microsoft Windows以及绝大多数类Unix操作系统上(包括Solaris,Linux,Mac OS X与各种版本的开放源代码BSD操作系统)。
代码:

package main

/*
#cgo CFLAGS: -I ./include
#cgo LDFLAGS: -L ./lib -lcrypto -ldl
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/md5.h>
*/
import "C"

import (
    "fmt"
    "os"
    "unsafe"
)

func main() {
    strdigestname := "md5-sha1"
    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 .
message digest=4b655b565c09136dd867a7e523371391405036731104eeb5fae59f5f600f8b4771d93ac5 md5-sha1 message digest len=36
echo -n "https://const.net.cn/" |openssl dgst -md5-sha1
(stdin)= 4b655b565c09136dd867a7e523371391405036731104eeb5fae59f5f600f8b4771d93ac5

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

echo -n "https://const.net.cn" > openssl_test.txt
echo -n "https://const.net.cn" | openssl dgst -md5 

(stdin)= 682d2c63236af6e721794b2988fc1d44

md5sum openssl_test.txt 

682d2c63236af6e721794b2988fc1d44 openssl_test.txt

openssl dgst -md5 openssl_test.txt 

MD5(openssl_test.txt)= 682d2c63236af6e721794b2988fc1d44

openssl dgst -md5 -c openssl_test.txt 

MD5(openssl_test.txt)= 68:2d:2c:63:23:6a:f6:e7:21:79:4b:29:88:fc:1d:44

openssl dgst -md5 -r openssl_test.txt 

682d2c63236af6e721794b2988fc1d44 *openssl_test.txt

openssl dgst -md5 -r *.txt 

682d2c63236af6e721794b2988fc1d44 *openssl_test.txt

openssl dgst -md5 -r *

682d2c63236af6e721794b2988fc1d44 *openssl_test.txt

openssl dgst -md5 -r * > openssl_test.md5
cat openssl_test.md5 

682d2c63236af6e721794b2988fc1d44 *openssl_test.txt

openssl dgst -md5 -c  *

MD5(openssl_test.md5)= f8:ca:f5:9a:35:44:19:c3:95:17:c4:19:47:40:8b:d2
MD5(openssl_test.txt)= 68:2d:2c:63:23:6a:f6:e7:21:79:4b:29:88:fc:1d:44

openssl md5 -c *

MD5(openssl_test.md5)= f8:ca:f5:9a:35:44:19:c3:95:17:c4:19:47:40:8b:d2
MD5(openssl_test.txt)= 68:2d:2c:63:23:6a:f6:e7:21:79:4b:29:88:fc:1d:44