Skip to content

Instantly share code, notes, and snippets.

@dodilp
Created October 5, 2014 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dodilp/f6e41f9db09a0e8d2b6f to your computer and use it in GitHub Desktop.
Save dodilp/f6e41f9db09a0e8d2b6f to your computer and use it in GitHub Desktop.
Connect Demo
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7.2 (/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7)" project-jdk-type="Python SDK" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/CryptoApp.iml" filepath="$PROJECT_DIR$/.idea/CryptoApp.iml" />
</modules>
</component>
</project>
<component name="DependencyValidationManager">
<state>
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</state>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
</component>
</project>
__author__ = 'donpinto'
from couchbase import Couchbase
import json
import keyring
from simplecrypt import encrypt,decrypt
#Get bucketpassword from keyring
bktpwd = keyring.get_password('bucketpassword','CBEncrypt')
cb = Couchbase.connect(bucket='customer', host='localhost', password=str(bktpwd))
#Get app encryptionkey from keyring
cryptkey = keyring.get_password('key1','CBEncrypt')
#Loop over the documents and encrypt the ccard
for i in range(0,1000):
result = cb.get('customer'+str(i))
customer = result.value
print customer['ccInfo']['cardNumber']
customer['ccInfo']['cardNumber'] = encrypt(cryptkey,customer['ccInfo']['cardNumber'])
#customer['ccInfo']['cardNumber'] = decrypt(cryptkey,customer['ccInfo']['cardNumber'])
print customer
cb.set('customer'+str(i), customer)
__author__ = 'donpinto'
{
"dateLastActive": "Wed Mar 05 03:14:59 UTC 2014",
"dateAdded": "Wed Mar 05 03:14:59 UTC 2014",
"firstName": "Matt",
"lastName": "Botsford",
"emailAddress": "bernard@gutmann.biz",
"postalCode": "12912-4268",
"phoneNumber": "600-649-7428 x055",
"ccInfo": {
"cardType": "discover",
"cardNumber": "1212-1221-1121-1234",
"cardExpiry": "2013-09-12"
},
"customerId": "customer00",
"firstName" : "Don"
}
from Crypto.Cipher import AES
from Crypto.Hash import MD5
from base64 import encodestring, decodestring #base64 encoding to enable json dumps
# the block size for the key and message must be 16, 24, or 32 for AES
BLOCK_SIZE = 32
# the character used for padding--with a block cipher such as AES, the value
# you encrypt must be a multiple of BLOCK_SIZE in length. This character is
# used to ensure that your value is always a multiple of BLOCK_SIZE
PADDING = '{'
def pad(msg, block_size=BLOCK_SIZE, padding=PADDING):
"""
*pad the text to be encrypted*
- appends a padding character to the end of the String
- until the string has block_size length
"""
return msg + ((block_size - len(msg) % block_size) * padding)
def depad(msg, padding=PADDING):
"""depad the decryptet message"""
return msg.rstrip(padding)
def getSecret(key):
"""hases the key to MD5"""
return MD5.new(key).hexdigest()
def encrypt(key, msg):
"""encrypts the message"""
secret = getSecret(key)
cipher = AES.new(secret)
return encodestring(cipher.encrypt(pad(msg)))
def decrypt(key, msg):
"""decrypts the message"""
secret = getSecret(key)
cipher = AES.new(secret)
return depad((cipher.decrypt(decodestring(msg))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment