简体   繁体   English

生成postgresql用户密码

[英]Generating postgresql user password

I tried to generate password for postgres using hashlib from Python. 我尝试使用Python的hashlibpostgres生成密码。

>>> import hashlib
>>> hashlib.md5("psql123").hexdigest()
2636d1ddc54901f98d011ffe050c0eb7

But postgresql requires md5 prefix, so then 但是postgresql需要md5前缀,所以那么

sudo -u postgres psql
ALTER USER postgres PASSWORD 'md52636d1ddc54901f98d011ffe050c0eb7';

However, authentication would fail if I use psql123 as password. 但是,如果我使用psql123作为密码,身份验证将失败。

If I use passlib , I am fine. 如果我使用passlib ,我很好。 See http://pythonhosted.org/passlib/lib/passlib.hash.postgres_md5.html 请参阅http://pythonhosted.org/passlib/lib/passlib.hash.postgres_md5.html

Doing the following using psql123 as password is okay. 使用psql123作为密码执行以下操作是可以的。

ALTER USER postgres PASSWORD 'md556074e7318bd4cee558faab0678a2fad';

I don't understand what the warning in passlib want to say. 我不明白passlib的警告要说什么。 Is it okay to use this hash for postgres user? 是否可以将这个哈希值用于postgres用户? Also, where in the doc does it say username has to be part of the input? 另外,在文档中它说username必须是输入的一部分?

I assume this is why postgres can't understand the result from hashlib . 我假设这就是为什么postgres无法理解hashlib的结果。 As a LDAP user, I can generate a password in the shell. 作为LDAP用户,我可以在shell中生成密码。 Does postgres has a built-in command to do that? postgres有内置命令吗? Does psycopg2 has that? psycopg2有吗? It looks like it doesn't. 它看起来没有。

Postgres' password hash is very close to what you did, it just needs the username to be included as follows: Postgres的密码哈希非常接近你所做的,它只需要包含用户名,如下所示:

 pghash = "md5" + hashlib.md5(password + username).hexdigest()

AFAIK, the postgres docs don't really document this hash format at all, and seem to assume admins will rarely deal with these hashes directly :( There are no builtin methods for generating these hashes that I know of. If the password provided to the ALTER USER command doesn't conform to the postgres hash format, it assumes the password hasn't been hashed, and takes care of that internally - per the docs for CREATE ROLE 's ENCRYPTED keyword. (IMHO this is a flawed behavior, because if a hash depends on the username, it means hashes can't be copied and pasted between different accounts, break when the account is renamed, and (guessing entropy wise) only has ~6 bits of effective salt). AFAIK,postgres文档根本没有真正记录这种哈希格式,并且似乎假设管理员很少直接处理这些哈希:(没有内置的方法来生成我知道的这些哈希。如果密码提供给ALTER USER命令不符合postgres哈希格式,它假设密码没有经过哈希处理,并在内部处理 - 根据CREATE ROLE的ENCRYPTED关键字的文档。(恕我直言,这是一个有缺陷的行为,因为如果哈希取决于用户名,则意味着哈希不能在不同帐户之间复制和粘贴,在重命名帐户时中断,并且(猜测熵)只有~6位有效盐)。

The warning at the top of passlib's documentation for the hash could probably be clearer. passlib的hash文档顶部的警告可能更清晰。 It was meant to warn people browsing through the passlib documentation that 1) this hash was horribly insecure, 2) that they shouldn't adopt it for use in their own applications, and 3) that it was only fit for the purpose of working with postgres user accounts, since it's the strongest (and only) hash format postgres supports for it's own accounts. 它的目的是警告浏览passlib文档的人1)这个哈希是非常不安全的,2)他们不应该将它用于他们自己的应用程序,3)它只适合于使用的目的postgres用户帐户,因为它是postgres支持自己帐户的最强(也是唯一)哈希格式。

(If you're trying to use postgres to hash passwords for your own application's user accounts, I'd strongly second Clodoaldo's recommendation to use bcrypt by way of the pgcrypto extension). (如果您尝试使用postgres来为您自己的应用程序的用户帐户哈希密码,我强烈推荐Clodoaldo建议通过pgcrypto扩展名来使用bcrypt)。

alter user postgres ENCRYPTED password 'psql123';

For other uses use the pgcrypto module. 对于其他用途,请使用pgcrypto模块。

create table "user" (name text, password_hash text);

insert into "user" (name, password_hash) values
('u1', crypt('psql123', gen_salt('bf')));

select * from "user";
 name |                        password_hash                         
------+--------------------------------------------------------------
 u1   | $2a$06$SeH4u4aRtT2Zr39er4eSiONT/0IBQHYMbQXn2RauPJKCYdNX1.58G

select name, password_hash = crypt('psql123', password_hash)
from "user"
;
 name | ?column? 
------+----------
 u1   | t

Install it as super user logged in the target database: 以超级用户身份登录目标数据库安装:

create extension pgcrypto;

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

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