<s> from django . db import models <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django import forms <EOL> from django . conf import settings <EOL> import warnings <EOL> try : <EOL> from keyczar import keyczar <EOL> except ImportError : <EOL> raise ImportError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class EncryptionWarning ( RuntimeWarning ) : pass <EOL> class BaseEncryptedField ( models . Field ) : <EOL> prefix = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if not hasattr ( settings , '<STR_LIT>' ) : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . crypt = keyczar . Crypter . Read ( settings . ENCRYPTED_FIELD_KEYS_DIR ) <EOL> self . unencrypted_length = max_length <EOL> if max_length : <EOL> max_length = len ( self . prefix ) + len ( self . crypt . Encrypt ( '<STR_LIT:x>' * max_length ) ) <EOL> super ( BaseEncryptedField , self ) . __init__ ( * args , ** kwargs ) <EOL> def to_python ( self , value ) : <EOL> if value and ( value . startswith ( self . prefix ) ) : <EOL> retval = self . crypt . Decrypt ( value [ len ( self . prefix ) : ] ) <EOL> else : <EOL> retval = value <EOL> return retval <EOL> def get_db_prep_value ( self , value , connection , prepared = False ) : <EOL> if value and not value . startswith ( self . prefix ) : <EOL> max_length = self . unencrypted_length <EOL> if max_length and len ( value ) > max_length : <EOL> warnings . warn ( "<STR_LIT>" % ( self . name , len ( value ) , max_length ) , EncryptionWarning ) <EOL> value = value [ : max_length ] <EOL> value = self . prefix + self . crypt . Encrypt ( value ) <EOL> return value <EOL> class EncryptedTextField ( BaseEncryptedField ) : <EOL> __metaclass__ = models . SubfieldBase <EOL> def get_internal_type ( self ) : <EOL> return '<STR_LIT>' <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . Textarea } <EOL> defaults . update ( kwargs ) <EOL> return super ( EncryptedTextField , self ) . formfield ( ** defaults ) <EOL> def south_field_triple ( self ) : <EOL> "<STR_LIT>" <EOL> from south . modelsinspector import introspector <EOL> field_class = "<STR_LIT>" <EOL> args , kwargs = introspector ( self ) <EOL> return ( field_class , args , kwargs ) <EOL> class EncryptedCharField ( BaseEncryptedField ) : <EOL> __metaclass__ = models . SubfieldBase <EOL> def __init__ ( self , max_length = None , * args , ** kwargs ) : <EOL> super ( EncryptedCharField , self ) . __init__ ( max_length = max_length , * args , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT:max_length>' : self . max_length } <EOL> defaults . update ( kwargs ) <EOL> return super ( EncryptedCharField , self ) . formfield ( ** defaults ) <EOL> def south_field_triple ( self ) : <EOL> "<STR_LIT>" <EOL> from south . modelsinspector import introspector <EOL> field_class = "<STR_LIT>" <EOL> args , kwargs = introspector ( self ) <EOL> return ( field_class , args , kwargs ) </s>
