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
8bafb407
Commit
8bafb407
authored
7 years ago
by
Nicolas Dandrimont
Browse files
Options
Downloads
Patches
Plain Diff
hashutil: improve docstrings
parent
bd43a7f6
No related branches found
Branches containing commit
No related tags found
Tags containing commit
Loading
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
swh/model/hashutil.py
+43
-10
43 additions, 10 deletions
swh/model/hashutil.py
with
43 additions
and
10 deletions
swh/model/hashutil.py
+
43
−
10
View file @
8bafb407
...
@@ -31,15 +31,17 @@ import os
...
@@ -31,15 +31,17 @@ import os
from
io
import
BytesIO
from
io
import
BytesIO
# Supported algorithms
ALGORITHMS
=
set
([
'
sha1
'
,
'
sha256
'
,
'
sha1_git
'
,
'
blake2s256
'
,
'
blake2b512
'
])
ALGORITHMS
=
set
([
'
sha1
'
,
'
sha256
'
,
'
sha1_git
'
,
'
blake2s256
'
,
'
blake2b512
'
])
"""
Hashing algorithms supported by this module
"""
# Default algorithms used
DEFAULT_ALGORITHMS
=
set
([
'
sha1
'
,
'
sha256
'
,
'
sha1_git
'
,
'
blake2s256
'
])
DEFAULT_ALGORITHMS
=
set
([
'
sha1
'
,
'
sha256
'
,
'
sha1_git
'
,
'
blake2s256
'
])
"""
Algorithms computed by default when calling the functions from this module.
Subset of :const:`ALGORITHMS`.
"""
# should be a multiple of 64 (sha1/sha256's block size)
# FWIW coreutils' sha1sum uses 32768
HASH_BLOCK_SIZE
=
32768
HASH_BLOCK_SIZE
=
32768
"""
Block size for streaming hash computations made in this module
"""
# Load blake2 hashes from pyblake2 if they are not available in the builtin
# Load blake2 hashes from pyblake2 if they are not available in the builtin
# hashlib
# hashlib
...
@@ -64,7 +66,7 @@ def _new_git_hash(base_algo, git_type, length):
...
@@ -64,7 +66,7 @@ def _new_git_hash(base_algo, git_type, length):
- One NUL byte
- One NUL byte
Args:
Args:
base_algo: a hashlib-supported algorithm
base_algo
(str from :const:`ALGORITHMS`)
: a hashlib-supported algorithm
git_type: the type of the git object (supposedly one of
'
blob
'
,
git_type: the type of the git object (supposedly one of
'
blob
'
,
'
commit
'
,
'
tag
'
,
'
tree
'
)
'
commit
'
,
'
tag
'
,
'
tree
'
)
length: the length of the git object you
'
re encoding
length: the length of the git object you
'
re encoding
...
@@ -90,7 +92,7 @@ def _new_hash(algo, length=None):
...
@@ -90,7 +92,7 @@ def _new_hash(algo, length=None):
Args:
Args:
algo (str): a hashing algorithm (one of ALGORITHMS)
algo (str): a hashing algorithm (one of ALGORITHMS)
length (int): the length of the hashed payload (needed for
length (int): the length of the hashed payload (needed for
git-specific algorithms)
git-specific algorithms)
Returns:
Returns:
a hashutil.hash object
a hashutil.hash object
...
@@ -210,7 +212,15 @@ def hash_git_data(data, git_type, base_algo='sha1'):
...
@@ -210,7 +212,15 @@ def hash_git_data(data, git_type, base_algo='sha1'):
@functools.lru_cache
()
@functools.lru_cache
()
def
hash_to_hex
(
hash
):
def
hash_to_hex
(
hash
):
"""
Converts a hash (in hex or bytes form) to its hexadecimal ascii form
"""
"""
Converts a hash (in hex or bytes form) to its hexadecimal ascii form
Args:
hash (str or bytes): a :class:`bytes` hash or a :class:`str` containing
the hexadecimal form of the hash
Returns:
str: the hexadecimal form of the hash
"""
if
isinstance
(
hash
,
str
):
if
isinstance
(
hash
,
str
):
return
hash
return
hash
return
binascii
.
hexlify
(
hash
).
decode
(
'
ascii
'
)
return
binascii
.
hexlify
(
hash
).
decode
(
'
ascii
'
)
...
@@ -218,13 +228,28 @@ def hash_to_hex(hash):
...
@@ -218,13 +228,28 @@ def hash_to_hex(hash):
@functools.lru_cache
()
@functools.lru_cache
()
def
hash_to_bytehex
(
hash
):
def
hash_to_bytehex
(
hash
):
"""
Converts a hash to its hexadecimal bytes representation
"""
"""
Converts a hash to its hexadecimal bytes representation
Args:
hash (bytes): a :class:`bytes` hash
Returns:
bytes: the hexadecimal form of the hash, as :class:`bytes`
"""
return
binascii
.
hexlify
(
hash
)
return
binascii
.
hexlify
(
hash
)
@functools.lru_cache
()
@functools.lru_cache
()
def
hash_to_bytes
(
hash
):
def
hash_to_bytes
(
hash
):
"""
Converts a hash (in hex or bytes form) to its raw bytes form
"""
"""
Converts a hash (in hex or bytes form) to its raw bytes form
Args:
hash (str or bytes): a :class:`bytes` hash or a :class:`str` containing
the hexadecimal form of the hash
Returns:
bytes: the :class:`bytes` form of the hash
"""
if
isinstance
(
hash
,
bytes
):
if
isinstance
(
hash
,
bytes
):
return
hash
return
hash
return
bytes
.
fromhex
(
hash
)
return
bytes
.
fromhex
(
hash
)
...
@@ -232,5 +257,13 @@ def hash_to_bytes(hash):
...
@@ -232,5 +257,13 @@ def hash_to_bytes(hash):
@functools.lru_cache
()
@functools.lru_cache
()
def
bytehex_to_hash
(
hex
):
def
bytehex_to_hash
(
hex
):
"""
Converts a hexadecimal bytes representation of a hash to that hash
"""
"""
Converts a hexadecimal bytes representation of a hash to that hash
Args:
hash (bytes): a :class:`bytes` containing the hexadecimal form of the
hash encoded in ascii
Returns:
bytes: the :class:`bytes` form of the hash
"""
return
hash_to_bytes
(
hex
.
decode
())
return
hash_to_bytes
(
hex
.
decode
())
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