# Path: src/petmail/errors.py
# class BadSignatureError(Exception):
#     pass


import os, base64, hashlib
from twisted.internet import tcp, protocol
from .errors import BadSignatureError
        s_bytes = base64.b16decode(s_ascii.upper())
    else:
        raise NotImplementedError
    return s_bytes

def make_nonce():
    return base64.b32encode(os.urandom(32)).strip("=").lower()

def equal(a, b):
    # not vulnerable to timing attack
    return hashlib.sha256(a).digest() == hashlib.sha256(b).digest()

def split_into(s, piece_sizes, plus_trailer=False):
    assert type(s) == type(b"")
    pieces = []
    piece_start = 0
    for size in piece_sizes:
        piece_end = piece_start+size
        pieces.append(s[piece_start:piece_end])
        piece_start = piece_end
    if plus_trailer:
        pieces.append(s[piece_start:])
    else:
        if piece_start != len(s):
            raise ValueError("split did not consume entire string")
    return pieces

def verify_with_prefix(vk, sm, prefix):
    m = vk.verify(sm)
    if not m.startswith(prefix):

# Path: src/petmail/errors.py
# class BadSignatureError(Exception):
#     pass


# Path: src/petmail/util.py
import os, base64, hashlib
from twisted.internet import tcp, protocol
from .errors import BadSignatureError

        s_bytes = base64.b16decode(s_ascii.upper())
    else:
        raise NotImplementedError
    return s_bytes

def make_nonce():
    return base64.b32encode(os.urandom(32)).strip("=").lower()

def equal(a, b):
    # not vulnerable to timing attack
    return hashlib.sha256(a).digest() == hashlib.sha256(b).digest()

def split_into(s, piece_sizes, plus_trailer=False):
    assert type(s) == type(b"")
    pieces = []
    piece_start = 0
    for size in piece_sizes:
        piece_end = piece_start+size
        pieces.append(s[piece_start:piece_end])
        piece_start = piece_end
    if plus_trailer:
        pieces.append(s[piece_start:])
    else:
        if piece_start != len(s):
            raise ValueError("split did not consume entire string")
    return pieces

def verify_with_prefix(vk, sm, prefix):
    m = vk.verify(sm)
    if not m.startswith(prefix):

        raise BadSignatureError("missing expected prefix")