简体   繁体   中英

crypt module not outputting a SHA512 hash as expected

I'm currently using the crypt module in Python to try and create a SHA512 hash in the following manner.

When I run the following line of code from this SO post :

>>> import crypt
>>> crypt.crypt('password', '$6$' + 'salt1234')

Instead of seeing the following output of the salt followed by the SHA512 hash per this SO post :

'$6$salt1234$Zr07alHmuONZlfKILiGKKULQZaBG6Qmf5smHCNH35KnciTapZ7dItwaCv5SKZ1xH9ydG59SCgkdtsTqVWGhk81'

I get the following:

$6FMi11BJFsAc

Below is a screenshot as proof:

在此处输入图像描述

Why is it that I'm unable to obtain the SHA512 hash that I'm expecting?

From https://docs.python.org/3/library/crypt.html :

This module implements an interface to the crypt(3) routine, which is a one-way hash function based upon a modified DES algorithm; see the Unix man page for further details. […]

Notice that the behavior of this module depends on the actual implementation of the crypt(3) routine in the running system. Therefore, any extensions available on the current implementation will also be available on this module.

and from the documentation of the function itself (emphasis mine):

The optional salt is either a string as returned from mksalt() , one of the crypt.METHOD_* values ( though not all may be available on all platforms )

The crypt of the platform you're on probably doesn't support SHA-512. You can confirm this by checking whether crypt.METHOD_SHA512 is in crypt.methods .

>>> crypt.methods
[<crypt.METHOD_CRYPT>]
>>> "\N{CRYING FACE}"
'😢'

One could look at a description of SHAcrypt and make an implementation based on it, or use someone else's .

from passlib.hash import sha512_crypt
sha512_crypt.hash('password')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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