move utility functions to their own module, better docs on remote_prefix

This commit is contained in:
meejah 2019-06-24 11:27:15 -06:00
parent 44cddc2ee3
commit a88b53825c
2 changed files with 34 additions and 21 deletions

View File

@ -1,21 +0,0 @@
class BadSignature(Exception):
"""
An alleged signature did not match
"""
class BadPrefixError(Exception):
"""
A key did not start with the required prefix
"""
def remove_prefix(s_bytes, prefix):
"""
Removes `prefix` from `s_bytes` safely
"""
if not s_bytes.startswith(prefix):
raise BadPrefixError(
"did not see expected '{}' prefix".format(prefix)
)
return s_bytes[len(prefix):]

View File

@ -0,0 +1,34 @@
"""
Utilities used by allmydata.crypto modules
"""
class BadSignature(Exception):
"""
An alleged signature did not match
"""
class BadPrefixError(Exception):
"""
A key did not start with the required prefix
"""
def remove_prefix(s_bytes, prefix):
"""
:param bytes s_bytes: a string of bytes whose prefix is removed
:param bytes prefix: the bytes to remove from the beginning of `s_bytes`
Removes `prefix` from `s_bytes` and returns the new bytes or
raises `BadPrefixError` if `s_bytes` did not start with the
`prefix` specified.
:returns: `s_bytes` with `prefix` removed from the front.
"""
if not s_bytes.startswith(prefix):
raise BadPrefixError(
"did not see expected '{}' prefix".format(prefix)
)
return s_bytes[len(prefix):]