简体   繁体   中英

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

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

But, when that string stored on postgresql it's converted and become like this:

>>> user.password
'\\x243262243132244a546d7673453238354c754a497a4a334f37644a307559672f52796a486a526c4f443431536f387748544132303077447176555675'

Of course, it won't pass flask-bcrypt check password. I tried to encode the password before save it to database and whenever check password is called I decode it back. And it works.

My question is, what data type should I use? Am I have to use BLOB data type on postgresql? Or is what I've done above ( encode and decode ) the right thing to do? I don't have this issue when using sqlite3.

I had a similar problem and I solved it using BYTEA data type at the Postgresql. I think problem occurs while writing byte array into VARCHAR datatype in Postgre. This might be related to database encoding and the details of that conversion from byte to varchar.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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