Skip to content

Commit bafebee

Browse files
author
lukpueh
authored
Merge pull request #909 from lukpueh/add-target-hash-function
Move repository_lib.get_taget_hash back from sslib to tuf
2 parents 011a08c + 7186625 commit bafebee

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

tuf/repository_lib.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import tuf.settings
5050

5151
import securesystemslib
52+
import securesystemslib.hash
5253
import securesystemslib.interface
5354
import iso8601
5455
import six
@@ -1174,6 +1175,8 @@ def get_metadata_versioninfo(rolename, repository_name):
11741175

11751176

11761177

1178+
# TODO: Is this function needed? It does not seem used, also the same
1179+
# function exists as private method in updater.Updater._get_target_hash.
11771180
def get_target_hash(target_filepath):
11781181
"""
11791182
<Purpose>
@@ -1198,9 +1201,20 @@ def get_target_hash(target_filepath):
11981201
11991202
<Returns>
12001203
The hash of 'target_filepath'.
1201-
"""
12021204
1203-
return securesystemslib.util.get_target_hash(target_filepath)
1205+
"""
1206+
securesystemslib.formats.RELPATH_SCHEMA.check_match(target_filepath)
1207+
1208+
# Calculate the hash of the filepath to determine which bin to find the
1209+
# target. The client currently assumes the repository uses
1210+
# 'tuf.settings.DEFAULT_HASH_ALGORITHM' to generate hashes and 'utf-8'.
1211+
digest_object = securesystemslib.hash.digest(
1212+
tuf.settings.DEFAULT_HASH_ALGORITHM)
1213+
encoded_target_filepath = target_filepath.encode('utf-8')
1214+
digest_object.update(encoded_target_filepath)
1215+
target_filepath_hash = digest_object.hexdigest()
1216+
1217+
return target_filepath_hash
12041218

12051219

12061220

tuf/settings.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@
9797

9898
# A setting for the instances where a default hashing algorithm is needed.
9999
# This setting is currently used to calculate the path hash prefixes of hashed
100-
# bin delegations. The other instances (e.g., digest of files) that require a
101-
# hashing algorithm rely on settings in the securesystemslib external library.
100+
# bin delegations, and digests of targets filepaths. The other instances
101+
# (e.g., digest of files) that require a hashing algorithm rely on settings in
102+
# the securesystemslib external library.
102103
DEFAULT_HASH_ALGORITHM = 'sha256'
103104

104105
# The client's update procedure (contained within a while-loop) can potentially

0 commit comments

Comments
 (0)