o
    5h#                     @   s   d Z ddlmZmZ ddlmZ ddlZddlZee	Z
ddlmZmZ ddlmZmZmZmZmZ ddlmZ ddlm  mZ dd	gZdddZdZG dd dejejZG dd dej ej!Z"dS )z3passlib.handlers.oracle - Oracle DB Password Hashes    )hexlify	unhexlify)sha1N)
to_unicode	xor_bytes)irangeuuascii_to_strunicodestr_to_uascii)des_encrypt_block	oracle10g	oracle11g               c                 C   sT   ||t | d  7 }|}tdt |dD ]}t||||d  }t| |}q|S )a  performs des-cbc encryption, returns only last block.

    this performs a specific DES-CBC encryption implementation
    as needed by the Oracle10 hash. it probably won't be useful for
    other purposes as-is.

    input value is null-padded to multiple of 8 bytes.

    :arg key: des key as bytes
    :arg value: value to encrypt, as bytes.
    :param iv: optional IV
    :param pad: optional pad byte

    :returns: last block of DES-CBC encryption of all ``value``'s byte blocks.
       r   )lenr   r   r   )keyvalueivpadhashoffsetchunk r   k/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/passlib/handlers/oracle.pydes_cbc_encrypt   s   r   s   #Egc                   @   s2   e Zd ZdZd ZejZdZe	dd Z
dd ZdS )oracle10a  This class implements the password hash used by Oracle up to version 10g, and follows the :ref:`password-hash-api`.

    It does a single round of hashing, and relies on the username as the salt.

    The :meth:`~passlib.ifc.PasswordHash.hash`, :meth:`~passlib.ifc.PasswordHash.genhash`, and :meth:`~passlib.ifc.PasswordHash.verify` methods all require the
    following additional contextual keywords:

    :type user: str
    :param user: name of oracle user account this password is associated with.
       c                 C   s   |  S )N)upper)clsr   r   r   r   
_norm_hashI   s   zoracle10._norm_hashc                 C   s\   t |tr
|d}t| jddd}||  d}tt|}t||}t	|d S )Nutf-8user)paramz	utf-16-beascii)

isinstancebytesdecoder   r#   r   encoder   ORACLE10_MAGICr   )selfsecretr#   inputr   r   r   r   _calc_checksumM   s   



zoracle10._calc_checksumN)__name__
__module____qualname____doc__nameuh	HEX_CHARSchecksum_charschecksum_sizeclassmethodr!   r.   r   r   r   r   r   4   s    
r   c                   @   s^   e Zd ZdZd ZdZdZejZ	d Z
ZejZeedejZedd Zdd	 Zd
d ZdS )oracle11aE  This class implements the Oracle11g password hash, and follows the :ref:`password-hash-api`.

    It supports a fixed-length salt.

    The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords:

    :type salt: str
    :param salt:
        Optional salt string.
        If not specified, one will be autogenerated (this is recommended).
        If specified, it must be 20 hexadecimal characters.

    :type relaxed: bool
    :param relaxed:
        By default, providing an invalid value for one of the other
        keywords will result in a :exc:`ValueError`. If ``relaxed=True``,
        and the error can be corrected, a :exc:`~passlib.exc.PasslibHashWarning`
        will be issued instead. Correctable errors include
        ``salt`` strings that are too long.

        .. versionadded:: 1.6
    )salt(      z/^S:(?P<chk>[0-9a-f]{40})(?P<salt>[0-9a-f]{20})$c                 C   sH   t |dd}| j|}|stj| |dd\}}| || dS )Nr%   r   r:   chk)r:   checksum)r   _hash_regexmatchr4   excInvalidHashErrorgroupr   )r    r   mr:   r=   r   r   r   from_string   s   zoracle11.from_stringc                 C   s(   | j }td| | j f }t|S )NzS:%s%s)r>   r   r   r:   r	   )r+   r=   r   r   r   r   	to_string   s   zoracle11.to_stringc                 C   s<   t |tr
|d}t|t| jd  }t| S )Nr"   r%   )	r&   r
   r)   r   r   r:   	hexdigestr   r   )r+   r,   r=   r   r   r   r.      s   

zoracle11._calc_checksumN)r/   r0   r1   r2   r3   setting_kwdsr7   r4   UPPER_HEX_CHARSr6   min_salt_sizemax_salt_size
salt_charsrecompiler   Ir?   r8   rE   rF   r.   r   r   r   r   r9   h   s    
r9   )r   r   )#r2   binasciir   r   hashlibr   rM   logging	getLoggerr/   logpasslib.utilsr   r   passlib.utils.compatr   r   r	   r
   r   passlib.crypto.desr   passlib.utils.handlersutilshandlersr4   __all__r   r*   HasUserContextStaticHandlerr   HasSaltGenericHandlerr9   r   r   r   r   <module>   s     
4