
    h8'h0                        d Z ddlZddlZddlZddl ddlmZ ej                  d   dk7  rddlm	Z
 n	 ddlm	Z
  e
j                  ej                  e        e
j                  ej"                  e        e
j                  ej$                  e       e
j&                  Z G d de      Z G d	 d
ee      Z G d dee      Zy# e$ r	 ddlm	Z
 Y w xY w)a  
Peewee integration with pysqlcipher.

Project page: https://github.com/leapcode/pysqlcipher/

**WARNING!!! EXPERIMENTAL!!!**

* Although this extention's code is short, it has not been properly
  peer-reviewed yet and may have introduced vulnerabilities.

Also note that this code relies on pysqlcipher and sqlcipher, and
the code there might have vulnerabilities as well, but since these
are widely used crypto modules, we can expect "short zero days" there.

Example usage:

     from peewee.playground.ciphersql_ext import SqlCipherDatabase
     db = SqlCipherDatabase('/path/to/my.db', passphrase="don'tuseme4real")

* `passphrase`: should be "long enough".
  Note that *length beats vocabulary* (much exponential), and even
  a lowercase-only passphrase like easytorememberyethardforotherstoguess
  packs more noise than 8 random printable characters and *can* be memorized.

When opening an existing database, passphrase should be the one used when the
database was created. If the passphrase is incorrect, an exception will only be
raised **when you access the database**.

If you need to ask for an interactive passphrase, here's example code you can
put after the `db = ...` line:

    try:  # Just access the database so that it checks the encryption.
        db.get_tables()
    # We're looking for a DatabaseError with a specific error message.
    except peewee.DatabaseError as e:
        # Check whether the message *means* "passphrase is wrong"
        if e.args[0] == 'file is encrypted or is not a database':
            raise Exception('Developer should Prompt user for passphrase '
                            'again.')
        else:
            # A different DatabaseError. Raise it.
            raise e

See a more elaborate example with this code at
https://gist.github.com/thedod/11048875
    N)*)SqliteExtDatabase   )dbapi2c                   "    e Zd ZeZd Zd Zd Zy)_SqlCipherDatabasec                 4   t        | j                        }|j                  dd      j                  dd      }t	        j
                  | j                  fdd i|}	 |r|j                  d|z         | j                  |       |S #  |j                           xY w)N
passphrase '''isolation_levelzPRAGMA key='%s')
dictconnect_paramspopreplace	sqlcipherconnectdatabaseexecute_add_conn_hooksclose)selfparamsr
   conns       S/var/www/html/trade_iq/venv/lib/python3.12/site-packages/playhouse/sqlcipher_ext.py_connectz_SqlCipherDatabase._connectF   s    d))*ZZb199#tD
  OOO	.;<  & 	JJLs   'B Bc                 X    | j                         st        d      || j                  d<   y )NzmCannot set passphrase when database is open. To change passphrase of an open database use the rekey() method.r
   )	is_closedImproperlyConfiguredr   r   r
   s     r   set_passphrasez!_SqlCipherDatabase.set_passphraseT   s3    ~~& (O P P -7L)    c                     | j                         r| j                          | j                  d|j                  dd      z         || j                  d<   y)NzPRAGMA rekey='%s'r   r   r
   T)r   r   execute_sqlr   r   r!   s     r   rekeyz_SqlCipherDatabase.rekey\   sI    >>LLN,z/A/A#t/LLM,6L)r#   N)__name__
__module____qualname____sqlcipher_version__server_versionr   r"   r&    r#   r   r   r   C   s    *N7r#   r   c                       e Zd Zy)SqlCipherDatabaseNr'   r(   r)   r,   r#   r   r.   r.   e       r#   r.   c                       e Zd Zy)SqlCipherExtDatabaseNr/   r,   r#   r   r2   r2   i   r0   r#   r2   )__doc__datetimedecimalsyspeeweeplayhouse.sqlite_extr   version_infopysqlcipherr   r   
sqlcipher3ImportErrorpysqlcipher3register_adapterDecimalstrdatetimesqlite_version_infor*   objectr   SqliteDatabaser.   r2   r,   r#   r   <module>rF      s   -\   
  2A!/52 	  7??C 0 	  8==# . 	  8==# .!55  D	*N 		-/@ 	_  545s   C CC