comparison sat/plugins/plugin_xep_0300.py @ 3286:ddf3ded93b78

plugin XEP-0300; fixed use of calculateHash with getHasher
author Goffi <goffi@goffi.org>
date Fri, 29 May 2020 21:06:10 +0200
parents 559a625a236b
children be6d91572633
comparison
equal deleted inserted replaced
3285:4240b44842bb 3286:ddf3ded93b78
107 def _calculateHashBlocking(self, file_obj, hasher): 107 def _calculateHashBlocking(self, file_obj, hasher):
108 """Calculate hash in a blocking way 108 """Calculate hash in a blocking way
109 109
110 /!\\ blocking method, please use calculateHash instead 110 /!\\ blocking method, please use calculateHash instead
111 @param file_obj(file): a file-like object 111 @param file_obj(file): a file-like object
112 @param hasher(callable): the method to call to initialise hash object 112 @param hasher(hash object): the method to call to initialise hash object
113 @return (str): the hex digest of the hash 113 @return (str): the hex digest of the hash
114 """ 114 """
115 hash_ = hasher()
116 while True: 115 while True:
117 buf = file_obj.read(BUFFER_SIZE) 116 buf = file_obj.read(BUFFER_SIZE)
118 if not buf: 117 if not buf:
119 break 118 break
120 hash_.update(buf) 119 hasher.update(buf)
121 return hash_.hexdigest() 120 return hasher.hexdigest()
122 121
123 def calculateHash(self, file_obj, hasher): 122 def calculateHash(self, file_obj, hasher):
124 return threads.deferToThread(self._calculateHashBlocking, file_obj, hasher) 123 return threads.deferToThread(self._calculateHashBlocking, file_obj, hasher)
125 124
126 def calculateHashElt(self, file_obj=None, algo=ALGO_DEFAULT): 125 def calculateHashElt(self, file_obj=None, algo=ALGO_DEFAULT):
132 """ 131 """
133 132
134 def hashCalculated(hash_): 133 def hashCalculated(hash_):
135 return self.buildHashElt(hash_, algo) 134 return self.buildHashElt(hash_, algo)
136 135
137 hasher = self.ALGOS[algo] 136 hasher = self.getHasher(algo)
138 hash_d = self.calculateHash(file_obj, hasher) 137 hash_d = self.calculateHash(file_obj, hasher)
139 hash_d.addCallback(hashCalculated) 138 hash_d.addCallback(hashCalculated)
140 return hash_d 139 return hash_d
141 140
142 def buildHashUsedElt(self, algo=ALGO_DEFAULT): 141 def buildHashUsedElt(self, algo=ALGO_DEFAULT):