Python to PHP password Encryption

Results 1 to 1 of 1
  1. #1
    Newbie KatiaSishost is offline
    MemberRank
    Dec 2018 Join Date
    1Posts

    angry Python to PHP password Encryption

    Python to PHP password Encryption
    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:
    import hashlib
    import hmac
    import base64
    import binascii
    import secrets
    
    class Hasher:
        algorithm = None
        separator = '$'
        
     @classmethod
        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])
        
     @classmethod
        def extract_salt(cls, encoded):
            try: (*_, salt, _) = encoded.split(cls.separator)
            except ValueError: return None
            return salt
        
     @classmethod
        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
        
     @classmethod
        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
        
     @classmethod
        def _encode_impl(cls, password, salt):
            md5 = hashlib.md5()
            md5.update((salt + password).encode('utf-8'))
            return (md5.digest(),)
        
     @classmethod
        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
    
    
    
    
    Example: 123456
    Code:
    pbkdf2_sha256$24000$d3bda36e23c8e4c62ccb$jjHg3zGe+Gdq45Ol0kkXoChJuIyfzEfnxiLqctjRAVQ=
    




    Example : 123456
    Code:
    "pw_md5": "md5$cabcada22ba54552f0ba$8Bbqdbsq+n9UK8uUF2dpkQ=="
    Have any idea how to make it in PHP?
    Last edited by KatiaSishost; 13-12-18 at 08:55 PM.




Advertisement