我需要在postgresql上用salt散列一些密码,但我还没有找到任何关于如何实现这一点的相关文档.
那么,我如何在postgresql中散列密码(使用一些盐)?
我需要在postgresql上用salt散列一些密码,但我还没有找到任何关于如何实现这一点的相关文档.
那么,我如何在postgresql中散列密码(使用一些盐)?
我问这个问题已经有一段时间了,现在我对密码理论更熟悉了,所以这里有一个更现代的方法:
sudo apt-get install postgresql // (of course)
sudo apt-get install postgresql-contrib libpq-dev // (gets bcrypt, crypt() and gen_salt())
sudo apt-get install php5-pgsql // (optional if you're using postgresql with php)
// Create your database first, then:
cd `pg_config --sharedir` // Move to the postgres directory that holds these scripts.
echo "create extension pgcrypto" | psql -d yOuRdATaBaSeNaMe // enable the pgcrypo extension
比较:通过以下内容传递到现有哈希:
select * from accounts where password_hash = crypt(:pass, password_hash);
//(note how the existing hash is used as its own individualized salt)
创建一个散列:密码和一个大的随机盐:
insert into accounts (password) values crypt(:password, gen_salt('bf', 8));
//(the 8 is the work factor)
PHP5.5及以上版本中有password_*
个函数,允许使用bcrypt进行简单的密码散列(大约是时候了!),下面的版本有一个向后兼容性库.Generally尽管您可能希望确保它安装在您的服务器上,但哈希还是会退回到包装linux系统调用以降低CPU使用率.参见:https://github.com/ircmaxell/password_compat(需要php 5.3.7+)
请注意,使用pg_crypto,密码在从浏览器传输到php和数据库的整个过程中都是明文的.这意味着,如果不小心处理数据库日志(log),可以从查询中记录in plaintext条日志(log).e、 g.拥有postgresql慢速查询日志(log)可以捕获并记录正在进行的登录查询中的密码.
如果可以的话,可以使用php bcrypt,这样可以减少密码未被删除的时间.尽量确保你的linux系统已经安装了bcrypt,它的性能是crypt()
.强烈建议至少升级到PHP5.3.7+,因为php的实现在PHP5.3.0到5.3.6.9之间有一些缺陷,并且在PHP5.2.9及更低版本中,在没有警告的情况下,不适当地返回到断开的DES
.
如果你想/需要postgres散列,安装bcrypt是一个不错的 Select ,因为默认安装的散列已经过时了(md5等).
以下是有关该主题的更多阅读参考: