Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-model
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Platform
Development
swh-model
Commits
91d74ef0
Verified
Commit
91d74ef0
authored
7 years ago
by
Antoine R. Dumont
Browse files
Options
Downloads
Patches
Plain Diff
swh.model.hashutil.hash_data: Optionally integrate length in result
parent
eff2692a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
swh/model/hashutil.py
+9
-4
9 additions, 4 deletions
swh/model/hashutil.py
swh/model/tests/test_hashutil.py
+11
-0
11 additions, 0 deletions
swh/model/tests/test_hashutil.py
with
20 additions
and
4 deletions
swh/model/hashutil.py
+
9
−
4
View file @
91d74ef0
...
...
@@ -167,12 +167,13 @@ def hash_path(path, algorithms=DEFAULT_ALGORITHMS, chunk_cb=None):
return
hash
def
hash_data
(
data
,
algorithms
=
DEFAULT_ALGORITHMS
):
def
hash_data
(
data
,
algorithms
=
DEFAULT_ALGORITHMS
,
with_length
=
False
):
"""
Hash the given binary blob with the given algorithms.
Args:
data: a bytes object
algorithms: the hashing algorithms used
data (bytes): raw content to hash
algorithms (list): the hashing algorithms used
with_length (bool): add the length key in the resulting dict
Returns: a dict mapping each algorithm to a bytes digest
...
...
@@ -181,7 +182,11 @@ def hash_data(data, algorithms=DEFAULT_ALGORITHMS):
ValueError if algorithms contains an unknown hash algorithm.
"""
fobj
=
BytesIO
(
data
)
return
hash_file
(
fobj
,
len
(
data
),
algorithms
)
length
=
len
(
data
)
data
=
hash_file
(
fobj
,
length
,
algorithms
)
if
with_length
:
data
[
'
length
'
]
=
length
return
data
def
hash_git_data
(
data
,
git_type
,
base_algo
=
'
sha1
'
):
...
...
This diff is collapsed.
Click to expand it.
swh/model/tests/test_hashutil.py
+
11
−
0
View file @
91d74ef0
...
...
@@ -47,6 +47,17 @@ class Hashutil(unittest.TestCase):
def
hash_data
(
self
):
checksums
=
hashutil
.
hash_data
(
self
.
data
)
self
.
assertEqual
(
checksums
,
self
.
checksums
)
self
.
assertFalse
(
'
length
'
in
checksums
)
@istest
def
hash_data_with_length
(
self
):
expected_checksums
=
self
.
checksums
.
copy
()
expected_checksums
[
'
length
'
]
=
len
(
self
.
data
)
checksums
=
hashutil
.
hash_data
(
self
.
data
,
with_length
=
True
)
self
.
assertEqual
(
checksums
,
expected_checksums
)
self
.
assertTrue
(
'
length
'
in
checksums
)
@istest
def
hash_data_unknown_hash
(
self
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment