我将使用什么数据类型在Postgres数据库中存储sha265密码

时间:2017-04-28 15:29:02

标签: sql database postgresql

解决了,谢谢那些问我错误的人:

query := fmt.Sprintf("INSERT INTO users(username,password) VALUES('%s','%s') returning uid;", username, hashedPassword)

这意味着我需要将密码存储为bytea

如果我想在Postgres数据库中插入sha265密码,我将使用哪种数据类型。文字似乎对我不起作用。

以下是查询

的代码行
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;
}

对于那些想知道这里是Go代码的人。就像我说的那样,只要密码没有加密就可以正常工作,所以我认为这不会有太大的帮助

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);

这是表格声明

public class ViewMeeting extends Activity implements OnClickListener {

    EditText deleteText;
    Button  deleteButton;
    TextView tv;
    List<MeetingModel> list = new ArrayList<>();
    DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_meeting);
        db = new DatabaseHelper(getApplicationContext());
        deleteText = (EditText) findViewById(R.id.editText6);
        deleteButton = (Button) findViewById(R.id.delete1);
        tv = (TextView) findViewById(textView6);
        deleteButton.setOnClickListener(this);
        list = db.getAllMeetingsList();
        print(list);
    }
//prints details from database table into a textview
   private void print(List<MeetingModel> list) {

        String value = "";
        for(MeetingModel sm : list){
            value = value+" ID: "+sm.id +"\n" +" Title: "+sm.title +"\n"+"             
            Date: "+sm.date +"\n" + " Time: "+sm.time +"\n" +" Location: 
            "+sm.location+"\n"+"\n";
        }
        tv.setText(value);
    }

0 个答案:

没有答案