Examples Filter
package main

import (
    "crypto/sha1"
    "fmt"
)

func main() {
    s := "sha1 this string"

    h := sha1.New()

    // Write expects bytes. If you have a string s, use []byte(s) to coerce it to bytes.
    h.Write([]byte(s))

    // This gets the finalized hash result as a byte slice.
    // The argument to Sum can be used to append to an existing byte slice: it usually isn’t needed.
    bs := h.Sum(nil)

    // SHA1 values are often printed in hex, for example in git commits.
    // Use the %x format verb to convert a hash results to a hex string.
    fmt.Println(s)
    fmt.Printf("%x\n", bs)
}
package main

import (
    "crypto/md5"
    "fmt"
)

func main() {
    s := "md5 this string"

    h := md5.New()

    // Write expects bytes. If you have a string s, use []byte(s) to coerce it to bytes.
    h.Write([]byte(s))

    // This gets the finalized hash result as a byte slice.
    // The argument to Sum can be used to append to an existing byte slice: it usually isn’t needed.
    bs := h.Sum(nil)

    // Use the %x format verb to convert a hash results to a hex string.
    fmt.Println(s)
    fmt.Printf("%x\n", bs)
}
package main

import (
    "crypto/sha256"
    "fmt"
)

func main() {
    s := "sha256 this string"

    h := sha256.New()

    // Write expects bytes. If you have a string s, use []byte(s) to coerce it to bytes.
    h.Write([]byte(s))

    // This gets the finalized hash result as a byte slice.
    // The argument to Sum can be used to append to an existing byte slice: it usually isn’t needed.
    bs := h.Sum(nil)

    // Use the %x format verb to convert a hash results to a hex string.
    fmt.Println(s)
    fmt.Printf("%x\n", bs)
}
Last Run  :
sha256 this string
1af1dfa857bf1d8814fe1af8983c18080019922e557f15a8a0d3db739d77aacb
package main

import (
	"fmt"

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

func HashPassword(password string) (string, error) {
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
	return string(bytes), err
}

func CheckPasswordHash(password, hash string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
	return err == nil
}

func main() {
	password := "secret"
	hash, _ := HashPassword(password) // ignore error for the sake of simplicity

	fmt.Println("Password:", password)
	fmt.Println("Hash:    ", hash)

	match := CheckPasswordHash(password, hash)
	fmt.Println("Match:   ", match)
}
Last Run  :
Password: secret
Hash:     $2a$14$w1tKUZnMxVrGozhFfMMODummhzHop3kPTEIwMWODnctTWGKT7d8hu
Match:    true