标签 hash 下的文章

“”

包含了md5,sha256,sha512,sha3_512,black2s,black2b等hash算法示例。
代码:

package main

import (
    "crypto/md5"
    "crypto/sha256"
    "crypto/sha512"
    "fmt"

    "golang.org/x/crypto/blake2b"
    "golang.org/x/crypto/blake2s"
    "golang.org/x/crypto/sha3"
)

func getHash(input string, hashType string) string {
    switch hashType {
    case "MD5":
        return fmt.Sprintf("%x", md5.Sum([]byte(input)))
    case "SHA256":
        return fmt.Sprintf("%x", sha256.Sum256([]byte(input)))
    case "SHA512":
        return fmt.Sprintf("%x", sha512.Sum512([]byte(input)))
    case "SHA3_512":
        return fmt.Sprintf("%x", sha3.Sum512([]byte(input)))
    case "BLAKE2s_256":
        return fmt.Sprintf("%x", blake2s.Sum256([]byte(input)))
    case "BLAKE2b_512":
        return fmt.Sprintf("%x", blake2b.Sum512([]byte(input)))
    default:
        return fmt.Sprintf("%x", sha256.Sum256([]byte(input)))
    }
}

func main() {
    fmt.Println("go md5,sha256,sha512,sha3_512,blake2s_256,blake2b_512 hash demo")
    fmt.Println("MD5:", getHash("https://const.net.cn/", "MD5"))
    fmt.Println("SHA256:", getHash("https://const.net.cn/", "SHA256"))
    fmt.Println("SHA512:", getHash("https://const.net.cn/", "SHA512"))
    fmt.Println("SHA3_512:", getHash("https://const.net.cn/", "SHA3_512"))
    fmt.Println("BLAKE2s_256:", getHash("https://const.net.cn/", "BLAKE2s_256"))
    fmt.Println("BLAKE2b_512:", getHash("https://const.net.cn/", "BLAKE2b_512"))
}

输出:

go run .
go md5,sha256,sha512,sha3_512,blake2s_256,blake2b_512 hash demo
MD5: 4b655b565c09136dd867a7e523371391
SHA256: 2249a0aa015fa72b155b297b331bc0e7e34052096c43297d9a2de3a2df38bded
SHA512: dee0877970a321da947b39223110525fa0d9a47d703bce0c5cd39066e25b1078b76d21d83657c35451484db62e5ef80e43a33769b1f626bb8940ad3890e64fd5
SHA3_512: 540bc484763d923575db2aec61fab3502524225f127270301d1cf7f572bf17cd252241322c9df8875df159e732f344cd2cb83991bfc1f3dfbe43c1887f366380
BLAKE2s_256: 5d91ecdf175b8f81bf3cd76c96f7fa1174d4cba8f4bcfa17d183bf4b20f7cdca
BLAKE2b_512: 4331451a749ca61a9d10f856f5eef9c453c34df8f519d21d2e336037aada9d9f0a234477ea8c44157dcd5349b993c65f5a22ddc8703e30041bba5679b5ecd936