go加密算法:非对称加密(二)--Hash

Posted on 2018-12-24 15:25  eilinge  阅读(559)  评论(0编辑  收藏  举报
关于一些加密算法的应用和信息,可以在以下博客中查找到:
https://www.cnblogs.com/charlesblc/p/6130141.html
// MyHash
package main

import (
    "crypto/md5"
    "crypto/sha512"
    "encoding/hex"
    "fmt"
    "io"
)

func SumGeniHash(src []byte) string {
    newsrc := md5.Sum(src)
    mysrc := hex.EncodeToString(newsrc[:])
    //mysrc := fmt.Sprintf("%x", newsrc)
    return mysrc
}

func NewGeniHash(src []byte) string {
    myHash := md5.New()
    //2.添加数据m
    io.WriteString(myHash, string(src))
    //myHash.Write(src)

    res := myHash.Sum(nil)

    return hex.EncodeToString(res[:])
}

func Sumsha512Hash(src []byte) string {
    newsrc := sha512.Sum512(src)
    mysrc := hex.EncodeToString(newsrc[:])
    //mysrc := fmt.Sprintf("%x", newsrc)
    return mysrc
}

func Newsha512Hash(src []byte) string {
    myHash := sha512.New()
    //2.添加数据m
    io.WriteString(myHash, string(src))
    //myHash.Write(src)

    res := myHash.Sum(nil)

    return hex.EncodeToString(res[:])
}

func main() {

    scr := []byte("少壮不努力,老大徒伤悲")
    fmt.Println(SumGeniHash(scr))
    //fmt.Println(src2)
    fmt.Println(NewGeniHash(scr))

    fmt.Println(Sumsha512Hash(scr))
    fmt.Println(Sumsha512Hash(scr))

}
运行的结果是:
/*
6e534d701bc50487b9f7b14ac465ae93
6e534d701bc50487b9f7b14ac465ae93
b82a63eea6aaa48d5b5d0debe525a5e290caf0e1444d6f3b56ce6f89ae58987f91ad8a56a7d8fd22f17e8ad7566eeec4c532a87428488c68b5bd4ef84a409053
b82a63eea6aaa48d5b5d0debe525a5e290caf0e1444d6f3b56ce6f89ae58987f91ad8a56a7d8fd22f17e8ad7566eeec4c532a87428488c68b5bd4ef84a409053
*/