comparison libervia/backend/plugins/plugin_xep_0300.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4b842c1fb686
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
48 C.PI_DESCRIPTION: _("""Management of cryptographic hashes"""), 48 C.PI_DESCRIPTION: _("""Management of cryptographic hashes"""),
49 } 49 }
50 50
51 NS_HASHES = "urn:xmpp:hashes:2" 51 NS_HASHES = "urn:xmpp:hashes:2"
52 NS_HASHES_FUNCTIONS = "urn:xmpp:hash-function-text-names:{}" 52 NS_HASHES_FUNCTIONS = "urn:xmpp:hash-function-text-names:{}"
53 BUFFER_SIZE = 2 ** 12 53 BUFFER_SIZE = 2**12
54 ALGO_DEFAULT = "sha-256" 54 ALGO_DEFAULT = "sha-256"
55 55
56 56
57 class XEP_0300(object): 57 class XEP_0300(object):
58 # TODO: add blake after moving to Python 3 58 # TODO: add blake after moving to Python 3
88 88
89 @defer.inlineCallbacks 89 @defer.inlineCallbacks
90 def get_best_peer_algo(self, to_jid, profile): 90 def get_best_peer_algo(self, to_jid, profile):
91 """Return the best available hashing algorith of other peer 91 """Return the best available hashing algorith of other peer
92 92
93 @param to_jid(jid.JID): peer jid 93 @param to_jid(jid.JID): peer jid
94 @parm profile: %(doc_profile)s 94 @parm profile: %(doc_profile)s
95 @return (D(unicode, None)): best available algorithm, 95 @return (D(unicode, None)): best available algorithm,
96 or None if hashing is not possible 96 or None if hashing is not possible
97 """ 97 """
98 client = self.host.get_client(profile) 98 client = self.host.get_client(profile)
99 for algo in reversed(XEP_0300.ALGOS): 99 for algo in reversed(XEP_0300.ALGOS):
100 has_feature = yield self.host.hasFeature( 100 has_feature = yield self.host.hasFeature(
101 client, NS_HASHES_FUNCTIONS.format(algo), to_jid 101 client, NS_HASHES_FUNCTIONS.format(algo), to_jid
173 """ 173 """
174 assert hash_ 174 assert hash_
175 assert algo 175 assert algo
176 hash_elt = domish.Element((NS_HASHES, "hash")) 176 hash_elt = domish.Element((NS_HASHES, "hash"))
177 if hash_ is not None: 177 if hash_ is not None:
178 b64_hash = base64.b64encode(hash_.encode('utf-8')).decode('utf-8') 178 b64_hash = base64.b64encode(hash_.encode("utf-8")).decode("utf-8")
179 hash_elt.addContent(b64_hash) 179 hash_elt.addContent(b64_hash)
180 hash_elt["algo"] = algo 180 hash_elt["algo"] = algo
181 return hash_elt 181 return hash_elt
182 182
183 def parse_hash_elt(self, parent: domish.Element) -> Tuple[str, bytes]: 183 def parse_hash_elt(self, parent: domish.Element) -> Tuple[str, bytes]:
203 algo = None 203 algo = None
204 continue 204 continue
205 205
206 if best_algo is None or algos.index(best_algo) < idx: 206 if best_algo is None or algos.index(best_algo) < idx:
207 best_algo = algo 207 best_algo = algo
208 best_value = base64.b64decode(str(hash_elt)).decode('utf-8') 208 best_value = base64.b64decode(str(hash_elt)).decode("utf-8")
209 209
210 if not hash_elt: 210 if not hash_elt:
211 raise exceptions.NotFound 211 raise exceptions.NotFound
212 if not best_algo or not best_value: 212 if not best_algo or not best_value:
213 raise exceptions.DataError 213 raise exceptions.DataError