Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

491
Visualizações
What datatype would I use to store a sha265 password in a Postgres database

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);
over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda