<s> '''<STR_LIT>''' <EOL> from Crypto . Cipher import Blowfish <EOL> from elixir . statements import Statement <EOL> from sqlalchemy . orm import MapperExtension , EXT_CONTINUE , EXT_STOP <EOL> try : <EOL> from sqlalchemy . orm import EXT_PASS <EOL> SA05orlater = False <EOL> except ImportError : <EOL> SA05orlater = True <EOL> __all__ = [ '<STR_LIT>' ] <EOL> __doc_all__ = [ ] <EOL> def encrypt_value ( value , secret ) : <EOL> return Blowfish . new ( secret , Blowfish . MODE_CFB ) . encrypt ( value ) . encode ( '<STR_LIT>' ) <EOL> def decrypt_value ( value , secret ) : <EOL> return Blowfish . new ( secret , Blowfish . MODE_CFB ) . decrypt ( value . decode ( '<STR_LIT>' ) ) <EOL> class ActsAsEncrypted ( object ) : <EOL> def __init__ ( self , entity , for_fields = [ ] , with_secret = '<STR_LIT>' ) : <EOL> def perform_encryption ( instance , encrypt = True ) : <EOL> encrypted = getattr ( instance , '<STR_LIT>' , None ) <EOL> if encrypted is encrypt : <EOL> return <EOL> else : <EOL> instance . _elixir_encrypted = encrypt <EOL> if encrypt : <EOL> func = encrypt_value <EOL> else : <EOL> func = decrypt_value <EOL> for column_name in for_fields : <EOL> current_value = getattr ( instance , column_name ) <EOL> if current_value : <EOL> setattr ( instance , column_name , <EOL> func ( current_value , with_secret ) ) <EOL> def perform_decryption ( instance ) : <EOL> perform_encryption ( instance , encrypt = False ) <EOL> class EncryptedMapperExtension ( MapperExtension ) : <EOL> def before_insert ( self , mapper , connection , instance ) : <EOL> perform_encryption ( instance ) <EOL> return EXT_CONTINUE <EOL> def before_update ( self , mapper , connection , instance ) : <EOL> perform_encryption ( instance ) <EOL> return EXT_CONTINUE <EOL> if SA05orlater : <EOL> def reconstruct_instance ( self , mapper , instance ) : <EOL> perform_decryption ( instance ) <EOL> return EXT_CONTINUE <EOL> else : <EOL> def populate_instance ( self , mapper , selectcontext , row , <EOL> instance , * args , ** kwargs ) : <EOL> mapper . populate_instance ( selectcontext , instance , row , <EOL> * args , ** kwargs ) <EOL> perform_decryption ( instance ) <EOL> return EXT_STOP <EOL> entity . _descriptor . add_mapper_extension ( EncryptedMapperExtension ( ) ) <EOL> acts_as_encrypted = Statement ( ActsAsEncrypted ) </s>
