def _doCreateKey(self): self.ui.stack.setCurrentWidget(self.ui.progressPage) self.ui.msgLabel.setText('Creating RSA Key...') self.repaint() self.rsaKey = RSAKey() self.rsaKey.generate(bits=2048) self._doRegisterKey()
def _onAddContactClicked(self): k = RSAKey() try: k.fromPEM_PublicKey(self.pemPublicKey) except RSAError: self._showError('Invalid public key.') self.ui.publicKey.setFocus() self.ui.publicKey.selectAll() return if not isValidUserName(self.contactName): self._showError('Only lowercase alphabets(a-z), ' + 'digits(0-9), and underscore(\'_\') are allowed ' + 'in the contact name.') self.ui.contactName.setFocus() self.ui.contactName.selectAll() return contact = self.profile.getContactByPublicKey(k) if contact: self._showError('This public key is already present in ' + 'your contact list as \'%s\'.' % contact.name) self.ui.publicKey.setFocus() self.ui.publicKey.selectAll() return contact = self.profile.getContactByName(self.contactName) if contact: self._showError('This name is already present in your ' + 'contact list.\nPlease choose a different name.') self.ui.contactName.setFocus() self.ui.contactName.selectAll() return contact = Contact(k, self.contactName) self.addContactCallback(contact) self.accept()
def loadProfile(entry, password): ps = profileSettings() userName = ps.getData(entry + '/Name') keyId = ps.getData(entry + '/KeyID') encKey = ps.getData(entry + '/PrivateKey') rsaKey = RSAKey() try: rsaKey.fromPEM_PrivateKey(encKey, password) except RSAError: return None profile = Profile(rsaKey, userName, keyId, entry) contactsData = ps.getData(entry + '/ContactList', '') for line in contactsData.split('\n'): line = line.strip() if not line: continue name, hexKey = line.split(':') assert isValidUserName(name) pubKey = RSAKey() pubKey.fromDER_PublicKey(hexDecode(hexKey)) contact = Contact(pubKey, name) profile.addContact(contact) return profile
def getPublicKey( name, addr ) : s = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) s.connect( addr ) s.send( 'getpubkey %s\n' % name ) line = '' while 1 : data = s.recv( 1 ) line += data if data == '\n' : break s.close() result,pubKeyData = line.split() assert result == 'OK' publicKey = RSAKey() publicKey.fromDER_PublicKey( pubKeyData.decode('hex') ) return publicKey
def verifySignature(publicKey, data, updateLevel, signature): payload = encode(('DHT-DATA', data, updateLevel)) if type(publicKey) is str: k = RSAKey() try: k.fromDER_PublicKey(publicKey) except RSAError: return False else: k = publicKey try: digest = Digest(digestType).digest(payload) k.verify(signature, digest, digestType) return True except RSAError: return False
def getProfile(): key = RSAKey() key.fromPEM_PrivateKey(file('ks.key').read()) profile = UserProfile(key, 'ks') return profile