diff options
author | Brian Sherson <caretaker82@euclid.shersonb.net> | 2013-08-27 20:39:32 -0700 |
---|---|---|
committer | Brian Sherson <caretaker82@euclid.shersonb.net> | 2013-08-27 20:39:32 -0700 |
commit | 25ed9b71564b73b07d664e2396c641ffe74f45b1 (patch) | |
tree | 14b948f5f08ec819a4c02a5836dc0350aec3aa3e /wallet.py | |
parent | f1504f7162c8c43e3b06dd279370451e9b683e9c (diff) |
Diffstat (limited to 'wallet.py')
-rw-r--r--[l---------] | wallet.py | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/wallet.py b/wallet.py index eba4a72..05321f0 120000..100644 --- a/wallet.py +++ b/wallet.py @@ -1 +1,37 @@ -../wallet.py
\ No newline at end of file +#!/usr/bin/python +import pickle, Crypto.Cipher.Blowfish, os, getpass +from threading import Lock + +class Wallet(dict): + def __init__(self, filename): + self.lock=Lock() + if os.path.isfile(filename): + self.f=open(filename,"rb+") + self.passwd=getpass.getpass() + self.crypt=Crypto.Cipher.Blowfish.new(self.passwd) + contents_encrypted=self.f.read() + contents=self.crypt.decrypt(contents_encrypted+"\x00"*((-len(contents_encrypted))%8)) + if contents.startswith(self.passwd): + self.update(dict(pickle.loads(contents[len(self.passwd):]))) + else: + self.f.close() + raise BaseException, "Incorrect Password" + else: + self.f=open(filename,"wb+") + passwd=self.passwd=None + while passwd==None or passwd!=self.passwd: + passwd=getpass.getpass("Enter new password: ") + self.passwd=getpass.getpass("Confirm new password: ") + if passwd!=self.passwd: print "Passwords do not match!" + self.crypt=Crypto.Cipher.Blowfish.new(self.passwd) + self.flush() + def flush(self): + contents=self.passwd+pickle.dumps(self.items(), protocol=2) + self.lock.acquire() + try: + self.f.seek(0) + self.f.write(self.crypt.encrypt(contents+"\x00"*((-len(contents))%8))) + self.f.truncate() + self.f.flush() + finally: + self.lock.release() |