Python对Crypt函数的使用

2024-06-09 22:46:09 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在python3中实现crypt选项?在

我理解其用途是:

Hash = crypt.crypt(password, salt)

但是,该函数有一组不同的哈希函数。根据文件:

crypt.METHOD_SHA512 A Modular Crypt Format method with 16 character salt and 86 character hash. This is the strongest method.

crypt.METHOD_SHA256 Another Modular Crypt Format method with 16 character salt and 43 character hash.

crypt.METHOD_MD5 Another Modular Crypt Format method with 8 character salt and 22 character hash.

crypt.METHOD_CRYPT The traditional method with a 2 character salt and 13 characters of hash. This is the weakest method.

同样,我的问题是如何选择函数使用的算法?在

这是一个非常基本的问题,我不能相信我自己没有找到答案-如果我浪费你的时间,我道歉。在


Tags: andthe函数formatiswithhashthis
3条回答

将方法作为salt参数传递。从crypt function docstring

If salt is not specified or is None, the strongest available method will be selected and a salt generated. Otherwise, salt may be one of the crypt.METHOD_* values, or a string as returned by crypt.mksalt().

例如:

crypt.crypt("password", crypt.METHOD_SHA512)

在引擎盖下面变成:

^{pr2}$

改编自Red Hat solution和@Leonard_Saracini答案(删除反斜杠使其成为一行行):

python3 -c 'import crypt,getpass,sys; \
print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))'

从2018年8月起,这应该是安全的。在

使用bash:$python3 -c "import crypt; print(crypt.crypt('password', '\$6\$saltstring'))"

$6$用于SHA512,如果您使用像我这样的bash终端,$必须由\转义

相关问题 更多 >