• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

Python to PHP password Encryption

Initiate Mage
Joined
Dec 13, 2018
Messages
1
Reaction score
0
Hello Guys, im traspassing a register system in Python... to PHP but i have some problems..

This the original script have 2 register...
PBKDF2 and MD5
Code:
[COLOR=#1A1A1A][COLOR=#000000]import hashlib
import hmac
import base64
import binascii
import secrets

class Hasher:
    algorithm = None
    separator = '$'
    
 @[I][B][URL="http://forum.ragezone.com/members/437263.html"]clas[/URL][/B][/I]smethod
    def encode(cls, password, salt = None, *stuff):
        assert password is not None
        if not salt:
            salt = secrets.token_hex(10)
        assert cls.separator not in salt
        (hash, *stuff) = cls._encode_impl(password, salt, *stuff)
        hash = base64.b64encode(hash).decode('ascii').strip()
        return cls.separator.join([cls.algorithm] + stuff + [salt, hash])
    
 @[I][B][URL="http://forum.ragezone.com/members/437263.html"]clas[/URL][/B][/I]smethod
    def extract_salt(cls, encoded):
        try: (*_, salt, _) = encoded.split(cls.separator)
        except ValueError: return None
        return salt
    
 @[I][B][URL="http://forum.ragezone.com/members/437263.html"]clas[/URL][/B][/I]smethod
    def verify(cls, password, encoded):
        try: (algorithm, *stuff, salt, hash) = encoded.split(cls.separator)
        except ValueError: return False
        
        try: hasher = cls._HASHERS[algorithm]
        except KeyError: return False
        
        assert algorithm == hasher.algorithm
        encoded_2 = hasher.encode(password, salt, *stuff)
        return hmac.compare_digest(encoded, encoded_2)
    
    _HASHERS = {}

class PBKDF2PasswordHasher(Hasher):
    algorithm = 'pbkdf2_sha256'
    iterations = 24000
    
 @[I][B][URL="http://forum.ragezone.com/members/437263.html"]clas[/URL][/B][/I]smethod
    def _encode_impl(cls, password, salt, iterations = None):
        if not iterations:
            iterations = cls.iterations
        iterations = int(iterations)
        hash = hashlib.pbkdf2_hmac('sha256', password.encode(), salt.encode(), iterations, None)
        return (hash, str(iterations))
Hasher._HASHERS[PBKDF2PasswordHasher.algorithm] = PBKDF2PasswordHasher

class MD5PasswordHasher(Hasher):
    algorithm = 'md5'
    digest = hashlib.md5
    
 @[I][B][URL="http://forum.ragezone.com/members/437263.html"]clas[/URL][/B][/I]smethod
    def _encode_impl(cls, password, salt):
        md5 = hashlib.md5()
        md5.update((salt + password).encode('utf-8'))
        return (md5.digest(),)
    
 @[I][B][URL="http://forum.ragezone.com/members/437263.html"]clas[/URL][/B][/I]smethod
    def verify_hash(cls, hash_1, encoded):
        try: (_, _, hash) = encoded.split(cls.separator)
        except ValueError: return False
        hash = binascii.hexlify(base64.b64decode(hash)).decode('ascii')
        return hmac.compare_digest(hash_1, hash)
Hasher._HASHERS[MD5PasswordHasher.algorithm] = MD5PasswordHasher

hasher = PBKDF2PasswordHasher
hasher_md5 = MD5PasswordHasher


[/COLOR]
[/COLOR]

Example: 123456
Code:
[COLOR=#1A1A1A][COLOR=#000000]pbkdf2_sha256$24000$d3bda36e23c8e4c62ccb$jjHg3zGe+Gdq45Ol0kkXoChJuIyfzEfnxiLqctjRAVQ=
[/COLOR][/COLOR]




Example : 123456
Code:
[COLOR=#000000]"pw_md5": "md5$cabcada22ba54552f0ba$8Bbqdbsq+n9UK8uUF2dpkQ=="[/COLOR]

Have any idea how to make it in PHP?
 
Last edited:
Back
Top