Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010-2012 OpenStack LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or # implied. # See the License for the specific language governing permissions and # limitations under the License.
Utilities for memcache encryption and integrity check.
Data is serialized before been encrypted or MACed. Encryption have a dependency on the pycrypto. If pycrypto is not available, CryptoUnabailableError will be raised.
Encrypted data stored in memcache are prefixed with '{ENCRYPT:AES256}'.
MACed data stored in memcache are prefixed with '{MAC:SHA1}'.
"""
# make sure pycrypt is available except ImportError: AES = None
# prefix marker indicating data is HMACed (signed by a secret key) # prefix marker indicating data is encrypted
""" raise when unable to verify MACed data
This usually indicates that data had been expectedly modified in memcache.
"""
""" raise when unable to decrypt encrypted data
"""
""" raise when Python Crypto module is not available
"""
""" Ensure Crypto module is available. """
def wrapper(*args, **kwds):
""" Generates and returns a 256 bit AES key, based on sha256 hash. """
""" Computes and returns the base64 encoded MAC. """
""" Return the base64 encoded SHA1 hash of the data. """
""" MAC the data using SHA1. """
""" Verify data integrity by ensuring MAC is valid. """ raise InvalidMacError('invalid MAC; expect=%s, actual=%s' % (mac_data['mac'], mac)) else: # doesn't appear to be MACed data
def encrypt_data(token, secret, data): """ Encryptes the data with the given secret key. """
def decrypt_data(token, secret, data): """ Decrypt the data with the given secret key. """ # encrypted data else: # doesn't appear to be encrypted data |