从python绑定到pgcrypto

Oin*_*Oin 7 c python postgresql python-c-api pgcrypto

我想从python中调用一些pgcrypto函数.即px_crypt.我似乎无法弄清楚正确的目标文件链接似乎.

这是我的代码:

#include <Python.h>

#include "postgres.h"

#include "pgcrypto/px-crypt.h"


static PyObject*
pgcrypt(PyObject* self, PyObject* args)
{
    const char* key;
    const char* setting;

    if (!PyArg_ParseTuple(args, "ss", &key, &setting))
        return NULL;

    return Py_BuildValue("s", px_crypt(key, setting, "", 0));
}

static PyMethodDef PgCryptMethods[] =
{
     {"pgcrypt", pgcrypt, METH_VARARGS, "Call pgcrypto's crypt"},
     {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initpypgcrypto(void)
{
     (void) Py_InitModule("pypgcrypto", PgCryptMethods);
}
Run Code Online (Sandbox Code Playgroud)

和gcc命令和输出:

x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/home/ionut/github/postgres/contrib/ -I/usr/include/postgresql/9.4/server/ -I/usr/include/python2.7 -c pypgcrypto.c -o build/temp.linux-x86_64-2.7/pypgcrypto.o
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-z,relro -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/pypgcrypto.o /usr/lib/postgresql/9.4/lib/pgcrypto.so -lpgport -lpq -o build/lib.linux-x86_64-2.7/pypgcrypto.so
Run Code Online (Sandbox Code Playgroud)

错误是:

python -c "import pypgcrypto; print pypgcrypto.pgcrypt('foo', 'bar')"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: /usr/lib/postgresql/9.4/lib/pgcrypto.so: undefined symbol: InterruptPending
Run Code Online (Sandbox Code Playgroud)

Har*_*rry 2

从你的一条评论中我得到了这个......

我想复制 pgcrypto 的行为,以便能够生成与我的数据库中已有的密码哈希相匹配的密码哈希。

您已经可以使用 python 来执行此操作。我不知道你使用的是什么算法,也不应该,这里有两种不同的方法使用 python 生成与 Postgresql 的 pgcrypto 完全相同的哈希值

地穴

=# select crypt('12345678', gen_salt('xdes')), md5('test');
        crypt         |               md5                
----------------------+----------------------------------
 _J9..b8FIoskMdlHvKjk | 098f6bcd4621d373cade4e832627b4f6
Run Code Online (Sandbox Code Playgroud)

这是检查密码的Python...

#!/usr/bin/env python
import crypt
from hmac import compare_digest as compare_hash

def login():
    hash_ = '_J9..OtC82a6snTAAqWg'
    print(compare_hash(crypt.crypt('123456789', hash_), hash_))
    #return True

if __name__ == '__main__':
  login()
Run Code Online (Sandbox Code Playgroud)

MD5

对于 md5,您可以使用 的passlibmd5_crypt 如下...

=# select crypt('12345678', gen_salt('md5')), md5('test');
               crypt                |               md5                
------------------------------------+----------------------------------
 $1$UUVXoPbO$JMA7yhrKvaZcKqoFoi9jl. | 098f6bcd4621d373cade4e832627b4f6
Run Code Online (Sandbox Code Playgroud)

Python 看起来像......

#!/usr/bin/env python
from passlib.hash import md5_crypt

def login():
    hash_ = '$1$kOFl2EuX$QhhnPMAdx2/j2Tsk15nfQ0'
    print(md5_crypt.verify("12345678", hash_))

if __name__ == '__main__':
  login()
Run Code Online (Sandbox Code Playgroud)

河豚

select crypt('12345678', gen_salt('bf')), md5('test');
                            crypt                             |               md5                
--------------------------------------------------------------+----------------------------------
 $2a$06$HLZUXMgqFhi/sl1D697il.lN8OMQFBWR2VBuZ5nTCd59jvGLU9pQ2 | 098f6bcd4621d373cade4e832627b4f6
Run Code Online (Sandbox Code Playgroud)

Python 代码...

#!/usr/bin/env python
from passlib.hash import md5_crypt
from passlib.hash import bcrypt

def blowfish():
    hash_ = '$2a$06$HLZUXMgqFhi/sl1D697il.lN8OMQFBWR2VBuZ5nTCd59jvGLU9pQ2'
    print(bcrypt.verify("12345678", hash_))

if __name__ == '__main__':
  blowfish()
Run Code Online (Sandbox Code Playgroud)

  • 您不需要绑定到 pgcrypto 来执行您需要执行的操作。使用的算法是相同的,或者我的答案中的代码实际上无法工作。如果两种不同的加密算法产生相同的结果,则说明算法存在严重问题,上面的代码证明所使用的算法确实是相同的。 (2认同)