Changeset 318 for trunk

Show
Ignore:
Timestamp:
05/21/09 16:36:02 (3 years ago)
Author:
ocjhb
Message:

encrypt the wallet storage with pure python blowfish

Location:
trunk/sandbox/jhb
Files:
1 added
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/sandbox/jhb/mobile/ocwallet.py

    r317 r318  
    262262            def log_message(self,*args,**kwargs): 
    263263                pass 
     264 
    264265        OCHandler.wallet = self.wallet 
    265266        self.startInternet() 
    266267         
    267         #hack to open internet 
    268         #r = urllib.urlopen('http://google.com') 
    269         #ip = urllib.urlopen('http://opencoin.org/myownip').read() 
    270268        self.httpd = StoppableHTTPServer(("",port),OCHandler) 
    271269        self.feedback(u'Receiving coins: waiting at %s' % (self.ip),self.stopReceiveCoinsHTTP) 
     
    287285            addr,services=btsocket.bt_discover() 
    288286            if len(services)>0: 
    289                 #choices=services.keys() 
    290                 #choices.sort() 
    291                 #choice=appuifw.popup_menu([unicode(services[x])+": "+x for x in choices],u'Choose port:') 
    292                 #port=services[choices[choice]] 
    293287                port = services[u'opencoin'] 
    294288            else: 
     
    490484             restore=7,right=8,save=9,up=10,zoom=11) 
    491485icons = dict([(k,appuifw.Icon(mediapath+u'ocicons.mbm',v*2,v*2+1)) for k,v in names.items()]) 
    492 startup('network') 
     486 
     487startup('storage') 
     488from oc2 import storage as oc2storage 
     489password = '' 
     490while 1: 
     491    password = appuifw.query(u'password','text') 
     492 
     493    if password == None: 
     494        sys.exit()    
     495    startup('encrypted data') 
     496    storage = oc2storage.CryptedStorage() 
     497    storage.setPassword(password) 
     498    storage.setFilename('wallet.bin') 
     499    try: 
     500        storage.restore() 
     501        break 
     502    except: 
     503        pass 
     504 
     505 
     506startup('netlib') 
    493507import httplib, urllib 
    494508startup('ui') 
     
    497511from key_codes import EKeyLeftArrow, EKeyRightArrow 
    498512 
    499 startup('storage') 
    500 from oc2 import storage 
    501513startup('oc wallet') 
    502514from oc2 import wallet 
     
    512524#appuifw.app.screen='full' 
    513525startup('coins') 
    514 storage = storage.Storage() 
    515 storage.setFilename('wallet.bin') 
    516 storage.restore() 
    517526startup('done') 
    518527w = WalletClient(storage) 
  • trunk/sandbox/jhb/oc2/storage.py

    r279 r318  
    1 import UserDict, cPickle 
     1import UserDict, cPickle, blowfish, random, os, base64 
    22class Storage(UserDict.UserDict): 
    33 
     
    1717        return self 
    1818 
     19class CryptedStorage(Storage): 
     20 
     21    prefix = 'salt' 
     22 
     23    def setPassword(self,password): 
     24        self.password = password 
     25 
     26 
     27    def decrypt(self,password,salt,text): 
     28        key = str(password+salt) 
     29        cipher = blowfish.Blowfish(key) 
     30        cipher.initCTR() 
     31        return cipher.decryptCTR(text) 
     32 
     33    def encrypt(self,password,salt,text):        
     34        key = str(password+salt) 
     35        cipher = blowfish.Blowfish(key) 
     36        cipher.initCTR() 
     37        return cipher.encryptCTR(text) 
     38 
     39 
     40    def save(self): 
     41        data = 'opencoin'+base64.b64encode(cPickle.dumps(self.data)) 
     42        salt = ''.join([str(random.randint(0,9)) for i in range(0,16)]) 
     43        crypted = self.encrypt(self.password,salt,data) 
     44        content = '%s%s%s' % (self.prefix,salt,base64.b64encode(crypted)) 
     45        open(self.filename,'w').write(content) 
     46        return self 
     47 
     48 
     49    def restore(self): 
     50        if os.path.exists(self.filename): 
     51            content = open(self.filename).read() 
     52            if content.startswith(self.prefix): 
     53                salt = content[4:20] 
     54                crypted = base64.b64decode(content[20:]) 
     55                data = self.decrypt(self.password, salt, crypted) 
     56                if data.startswith('opencoin'): 
     57                    data = data[8:] 
     58                else: 
     59                    raise 'wrong password' 
     60                data = base64.b64decode(data)                     
     61            else: 
     62                data = content 
     63             
     64            self.data = cPickle.loads(data) 
     65        return self             
     66                             
     67 
     68 
     69     
  • trunk/sandbox/jhb/oc2/wallet.py

    r306 r318  
    131131        if amount != int(orig.amount): 
    132132            answer = messages.SpendReject() 
    133             answer.reason = 'amount of coins does not match announced one' 
     133            answer.reason = 'amount of coins does not match announced one. Announced: %s, got %s' % (orig.amount, amount) 
    134134            return answer 
    135135        #do exchange