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
eb338cda
Verified
Commit
eb338cda
authored
6 years ago
by
Antoine R. Dumont
Browse files
Options
Downloads
Patches
Plain Diff
hashutil: Clarify further the module dostring
parent
8c26ddb0
No related branches found
Branches containing commit
Tags
v0.0.27
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
swh/model/hashutil.py
+19
-6
19 additions, 6 deletions
swh/model/hashutil.py
with
19 additions
and
6 deletions
swh/model/hashutil.py
+
19
−
6
View file @
eb338cda
...
...
@@ -10,24 +10,33 @@ Only a subset of hashing algorithms is supported as defined in the
ALGORITHMS set. Any provided algorithms not in that list will result
in a ValueError explaining the error.
This module defines MultiHash class to ease the softwareheritage
hashing algorithm. This allows as before (with hash_* function) to
compute hashes from file object, path, data.
This module defines a MultiHash class to ease the softwareheritage
hashing algorithms computation. This allows to compute hashes from
file object, path, data using a similar interface as what the standard
hashlib module provides.
Basic usage examples:
- file object: MultiHash.from_file(file_object).digest()
- file object: MultiHash.from_file(
file_object, hash_names=DEFAULT_ALGORITHMS).digest()
- path (filepath): MultiHash.from_path(b
'
foo
'
).hexdigest()
- data (bytes): MultiHash.from_data(b
'
foo
'
).bytehexdigest()
Complex usage (old use was through callback):
"
Complex
"
usage, defining a swh hashlib instance first:
- To compute length, integrate the length to the set of algorithms to
compute, for example:
h = MultiHash(hash_names=set({
'
length
'
}).union(DEFAULT_ALGORITHMS))
h = MultiHash(hash_names=set({
'
length
'
}).union(DEFAULT_ALGORITHMS))
with open(filepath,
'
rb
'
) as f:
h.update(f.read(HASH_BLOCK_SIZE))
hashes = h.digest() # returns a dict of {hash_algo_name: hash_in_bytes}
for chunk in
# then use h as you would
- Write alongside computing hashing algorithms (from a stream), example:
...
...
@@ -36,6 +45,10 @@ Complex usage (old use was through callback):
for chunk in r.iter_content(): # r a stream of sort
h.update(chunk)
f.write(chunk)
hashes = h.hexdigest() # returns a dict of {hash_algo_name: hash_in_hex}
Note: Prior to this, we would have to use chunk_cb (cf. hash_file,
hash_path)
This module also defines the following (deprecated) hashing functions:
...
...
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