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 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 _doConnectPubKey( self, words ) : if len(words) != 2 : self._writeError( 'Malformed request' ) return hexPubKey, service = words if not self.session.isOnline() : self._writeError( 'Not online' ) return try : pubKeyData = hexDecode( hexPubKey ) pubKey = RSAKey() pubKey.fromDER_PublicKey( pubKeyData ) except (HexDecodeError,RSAError) : self._writeError( 'Malformed publickey' ) return self._connectInternal( pubKey, service )
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