Skip to main content
 首页 » 编程设计

Crypt 函数的 Python 用法

2024年10月01日1pander-it

如何在Python 3中实现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.

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

这是一个非常基本的问题,我不敢相信我自己找不到答案 - 如果我浪费了你的时间,我深表歉意。

请您参考如下方法:

您将方法作为盐参数传递。来自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) 

引擎盖下变成了:

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