BLAKE和BLAKE2是基于丹尼尔·J·伯恩斯坦ChaCha流密码的密码散列函数。与SHA-2一样,有两种不同字大小的变体。BLAKE-256和BLAKE-224使用32位字,分别产生256位和224位的摘要大小,而BLAKE-512和BLAKE-384分别使用64位字,产生512位和384位的摘要大小。在64位的x64和ARM体系结构上运行时,BLAKE2b比SHA-3,SHA-2,SHA-1和MD5更快。BLAKE2的安全性提供类似于SHA-3,优于SHA-2:免疫长度扩展攻击,来自随机预言机的无差异性等。BLAKE的改进版本BLAKE2于2012年12月21日宣布推出。BLAKE3于2020年1月9日宣布推出。

If you are planning to produce checksum for files, you can consider using BLAKE2 is a cryptographic hash function. According to the official site, it is faster than MD5, SHA-1, SHA-2, and SHA-3.

代码:

package main

import (
    "encoding/hex"
    "fmt"
    "io"
    "log"
    "os"

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

func main() {
    fmt.Println("go crypto blake2b_512 demo. blake2b hash file demo. blake2b hash file example.")
    str1 := "https://const.net.cn"
    str2 := "/"
    w, _ := blake2b.New512(nil)
    io.WriteString(w, str1)
    io.WriteString(w, str2)
    hash := w.Sum(nil)
    hashstr := fmt.Sprintf("blake2b_512 = %x ,blake2b_512 len = %d", hash, len(hash))
    fmt.Println(hashstr)

    //blake2b_512 hash file
    hasher, _ := blake2b.New512(nil)

    path := "/const/net/cn/file"
    f, err := os.Open(path)
    if err != nil {
        log.Fatal(err)
    }

    defer f.Close()

    if _, err := io.Copy(hasher, f); err != nil {
        log.Fatal(err)
    }

    filehash := hasher.Sum(nil)
    encodedHex := hex.EncodeToString(filehash[:])
    fmt.Printf("/const/net/cn/file hash = %s", encodedHex)
}

输出:

echo -n "https://const.net.cn/" > /const/net/cn/file
go run .
go crypto blake2b_512 demo. blake2b hash file demo. blake2b hash file example.
blake2b_512 = 4331451a749ca61a9d10f856f5eef9c453c34df8f519d21d2e336037aada9d9f0a234477ea8c44157dcd5349b993c65f5a22ddc8703e30041bba5679b5ecd936 ,blake2b_512 len = 64
/const/net/cn/file hash = 4331451a749ca61a9d10f856f5eef9c453c34df8f519d21d2e336037aada9d9f0a234477ea8c44157dcd5349b993c65f5a22ddc8703e30041bba5679b5ecd936
本文链接地址:https://const.net.cn/67.html

标签: crypto, black2b

添加新评论