How to perform SHA1 hash operation on strings in Golang

package main
import (
     "crypto/md5"
     "crypto/sha1"
     "fmt"
     "io"
)
// MD5 hash the string
func a(data string) string {
     t := md5.New()
     io.WriteString(t, data)
     return fmt.Sprintf("%x", t.Sum(nil))
}
// SHA1 hash the string
func b(data string) string {
     t := sha1.New()
     io.WriteString(t, data)
     return fmt.Sprintf("%x", t.Sum(nil))
}
func main() {
     var data string = "abc"
     fmt.Printf("MD5 : %s\n", a(data))
     fmt.Printf("SHA1 : %s\n", b(data))
}

Leave a Reply

Your email address will not be published.

en_USEnglish