0

I need to port old Python 2 code to Python 3 and I think I'm messing up with string encoding.

It's a custom password hasher.

I've tried different ways, unsuccessfully, obtaining only errors or wrong results.

This is the Python 2 code which needs to work with Python 3:

from hashlib import sha256 from base64 import b64encode # 32 characters length string SALT = "SQ7HqXQhrOIPEALbI7QhVjZ3DHJGhK18" PLAIN_PASSWORD = "PLAIN_PASSWORD" SALTED_PASSWORD = "%s{%s}" % (PLAIN_PASSWORD, SALT) digest = "" for i in range(100): digest = sha256(digest + SALTED_PASSWORD).digest() print b64encode(digest) 

Output:

Yb0W9H+R7xQDStPfBjKMjFbe05jDPK6OXrdhVWCDJrU= 
1
  • I don't see any python 3 code. Commented May 1, 2020 at 23:57

2 Answers 2

2

Operate on bytes from the beginning:

SALTED_PASSWORD = ("%s{%s}" % (PLAIN_PASSWORD, SALT)).encode() digest = b"" for i in range(100): digest = sha256(digest + SALTED_PASSWORD).digest() print(b64encode(digest).decode()) # Yb0W9H+R7xQDStPfBjKMjFbe05jDPK6OXrdhVWCDJrU= 
Sign up to request clarification or add additional context in comments.

Comments

0
from hashlib import sha256 from base64 import b64encode # 32 characters length string SALT = b"SQ7HqXQhrOIPEALbI7QhVjZ3DHJGhK18" PLAIN_PASSWORD = b"PLAIN_PASSWORD" SALTED_PASSWORD = b"%s{%s}" % (PLAIN_PASSWORD, SALT) digest = b"" for i in range(100): digest = sha256(digest + SALTED_PASSWORD).digest() print(b64encode(digest)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.