Last active
January 27, 2016 01:34
-
-
Save gcmurphy/08f9819986d1a566d4e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example using libnacl based encryption engine | |
(venv)[gm@localhost libnacl]$ python demo.py | |
username = fred, password = secret | |
username = mary, password = secret | |
(venv)[gm@localhost libnacl]$ sqlite3 /tmp/demo.db 'select * from user;' | |
1|fred|b2997d0bf6634b6472881f7c40dab2f5e880f03fdeca340aea977500441479ed04637fae0d5f49bd0c9ab4bdcd9a | |
2|mary|abb99fe819e861b105eae670741360cf3a51e414a0670d87e20432166bafa859d4733092c14a602fb76f58264fb6 | |
(venv)[gm@localhost libnacl]$ pip freeze | |
You are using pip version 6.0.8, however version 8.0.2 is available. | |
You should consider upgrading via the 'pip install --upgrade pip' command. | |
cffi==1.5.0 | |
cryptography==1.2.1 | |
enum34==1.1.2 | |
idna==2.0 | |
ipaddress==1.0.16 | |
libnacl==1.4.4 | |
pyasn1==0.1.9 | |
pycparser==2.14 | |
six==1.10.0 | |
SQLAlchemy==1.0.11 | |
SQLAlchemy-Utils==0.31.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlalchemy as sa | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy import create_engine | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy_utils import EncryptedType | |
from sqlalchemy_utils.types.encrypted import EncryptionDecryptionBaseEngine | |
from libnacl.secret import SecretBox | |
class NaclEngine(EncryptionDecryptionBaseEngine): | |
def _initialize_engine(self, parent_key_class): | |
self.secret_key = parent_key_class | |
def encrypt(self, value): | |
return SecretBox(key=self.secret_key).encrypt(value).encode('hex') | |
def decrypt(self, value): | |
return SecretBox(key=self.secret_key).decrypt(value.decode('hex')) | |
secret_key = 'secretkey1234' | |
# setup | |
#engine = create_engine('sqlite:///:memory:') | |
engine = create_engine('sqlite:////tmp/demo.db') | |
connection = engine.connect() | |
Base = declarative_base() | |
class User(Base): | |
__tablename__ = "user" | |
id = sa.Column(sa.Integer, primary_key=True) | |
username = sa.Column(sa.String()) | |
password = sa.Column(EncryptedType(sa.String, secret_key, engine=NaclEngine)) | |
sa.orm.configure_mappers() | |
Base.metadata.create_all(connection) | |
# create a configured "Session" class | |
Session = sessionmaker(bind=connection) | |
# create a Session | |
session = Session() | |
# example | |
fred = User(username='fred', password='secret') | |
session.add(fred) | |
session.commit() | |
mary = User(username='mary', password='secret') | |
session.add(mary) | |
session.commit() | |
for user in session.query(User).all(): | |
print("username = {}, password = {}".format(user.username, user.password)) | |
# teardown | |
session.close_all() | |
#Base.metadata.drop_all(connection) | |
connection.close() | |
engine.dispose() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment