
     h"!                     p   d Z ddlmZmZ ddlmZ ddlZddlZ ej                  e	      Z
ddlmZ ddlmZ ddlmZmZmZ ddlmc mZ dd	gZd
 ZdZ ed      Zd Zd Z G d dej:                  ej<                  ej>                        Z  G d d	ej:                  ej<                  ej>                        Z!y)a  passlib.handlers.mssql - MS-SQL Password Hash

Notes
=====
MS-SQL has used a number of hash algs over the years,
most of which were exposed through the undocumented
'pwdencrypt' and 'pwdcompare' sql functions.

Known formats
-------------
6.5
    snefru hash, ascii encoded password
    no examples found

7.0
    snefru hash, unicode (what encoding?)
    saw ref that these blobs were 16 bytes in size
    no examples found

2000
    byte string using displayed as 0x hex, using 0x0100 prefix.
    contains hashes of password and upper-case password.

2007
    same as 2000, but without the upper-case hash.

refs
----------
https://blogs.msdn.com/b/lcris/archive/2007/04/30/sql-server-2005-about-login-password-hashes.aspx?Redirected=true
http://us.generation-nt.com/securing-passwords-hash-help-35429432.html
http://forum.md5decrypter.co.uk/topic230-mysql-and-mssql-get-password-hashes.aspx
http://www.theregister.co.uk/2002/07/08/cracking_ms_sql_server_passwords/
    )hexlify	unhexlify)sha1N)warn)consteq)bascii_to_strunicodeu	mssql2000	mssql2005c                     t        | t              sJ t        |t              sJ t        | j	                  d      |z         j                         S )Nz	utf-16-le)
isinstancer	   bytesr   encodedigest)secretsalts     X/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/passlib/handlers/mssql.py
_raw_mssqlr   9   s>    fg&&dE""k*T1299;;    s   0x01000x0100c                    t        | t              r%t        |       |k(  r| j                  t              ryyt        | t
              r%t        |       |k(  r| j                  t              ryyt        j                  j                  | d      )z#common identify for mssql 2000/2005ThashF)
r   r	   len
startswithUIDENTr   BIDENTuhexcExpectedStringError)r   csizebsizes      r   _ident_mssqlr#   B   so    $ t9$//&"9  
D%	 t9$//&"9
  ff((v66r   c                    t        | t              rAt        |       |k(  r| j                  t              r	 t        | dd j                  d            S t        | t              rDt        | t              sJ t        |       |k(  rD| j                  t              r/	 t        | dd       S t        j                  j                  | d      t        j                  j                  |      # t        $ r Y *w xY w# t        $ r Y 9w xY w)zAcommon parser for mssql 2000/2005; returns 4 byte salt + checksum   Nutf-8r   )r   r	   r   r   r   r   r   	TypeErrorr   r   r   r   r    InvalidHashError)r   r!   r"   handlers       r   _parse_mssqlr*   P   s    $ t9$//&"9 ab!9:: 
D%	 $&&t9$//&"9 ab** ff((v66
&&
!
!'
**    s#   C% C4 %	C10C14	D ?D c                   `    e Zd ZdZd ZdZdZdxZZe	d        Z
e	d        Zd Zd Ze	d	        Zy
)r   a*  This class implements the password hash used by MS-SQL 2000, 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: bytes
    :param salt:
        Optional salt string.
        If not specified, one will be autogenerated (this is recommended).
        If specified, it must be 4 bytes in length.

    :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.
    r   (      c                     t        |dd      S )N^   .   r#   clsr   s     r   identifyzmssql2000.identify       D"b))r   c                 >    t        |dd|       } | |d d |dd        S )Nr0   r1   r.   r   checksumr*   r4   r   datas      r   from_stringzmssql2000.from_string   +    D"b#.Ra4844r   c                 ~    | j                   | j                  z   }dt        t        |      j	                               z   S Nr   r   r9   r   r   upperselfraws     r   	to_stringzmssql2000.to_string   s1    ii$--'-(:(:(<===r   c                     t        |t              r|j                  d      }| j                  }t	        ||      t	        |j                         |      z   S Nr&   )r   r   decoder   r   rB   )rD   r   r   s      r   _calc_checksumzmssql2000._calc_checksum   sB    fe$]]7+Fyy&$'*V\\^T*JJJr   c                 P   t        j                  |       | j                  |      }|j                  }|t         j                  j                  |       t        |t              r|j                  d      }t        |j                         |j                        }t        ||dd        S )Nr&      )r   validate_secretr=   r9   r   MissingDigestErrorr   r   rI   r   rB   r   r   )r4   r   r   rD   chkresults         r   verifyzmssql2000.verify   s     	6"t$mm;&&++C00fe$]]7+FFLLNDII6vs23x((r   N)__name__
__module____qualname____doc__namesetting_kwdschecksum_sizemin_salt_sizemax_salt_sizeclassmethodr5   r=   rF   rJ   rQ    r   r   r   r   f   si    . DLM$%%MM * * 5 5>
K ) )r   c                   P    e Zd ZdZd ZdZdZdxZZe	d        Z
e	d        Zd Zd Zy	)
r   a*  This class implements the password hash used by MS-SQL 2005, 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: bytes
    :param salt:
        Optional salt string.
        If not specified, one will be autogenerated (this is recommended).
        If specified, it must be 4 bytes in length.

    :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.
    r,   rL   r.   c                     t        |dd      S )N6      r2   r3   s     r   r5   zmssql2005.identify   r6   r   c                 >    t        |dd|       } | |d d |dd        S )Nr_   r`   r.   r8   r:   r;   s      r   r=   zmssql2005.from_string   r>   r   c                 ~    | j                   | j                  z   }dt        t        |            j	                         z   S r@   rA   rC   s     r   rF   zmssql2005.to_string   s1    ii$--'-5;;===r   c                 p    t        |t              r|j                  d      }t        || j                        S rH   )r   r   rI   r   r   )rD   r   s     r   rJ   zmssql2005._calc_checksum   s+    fe$]]7+F&$)),,r   N)rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r5   r=   rF   rJ   r\   r   r   r   r      sT    . DLM$%%MM * * 5 5>
-r   )"rU   binasciir   r   hashlibr   relogging	getLoggerrR   logwarningsr   passlib.utilsr   passlib.utils.compatr   r	   r
   passlib.utils.handlersutilshandlersr   __all__r   r   r   r#   r*   
HasRawSaltHasRawChecksumGenericHandlerr   r   r\   r   r   <module>rt      s    J (  	 'g''1  " : : # # <
 
	
8+,H)r00"2C2C H)Z9-r00"2C2C 9-r   