Solved, thank you for those asking me what error it was:
pq: invalid byte sequence for encoding "UTF8": 0xe3 0xb0 0xc4
This meant I needed to store the password as bytea
If I want to insert a sha265 password in a Postgres database what datatype would I use. Text does not seem to be working for me.
Here is the line of code with the query
query := fmt.Sprintf("INSERT INTO users(username,password) VALUES('%s','%s') returning uid;", username, hashedPassword)
For those wondering here is the Go code. Like I said though, it works fine as long as the password isn't encrypted so I don't think this will be of much help
func hashPassword(password string) string {
h := sha256.New()
passwordBytes := []byte(password)
passwordHashed := h.Sum(passwordBytes)
return string(passwordHashed)
}
func createUser(username string, password string, passwordConfirm string) bool {
dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
DB_USER, DB_PASSWORD, DB_NAME)
db, err := sql.Open("postgres", dbinfo)
checkErr(err)
defer db.Close()
hashedPassword := hashPassword(password)
hashedPasswordConfirm := hashPassword(passwordConfirm)
if hashedPassword != hashedPasswordConfirm {
return false
}
var lastInsertId int
query := fmt.Sprintf("INSERT INTO users(username,password) VALUES('%s',$trick$%s$trick$) returning uid;", username, hashedPassword)
err = db.QueryRow(query).Scan(&lastInsertId)
fmt.Println(lastInsertId)
if err != nil {
return false;
}
return true;
}
Here is the table declaration
CREATE TABLE users
(
uid SERIAL,
username text NOT NULL UNIQUE,
password text NOT NULL,
weekly_goals bytea,
CONSTRAINT users_pkey PRIMARY KEY (uid)
) WITH (OIDS=FALSE);