-3

I am hashing passwords using Sha256.Sum256 then I'm supposed to store the hashed password on the database, but what I get instead is a byte array instead of a hex value. I cannot do ("%x", hash) because that only works on fmt

2
  • 3
    "I cannot do ("%x", hash) because that only works on fmt" I don't understand this. This works, and returns the hexa string representation: fmt.Sprintf("%x", sha256.Sum256([]byte{1, 2})) Commented May 11, 2019 at 11:06
  • 3
    You may also do: hex.EncodeToString(hash[:]), see Why can not convert [Size]byte to string in Go? Commented May 11, 2019 at 11:09

1 Answer 1

1

I think what you are asking is "how do I make a string of the hexadecimal representation of the byte array"

package main

import (
    "crypto/sha256"
    "fmt"
)

func main() {
    password := "abc123"
    sum := sha256.Sum256([]byte(password))
    hexstring := fmt.Sprintf("%x", sum)
    fmt.Println(hexstring)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.