简体   繁体   English

bcrypt 和 postgresql。 应该使用什么数据类型?

[英]bcrypt and postgresql. What data type should be used?

On flask-bcrypt extension, the encrypted string is stored as bytes object like this (with python 3.5):在flask-bcrypt 扩展中,加密的字符串存储为字节对象,如下所示(使用python 3.5):

>>> user.password
b'$2b$12$3UutBDuGIrxp2z95alVTp.0HO3qQEtk7O/emR0UC27aNaJKC/WCU.'

But, when that string stored on postgresql it's converted and become like this:但是,当该字符串存储在 postgresql 上时,它会被转换并变成这样:

>>> user.password
'\\x243262243132244a546d7673453238354c754a497a4a334f37644a307559672f52796a486a526c4f443431536f387748544132303077447176555675'

Of course, it won't pass flask-bcrypt check password.当然,它不会通过flask-bcrypt检查密码。 I tried to encode the password before save it to database and whenever check password is called I decode it back.我尝试在将密码保存到数据库之前对其encode ,每当调用检查密码时,我都会将其解码。 And it works.它有效。

My question is, what data type should I use?我的问题是,我应该使用什么数据类型? Am I have to use BLOB data type on postgresql?我必须在 postgresql 上使用BLOB数据类型吗? Or is what I've done above ( encode and decode ) the right thing to do?或者我上面所做的( encodedecode )是正确的吗? I don't have this issue when using sqlite3.使用sqlite3时我没有这个问题。

I had a similar problem and I solved it using BYTEA data type at the Postgresql.我有一个类似的问题,我在 Postgresql 中使用 BYTEA 数据类型解决了它。 I think problem occurs while writing byte array into VARCHAR datatype in Postgre.我认为在 Postgre 中将字节数组写入 VARCHAR 数据类型时会出现问题。 This might be related to database encoding and the details of that conversion from byte to varchar.这可能与数据库编码以及从字节到 varchar 的转换细节有关。

您需要将字节转换为字符串(因为所有字节都是 ascii 字符),然后您可以将其存储在文本(或其他字符类型)列中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM