标签 sha1 下的文章

“”

SHA-1(英语:Secure Hash Algorithm 1,中文名:安全散列算法1)是一种密码散列函数,美国国家安全局设计,并由美国国家标准技术研究所(NIST)发布为联邦资料处理标准(FIPS)。SHA-1可以生成一个被称为消息摘要的160位(20字节)散列值,散列值通常的呈现形式为40个十六进制数。

package main

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

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

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

输出:

go run .
go crypto sha1 demo.
405036731104eeb5fae59f5f600f8b4771d93ac5
echo -n "https://const.net.cn/" | openssl dgst -sha1
(stdin)= 405036731104eeb5fae59f5f600f8b4771d93ac5

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

SHA-1(英语:Secure Hash Algorithm 1,中文名:安全散列算法1)是一种密码散列函数,美国国家安全局设计,并由美国国家标准技术研究所(NIST)发布为联邦资料处理标准(FIPS)。SHA-1可以生成一个被称为消息摘要的160位(20字节)散列值,散列值通常的呈现形式为40个十六进制数。
代码:

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 := "sha1"
    fmt.Printf("go OpenSSL %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 sha1 demo/example.
message digest=405036731104eeb5fae59f5f600f8b4771d93ac5 sha1 message digest len=20
echo -n "https://const.net.cn/" |openssl dgst -sha1
(stdin)= 405036731104eeb5fae59f5f600f8b4771d93ac5

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