This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: crypt produces wrong hashes for passwords containing newline character
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: amarahzm, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-09-30 08:41 by amarahzm, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg377694 - (view) Author: Hazem Amara (amarahzm) Date: 2020-09-30 08:41
I am having an issue with crypt library (Lib/ when hashing passwords containing \n character. I am using python 3.8.2 on Linux.  To compare hashed passwords produced by crypt, I used openssl and mkpasswd utilities. 

When generating hashes for password without \n, crypt, openssl and mkpasswd return the same result:

openssl passwd -6 -salt "saltySalt" "password"

mkpasswd -S "saltySalt" -m sha-512 "password"

python3 -c 'import crypt; print(crypt.crypt("password","$6$saltySalt"))'

But when generating hashes for passwords containing \n character, crypt returns a result different from the result returned by openssl and mkpasswd: 

openssl passwd -6 -salt "saltySalt" "password\n"
mkpasswd -S "saltySalt" -m sha-512 "password\n"
python3 -c 'import crypt; print(crypt.crypt("password\n","$6$saltySalt"))'

I did not find a special mention for newline character in the documentation. Thanks for your help.
msg377696 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-09-30 11:22
For openssl and mkpasswd the password does not contain the newline character. It contains a pair of characters "\" and "n". And the crypt module produces the same output for it:

$ python3 -c 'import crypt; print(crypt.crypt(r"password\n","$6$saltySalt"))'
msg377697 - (view) Author: Hazem Amara (amarahzm) Date: 2020-09-30 13:18
Thanks for your answer :)
Date User Action Args
2022-04-11 14:59:36adminsetgithub: 86056
2020-09-30 13:18:56amarahzmsetstatus: open -> closed

messages: + msg377697
stage: resolved
2020-09-30 11:22:58serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg377696
2020-09-30 08:51:31amarahzmsettype: behavior
2020-09-30 08:41:57amarahzmcreate