Skip to content

Commit

Permalink
Remove TUF-specific util.get_target_hash function
Browse files Browse the repository at this point in the history
Also remove now obsolete util.HASH_FUNCTION and corresponding
tests.

The function is added back to tuf in theupdateframework/python-tuf#909.
  • Loading branch information
lukpueh committed Sep 5, 2019
1 parent 318f90a commit 532fc20
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 64 deletions.
46 changes: 0 additions & 46 deletions securesystemslib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@

import six

# The algorithm used by the repository to generate the digests of the
# target filepaths, which are included in metadata files and may be prepended
# to the filenames of consistent snapshots.
HASH_FUNCTION = 'sha256'

logger = logging.getLogger('securesystemslib_util')


Expand Down Expand Up @@ -486,48 +481,7 @@ def file_in_confined_directories(filepath, confined_directories):
return False


# TODO: Move get_target_hash back to TUF; it's TUF-specific.
def get_target_hash(target_filepath):
"""
<Purpose>
Compute the hash of 'target_filepath'. This is useful in conjunction with
the "path_hash_prefixes" attribute in a delegated targets role, which tells
us which paths it is implicitly responsible for.
The repository may optionally organize targets into hashed bins to ease
target delegations and role metadata management. The use of consistent
hashing allows for a uniform distribution of targets into bins.
<Arguments>
target_filepath:
The path to the target file on the repository. This will be relative to
the 'targets' (or equivalent) directory on a given mirror.
<Exceptions>
None.
<Side Effects>
None.
<Returns>
The hash of 'target_filepath'.
"""

# Does 'target_filepath' have the correct format?
# Ensure the arguments have the appropriate number of objects and object
# types, and that all dict keys are properly named.
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
securesystemslib.formats.PATH_SCHEMA.check_match(target_filepath)

# Calculate the hash of the filepath to determine which bin to find the
# target. The client currently assumes the repository uses
# 'HASH_FUNCTION' to generate hashes and 'utf-8'.
digest_object = securesystemslib.hash.digest(HASH_FUNCTION)
encoded_target_filepath = target_filepath.encode('utf-8')
digest_object.update(encoded_target_filepath)
target_filepath_hash = digest_object.hexdigest()

return target_filepath_hash


_json_module = None
Expand Down
18 changes: 0 additions & 18 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,24 +385,6 @@ def test_B6_load_json_file(self):



def test_C1_get_target_hash(self):
# Test normal case.
expected_target_hashes = {
'/file1.txt': 'e3a3d89eb3b70ce3fbce6017d7b8c12d4abd5635427a0e8a238f53157df85b3d',
'/README.txt': '8faee106f1bb69f34aaf1df1e3c2e87d763c4d878cb96b91db13495e32ceb0b0',
'/warehouse/file2.txt': 'd543a573a2cec67026eff06e75702303559e64e705eba06f65799baaf0424417'
}
for filepath, target_hash in six.iteritems(expected_target_hashes):
self.assertTrue(securesystemslib.formats.PATH_SCHEMA.matches(filepath))
self.assertTrue(securesystemslib.formats.HASH_SCHEMA.matches(target_hash))
self.assertEqual(securesystemslib.util.get_target_hash(filepath), target_hash)

# Test for improperly formatted argument.
self.assertRaises(securesystemslib.exceptions.FormatError,
securesystemslib.util.get_target_hash, 8)



def test_C5_unittest_toolbox_make_temp_directory(self):
# Verify that the tearDown function does not fail when
# unittest_toolbox.make_temp_directory deletes the generated temp directory
Expand Down

0 comments on commit 532fc20

Please sign in to comment.