From b2fa6fbc17889619d6cbc1e1b3b86d2302ccc37b Mon Sep 17 00:00:00 2001 From: Imelstorm Date: Tue, 22 Jan 2019 16:46:25 +0200 Subject: [PATCH] Optimize hash calculation usage --- virgil_crypto/crypto.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/virgil_crypto/crypto.py b/virgil_crypto/crypto.py index a01aa0e..6d3443b 100644 --- a/virgil_crypto/crypto.py +++ b/virgil_crypto/crypto.py @@ -401,12 +401,7 @@ def calculate_fingerprint(self, data): Returns: Fingerprint of the source data. """ - if self.use_sha256_fingerprints: - hash_algorithm = HashAlgorithm.SHA256 - hash_data = self.compute_hash(data, hash_algorithm) - else: - hash_algorithm = HashAlgorithm.SHA512 - hash_data = self.compute_hash(data, hash_algorithm)[0:8] + hash_data = self.__calculate_hash(data) return Fingerprint(hash_data) @@ -438,13 +433,8 @@ def compute_public_key_hash(self, public_key): Hash bytes. """ public_key_der = VirgilKeyPair.publicKeyToDER(public_key) - if self.use_sha256_fingerprints: - hash_algorithm = HashAlgorithm.SHA256 - computed_hash = self.compute_hash(public_key_der, hash_algorithm) - else: - hash_algorithm = HashAlgorithm.SHA512 - computed_hash = self.compute_hash(public_key_der, hash_algorithm)[0:8] - return computed_hash + hash_data = self.__calculate_hash(public_key_der) + return hash_data @property def custom_param_key_signature(self): @@ -458,3 +448,12 @@ def custom_param_key_signature(self): return self._CUSTOM_PARAM_KEY_SIGNATURE self._CUSTOM_PARAM_KEY_SIGNATURE = self.strtobytes("VIRGIL-DATA-SIGNATURE") return self._CUSTOM_PARAM_KEY_SIGNATURE + + def __calculate_hash(self, data): + if self.use_sha256_fingerprints: + hash_algorithm = HashAlgorithm.SHA256 + calculated_hash = self.compute_hash(data, hash_algorithm) + else: + hash_algorithm = HashAlgorithm.SHA512 + calculated_hash = self.compute_hash(data, hash_algorithm)[0:8] + return calculated_hash