go的crypto对文件进行加密

最近在做go 加解密, 做个小的笔记
package main

import (
    "crypto/hmac"
    "crypto/sha256"
    "fmt"
    "io"
)

func main() {
    c := getSha256Code("[email protected]")
    fmt.Println(c)

    c = getHmacCode("[email protected]")
    fmt.Println(c)
}

func getHmacCode(s string) string {
    h := hmac.New(sha256.New, []byte("ourkey"))
    io.WriteString(h, s)
    return fmt.Sprintf("%x", h.Sum(nil))
}

func getSha256Code(s string) string {
    h := sha256.New()
    h.Write([]byte(s))
    return fmt.Sprintf("%x", h.Sum(nil))
}

输出:

973dfe463ec85785f5f95af5ba3906eedb2d931c24e69824a89ea65dba4e813b 
453159a62580804892cc90a27dfb8bfdf2309107336445dcfd0186674111ee71

你可能感兴趣的:(黑暗终章学习笔记)