Skip to content

Instantly share code, notes, and snippets.

View jckdotim's full-sized avatar
💭
I may be slow to respond.

jckim.xyz jckdotim

💭
I may be slow to respond.
View GitHub Profile
print "hi!!!"
class LocationMixIn(object):
lat = db.Column(db.Float(25), nullable=True)
lng = db.Column(db.Float(25), nullable=True)
from Crypto.Cipher import AES
import binascii
aes_key = '1234567890123456' # it must be 128-bit.
def aes_encrypt(data):
cipher = AES.new(aes_key)
expected_length = 16 * ((len(data) / 16) + 1)
padding_length = expected_length - len(data)
data = data + chr(padding_length) * padding_length
class EncryptedLocationMixIn(object):
encrypted_lat = db.Column(db.String(64), nullable=True)
encrypted_lng = db.Column(db.String(64), nullable=True)
@hybrid_property
def lat(self):
return float(aes_decrypt(self.encrypted_lat))
@lat.expression
def lat(cls):
decrypted = expr.func.aes_decrypt(
expr.func.unhex(cls.encrypted_lat), aes_key)
return cast(decrypted, db.Float(25))
@lat.setter
def lat(self, value):
self.encrypted_lat = aes_encrypt(str(value)[:16])
if str(db.engine.url).find('mysql://') >= 0:
import EncryptedLocationMixIn as LocationMixIn
>>> Place.query.get(1).lat
37.520410699999999
>>>
>>> Place.query.filter(Place.lat - 37.52 > 0.01).count()
2011-12-10 02:26:57,731 INFO sqlalchemy.engine.base.Engine SELECT count(*) AS count_1
FROM ...
FROM place
WHERE aes_decrypt(unhex(place.encrypted_lat), %s) - %s > %s) AS anon_1
2011-12-10 02:26:57,731 INFO sqlalchemy.engine.base.Engine ('1234567890123456', 37.520000000000003, 0.01)
8L
>>>