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
Package Registry
Model registry
Operate
Environments
Terraform modules
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
Renaud Boyer
swh-model
Commits
5ca5dce7
Verified
Commit
5ca5dce7
authored
6 years ago
by
Antoine R. Dumont
Browse files
Options
Downloads
Patches
Plain Diff
hashutil: Allow option to require hexdigest instead of binary digest
Related T421
parent
2034fed5
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
-3
9 additions, 3 deletions
swh/model/hashutil.py
swh/model/tests/test_hashutil.py
+7
-0
7 additions, 0 deletions
swh/model/tests/test_hashutil.py
with
16 additions
and
3 deletions
swh/model/hashutil.py
+
9
−
3
View file @
5ca5dce7
...
...
@@ -162,7 +162,8 @@ def _new_hash(algo, length=None):
return
_new_hashlib_hash
(
algo
)
def
hash_file
(
fobj
,
length
=
None
,
algorithms
=
DEFAULT_ALGORITHMS
,
chunk_cb
=
None
):
def
hash_file
(
fobj
,
length
=
None
,
algorithms
=
DEFAULT_ALGORITHMS
,
chunk_cb
=
None
,
hexdigest
=
False
):
"""
Hash the contents of the given file object with the given algorithms.
Args:
...
...
@@ -171,11 +172,14 @@ def hash_file(fobj, length=None, algorithms=DEFAULT_ALGORITHMS, chunk_cb=None):
git-specific algorithms)
algorithms: the hashing algorithms to be used, as an iterable over
strings
hexdigest (bool): False returns the hash as binary, otherwise
returns as hex
Returns: a dict mapping each algorithm to a
bytes
digest.
Returns: a dict mapping each algorithm to a digest
(bytes by default)
.
Raises:
ValueError if algorithms contains an unknown hash algorithm.
"""
hashes
=
{
algo
:
_new_hash
(
algo
,
length
)
for
algo
in
algorithms
}
...
...
@@ -188,6 +192,8 @@ def hash_file(fobj, length=None, algorithms=DEFAULT_ALGORITHMS, chunk_cb=None):
if
chunk_cb
:
chunk_cb
(
chunk
)
if
hexdigest
:
return
{
algo
:
hash
.
hexdigest
()
for
algo
,
hash
in
hashes
.
items
()}
return
{
algo
:
hash
.
digest
()
for
algo
,
hash
in
hashes
.
items
()}
...
...
@@ -209,7 +215,7 @@ def hash_path(path, algorithms=DEFAULT_ALGORITHMS, chunk_cb=None):
"""
length
=
os
.
path
.
getsize
(
path
)
with
open
(
path
,
'
rb
'
)
as
fobj
:
hash
=
hash_file
(
fobj
,
length
,
algorithms
,
chunk_cb
)
hash
=
hash_file
(
fobj
,
length
,
algorithms
,
chunk_cb
=
chunk_cb
)
hash
[
'
length
'
]
=
length
return
hash
...
...
This diff is collapsed.
Click to expand it.
swh/model/tests/test_hashutil.py
+
7
−
0
View file @
5ca5dce7
...
...
@@ -95,6 +95,13 @@ class Hashutil(unittest.TestCase):
checksums
=
hashutil
.
hash_file
(
fobj
,
length
=
len
(
self
.
data
))
self
.
assertEqual
(
checksums
,
self
.
checksums
)
@istest
def
hash_file_hexdigest
(
self
):
fobj
=
io
.
BytesIO
(
self
.
data
)
checksums
=
hashutil
.
hash_file
(
fobj
,
length
=
len
(
self
.
data
),
hexdigest
=
True
)
self
.
assertEqual
(
checksums
,
self
.
hex_checksums
)
@istest
def
hash_file_missing_length
(
self
):
fobj
=
io
.
BytesIO
(
self
.
data
)
...
...
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