Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def read_keyset(keyset_filename):
"""Load a keyset from a file.
Args:
keyset_filename: A path to a keyset file
Returns:
A KeysetHandle of the file's keyset
Raises:
TinkError: if the file is not valid
IOError: if the file does not exist
"""
with open(keyset_filename, 'rb') as keyset_file:
text = keyset_file.read()
keyset = cleartext_keyset_handle.CleartextKeysetHandle(
tink.BinaryKeysetReader(text).read())
return keyset
expected_code_hex = None
logging.info('Using keyset from file %s to verify file %s', keyset_filename,
data_filename)
# Initialise Tink.
try:
tink.tink_config.register()
except tink.TinkError as e:
logging.error('Error initialising Tink: %s', e)
return 1
# Read the keyset.
with open(keyset_filename, 'rb') as keyset_file:
try:
text = keyset_file.read()
keyset = cleartext_keyset_handle.CleartextKeysetHandle(
tink.JsonKeysetReader(text).read())
except tink.TinkError as e:
logging.error('Error reading key: %s', e)
return 1
# Get the primitive.
try:
cipher = keyset.primitive(tink.Mac)
except tink.TinkError as e:
logging.error('Error creating primitive: %s', e)
return 1
# Compute the MAC.
with open(data_filename, 'rb') as data_file:
data = data_file.read()