<s> import binascii <EOL> import datetime <EOL> import string <EOL> import sys <EOL> import warnings <EOL> from django import forms <EOL> from django . forms import fields <EOL> from django . db import models <EOL> from django . conf import settings <EOL> from django . utils . encoding import smart_str , force_unicode <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from Crypto import Random <EOL> from Crypto . Random import random <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning , <EOL> ) <EOL> if settings . DEBUG : <EOL> import pickle <EOL> else : <EOL> try : <EOL> import cPickle as pickle <EOL> except : <EOL> import pickle <EOL> class BaseEncryptedField ( models . Field ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . cipher_type = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . block_type = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . secret_key = kwargs . pop ( '<STR_LIT>' , settings . SECRET_KEY ) <EOL> self . secret_key = self . secret_key [ : <NUM_LIT:32> ] <EOL> if self . block_type is None : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning , <EOL> ) <EOL> try : <EOL> imp = __import__ ( '<STR_LIT>' , globals ( ) , locals ( ) , [ self . cipher_type ] , - <NUM_LIT:1> ) <EOL> except : <EOL> imp = __import__ ( '<STR_LIT>' , globals ( ) , locals ( ) , [ self . cipher_type ] ) <EOL> self . cipher_object = getattr ( imp , self . cipher_type ) <EOL> if self . block_type : <EOL> self . prefix = '<STR_LIT>' % ( self . cipher_type , self . block_type ) <EOL> self . iv = Random . new ( ) . read ( self . cipher_object . block_size ) <EOL> self . cipher = self . cipher_object . new ( <EOL> self . secret_key , <EOL> getattr ( self . cipher_object , self . block_type ) , <EOL> self . iv ) <EOL> else : <EOL> self . cipher = self . cipher_object . new ( self . secret_key ) <EOL> self . prefix = '<STR_LIT>' % self . cipher_type <EOL> self . original_max_length = max_length = kwargs . get ( '<STR_LIT:max_length>' , <NUM_LIT> ) <EOL> self . unencrypted_length = max_length <EOL> max_length += <NUM_LIT:2> <EOL> mod = max_length % self . cipher . block_size <EOL> if mod > <NUM_LIT:0> : <EOL> max_length += self . cipher . block_size - mod <EOL> if self . block_type : <EOL> max_length += len ( self . iv ) <EOL> kwargs [ '<STR_LIT:max_length>' ] = max_length * <NUM_LIT:2> + len ( self . prefix ) <EOL> super ( BaseEncryptedField , self ) . __init__ ( * args , ** kwargs ) <EOL> def _is_encrypted ( self , value ) : <EOL> return isinstance ( value , basestring ) and value . startswith ( self . prefix ) <EOL> def _get_padding ( self , value ) : <EOL> mod = ( len ( value ) + <NUM_LIT:2> ) % self . cipher . block_size <EOL> return self . cipher . block_size - mod + <NUM_LIT:2> <EOL> def to_python ( self , value ) : <EOL> if self . _is_encrypted ( value ) : <EOL> if self . block_type : <EOL> self . iv = binascii . a2b_hex ( value [ len ( self . prefix ) : ] ) [ : len ( self . iv ) ] <EOL> self . cipher = self . cipher_object . new ( <EOL> self . secret_key , <EOL> getattr ( self . cipher_object , self . block_type ) , <EOL> self . iv ) <EOL> decrypt_value = binascii . a2b_hex ( value [ len ( self . prefix ) : ] ) [ len ( self . iv ) : ] <EOL> else : <EOL> decrypt_value = binascii . a2b_hex ( value [ len ( self . prefix ) : ] ) <EOL> return force_unicode ( <EOL> self . cipher . decrypt ( decrypt_value ) . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> ) <EOL> return value <EOL> def get_db_prep_value ( self , value , connection = None , prepared = False ) : <EOL> if value is None : <EOL> return None <EOL> value = smart_str ( value ) <EOL> if not self . _is_encrypted ( value ) : <EOL> padding = self . _get_padding ( value ) <EOL> if padding > <NUM_LIT:0> : <EOL> value += "<STR_LIT>" + '<STR_LIT>' . join ( [ <EOL> random . choice ( string . printable ) <EOL> for index in range ( padding - <NUM_LIT:1> ) <EOL> ] ) <EOL> if self . block_type : <EOL> self . cipher = self . cipher_object . new ( <EOL> self . secret_key , <EOL> getattr ( self . cipher_object , self . block_type ) , <EOL> self . iv ) <EOL> value = self . prefix + binascii . b2a_hex ( self . iv + self . cipher . encrypt ( value ) ) <EOL> else : <EOL> value = self . prefix + binascii . b2a_hex ( self . cipher . encrypt ( value ) ) <EOL> return value <EOL> def deconstruct ( self ) : <EOL> original = super ( BaseEncryptedField , self ) . deconstruct ( ) <EOL> kwargs = original [ - <NUM_LIT:1> ] <EOL> if self . cipher_type != '<STR_LIT>' : <EOL> kwargs [ '<STR_LIT>' ] = self . cipher_type <EOL> if self . block_type is not None : <EOL> kwargs [ '<STR_LIT>' ] = self . block_type <EOL> if self . original_max_length != <NUM_LIT> : <EOL> kwargs [ '<STR_LIT:max_length>' ] = self . original_max_length <EOL> return original [ : - <NUM_LIT:1> ] + ( kwargs , ) <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> class EncryptedCharField ( BaseEncryptedField ) : <EOL> __metaclass__ = models . SubfieldBase <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 get_db_prep_value ( self , value , connection = None , prepared = False ) : <EOL> if value is not None and not self . _is_encrypted ( value ) : <EOL> if len ( value ) > self . unencrypted_length : <EOL> raise ValueError ( <EOL> "<STR_LIT>" + <EOL> str ( len ( value ) ) + "<STR_LIT>" + str ( self . unencrypted_length ) <EOL> ) <EOL> return super ( EncryptedCharField , self ) . get_db_prep_value ( <EOL> value , <EOL> connection = connection , <EOL> prepared = prepared , <EOL> ) <EOL> class BaseEncryptedDateField ( BaseEncryptedField ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT:max_length>' ] = self . max_raw_length <EOL> super ( BaseEncryptedDateField , self ) . __init__ ( * args , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return '<STR_LIT>' <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : self . form_widget , '<STR_LIT>' : self . form_field } <EOL> defaults . update ( kwargs ) <EOL> return super ( BaseEncryptedDateField , self ) . formfield ( ** defaults ) <EOL> def to_python ( self , value ) : <EOL> if value in fields . EMPTY_VALUES : <EOL> date_value = value <EOL> else : <EOL> if isinstance ( value , self . date_class ) : <EOL> date_value = value <EOL> else : <EOL> date_text = super ( BaseEncryptedDateField , self ) . to_python ( value ) <EOL> date_value = self . date_class ( * map ( int , date_text . split ( '<STR_LIT::>' ) ) ) <EOL> return date_value <EOL> def get_db_prep_value ( self , value , connection = None , prepared = False ) : <EOL> if value : <EOL> date_text = value . strftime ( self . save_format ) <EOL> else : <EOL> date_text = None <EOL> return super ( BaseEncryptedDateField , self ) . get_db_prep_value ( <EOL> date_text , <EOL> connection = connection , <EOL> prepared = prepared <EOL> ) <EOL> class EncryptedDateField ( BaseEncryptedDateField ) : <EOL> __metaclass__ = models . SubfieldBase <EOL> form_widget = forms . DateInput <EOL> form_field = forms . DateField <EOL> save_format = "<STR_LIT>" <EOL> date_class = datetime . date <EOL> max_raw_length = <NUM_LIT:10> <EOL> class EncryptedDateTimeField ( BaseEncryptedDateField ) : <EOL> __metaclass__ = models . SubfieldBase <EOL> form_widget = forms . DateTimeInput <EOL> form_field = forms . DateTimeField <EOL> save_format = "<STR_LIT>" <EOL> date_class = datetime . datetime <EOL> max_raw_length = <NUM_LIT> <EOL> class BaseEncryptedNumberField ( BaseEncryptedField ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if self . max_raw_length : <EOL> kwargs [ '<STR_LIT:max_length>' ] = self . max_raw_length <EOL> super ( BaseEncryptedNumberField , self ) . __init__ ( * args , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return '<STR_LIT>' <EOL> def to_python ( self , value ) : <EOL> if isinstance ( value , self . number_type ) or value == '<STR_LIT>' : <EOL> number = value <EOL> else : <EOL> number_text = super ( BaseEncryptedNumberField , self ) . to_python ( value ) <EOL> number = self . number_type ( number_text ) <EOL> return number <EOL> def get_db_prep_value ( self , value , connection = None , prepared = False ) : <EOL> number_text = self . format_string % value <EOL> return super ( BaseEncryptedNumberField , self ) . get_db_prep_value ( <EOL> number_text , <EOL> connection = connection , <EOL> prepared = prepared , <EOL> ) <EOL> class EncryptedIntField ( BaseEncryptedNumberField ) : <EOL> __metaclass__ = models . SubfieldBase <EOL> max_raw_length = len ( str ( - sys . maxint - <NUM_LIT:1> ) ) <EOL> number_type = int <EOL> format_string = "<STR_LIT>" <EOL> class EncryptedLongField ( BaseEncryptedNumberField ) : <EOL> __metaclass__ = models . SubfieldBase <EOL> max_raw_length = None <EOL> number_type = long <EOL> format_string = "<STR_LIT>" <EOL> def get_internal_type ( self ) : <EOL> return '<STR_LIT>' <EOL> class EncryptedFloatField ( BaseEncryptedNumberField ) : <EOL> __metaclass__ = models . SubfieldBase <EOL> max_raw_length = <NUM_LIT> <EOL> number_type = float <EOL> format_string = "<STR_LIT>" <EOL> class PickleField ( models . TextField ) : <EOL> __metaclass__ = models . SubfieldBase <EOL> editable = False <EOL> serialize = False <EOL> def get_db_prep_value ( self , value , connection = None , prepared = False ) : <EOL> return pickle . dumps ( value ) <EOL> def to_python ( self , value ) : <EOL> if not isinstance ( value , basestring ) : <EOL> return value <EOL> try : <EOL> return pickle . loads ( smart_str ( value ) ) <EOL> except ValueError : <EOL> return value <EOL> except EOFError : <EOL> return value <EOL> class EncryptedUSPhoneNumberField ( BaseEncryptedField ) : <EOL> __metaclass__ = models . SubfieldBase <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def formfield ( self , ** kwargs ) : <EOL> try : <EOL> from localflavor . us . forms import USPhoneNumberField <EOL> except ImportError : <EOL> from django . contrib . localflavor . us . forms import USPhoneNumberField <EOL> defaults = { '<STR_LIT>' : USPhoneNumberField } <EOL> defaults . update ( kwargs ) <EOL> return super ( EncryptedUSPhoneNumberField , self ) . formfield ( ** defaults ) <EOL> class EncryptedUSSocialSecurityNumberField ( BaseEncryptedField ) : <EOL> __metaclass__ = models . SubfieldBase <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def formfield ( self , ** kwargs ) : <EOL> try : <EOL> from localflavor . us . forms import USSocialSecurityNumberField <EOL> except ImportError : <EOL> from django . contrib . localflavor . us . forms import USSocialSecurityNumberField <EOL> defaults = { '<STR_LIT>' : USSocialSecurityNumberField } <EOL> defaults . update ( kwargs ) <EOL> return super ( EncryptedUSSocialSecurityNumberField , self ) . formfield ( ** defaults ) <EOL> class EncryptedEmailField ( BaseEncryptedField ) : <EOL> __metaclass__ = models . SubfieldBase <EOL> description = _ ( "<STR_LIT>" ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def formfield ( self , ** kwargs ) : <EOL> from django . forms import EmailField <EOL> defaults = { '<STR_LIT>' : EmailField , '<STR_LIT:max_length>' : self . unencrypted_length } <EOL> defaults . update ( kwargs ) <EOL> return super ( EncryptedEmailField , self ) . formfield ( ** defaults ) <EOL> try : <EOL> from south . modelsinspector import add_introspection_rules <EOL> add_introspection_rules ( [ <EOL> ( <EOL> [ <EOL> BaseEncryptedField , EncryptedDateField , BaseEncryptedDateField , EncryptedCharField , EncryptedTextField , <EOL> EncryptedFloatField , EncryptedDateTimeField , BaseEncryptedNumberField , EncryptedIntField , EncryptedLongField , <EOL> EncryptedUSPhoneNumberField , EncryptedEmailField , <EOL> ] , <EOL> [ ] , <EOL> { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , { } ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , { } ) , <EOL> } , <EOL> ) , <EOL> ] , [ "<STR_LIT>" ] ) <EOL> add_introspection_rules ( [ ] , [ "<STR_LIT>" ] ) <EOL> except ImportError : <EOL> pass </s>
