To create a secure password in Go, you should not store the raw password in the database because if the database is compromised, the attacker can easily read all the passwords in plain text. Instead, you should store a salted hash of the password.
Here's an example code to hash a password using Go's crypto
package:
main.go671 chars30 lines
To verify a password, you need to retrieve the salt from the database for the user and generate the hash of the password with the retrieved salt, and compare the hash with the stored hashed password.
main.go597 chars20 lines
Keep in mind that this is just an example, and there are more secure and efficient ways to store passwords, such as using a dedicated password hashing library like bcrypt
or scrypt
.
gistlibby LogSnag