Skip to content

Commit

Permalink
Optimize hash calculation usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kmuzychko committed Jan 22, 2019
1 parent 2104171 commit b2fa6fb
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions virgil_crypto/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand All @@ -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

0 comments on commit b2fa6fb

Please sign in to comment.