def synchronize(self): data = WebClient.download_binary(self.evernote_url).decode('UTF-8') data = evernote_data_pattern.search(data).group() dict_ = json.loads(data.replace('\<', '<').replace('\>', '>')) self.title = dict_['title'] self.content = dict_['content'] image_urls = img_url_pattern.findall(self.content) if image_urls: try: dbm = anydbm.open(app.config['IMAGE_CACHE'], 'c') for img_url in image_urls: key = Encryption.computer_hashcode(img_url) value = WebClient.download_binary(img_url) dbm[key] = value self.content = self.content.replace( img_url, '/image/{0}/'.format(key)) finally: dbm.close() self.uid = dict_['created'] / 1000 self.created = datetime.datetime.utcfromtimestamp(self.uid) self.updated = datetime.datetime.utcfromtimestamp(dict_['updated'] / 1000) self.tags = [] if 'tagNames' in dict_: for tagName in dict_['tagNames']: tag = db.session.query(Tag).filter_by(name=tagName).first() tag = tag if tag else Tag(name=tagName) self.tags.append(tag)
def synchronize(self): data = WebClient.download_binary(self.evernote_url).decode('UTF-8') data = evernote_data_pattern.search(data).group() dict_ = json.loads(data.replace('\<', '<').replace('\>', '>')) self.title = dict_['title'] self.content = dict_['content'] image_urls = img_url_pattern.findall(self.content) if image_urls: try: dbm = anydbm.open(app.config['IMAGE_CACHE'], 'c') for img_url in image_urls: key = Encryption.computer_hashcode(img_url) value = WebClient.download_binary(img_url) dbm[key] = value self.content = self.content.replace(img_url, '/image/{0}/'.format(key)) finally: dbm.close() self.uid = dict_['created'] / 1000 self.created = datetime.datetime.utcfromtimestamp(self.uid) self.updated = datetime.datetime.utcfromtimestamp(dict_['updated'] / 1000) self.tags = [] if 'tagNames' in dict_: for tagName in dict_['tagNames']: tag = db.session.query(Tag).filter_by(name = tagName).first() tag = tag if tag else Tag(name = tagName) self.tags.append(tag)
def test_different_hashcode(self): """Ensure everytime a different hashcode is generated. This is for preventing Rainbow-tables cracking""" hashcode1 = Encryption.generate_hashcode_and_salt(self.password)[0] hashcode2 = Encryption.generate_hashcode_and_salt(self.password)[0] assert hashcode1 != hashcode2
def test_generate_password(self): """Ensure generated salt and password could authenticate the original password""" hashcode, salt = Encryption.generate_hashcode_and_salt(self.password) assert Encryption.check_password(self.password, hashcode, salt)
from werkzeug.routing import BaseConverter from quick_orm.core import Database from toolkit_library.encryption import Encryption class RegexConverter(BaseConverter): def __init__(self, url_map, *items): super(RegexConverter, self).__init__(url_map) self.regex = items[0] app = Flask(__name__) app.url_map.converters['regex'] = RegexConverter app.config.update( DEBUG = True, SECRET_KEY = Encryption.generate_random_string(), CONNECTION_STRING = 'postgresql://*****:*****@localhost/everblog', IMAGE_CACHE = 'everblog/everblog.dbm', ADMIN_USERNAME = '******', ADMIN_PASSWORD = '******', BLOG_OWNER = 'Tyler Long', CONTACT_METHODS = [ ('email', 'mailto:[email protected]'), (u'微博', 'http://weibo.com/tylerlong'), ('github', 'https://github.com/tylerlong'), ('bitbucket', 'https://bitbucket.org/tylerlong'), ('stackoverflow', 'http://stackoverflow.com/users/862862/tyler-long'), ],