# Path: src/petmail/util.py
# 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


import os
from hashlib import sha256
from .util import split_into


TOKEN_LENGTH = 32+32+32

# actual crypto is stubbed out for now

def create_keypair():
    privkey = "\x11"*32
    pubkey = "\x22"*32
    return privkey, pubkey

def create_token(pubkey):
    message = os.urandom(32)
    tokenid = sha256(message).digest()
    IDENTITY_ELEMENT = "\x00"*32
    # encrypting with r=0 gives (IDENTITY_ELEMENT,m), which is suitable for
    # the first token (which is not encrypted), and can also be fed into
    # rerandomize().
    nullencrypted_token = pubkey + IDENTITY_ELEMENT + message
    return tokenid, nullencrypted_token

def decrypt(privkey, token):
    assert len(privkey) == 32
    assert privkey == "\x11"*32
    pubkey, C1, C2 = split_into(token, [32,32,32])
    message = C2
    tokenid = sha256(message).digest()
    return tokenid

def randomize(token):

# Path: src/petmail/util.py
# 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


# Path: src/petmail/rrid.py
import os
from hashlib import sha256
from .util import split_into



TOKEN_LENGTH = 32+32+32

# actual crypto is stubbed out for now

def create_keypair():
    privkey = "\x11"*32
    pubkey = "\x22"*32
    return privkey, pubkey

def create_token(pubkey):
    message = os.urandom(32)
    tokenid = sha256(message).digest()
    IDENTITY_ELEMENT = "\x00"*32
    # encrypting with r=0 gives (IDENTITY_ELEMENT,m), which is suitable for
    # the first token (which is not encrypted), and can also be fed into
    # rerandomize().
    nullencrypted_token = pubkey + IDENTITY_ELEMENT + message
    return tokenid, nullencrypted_token

def decrypt(privkey, token):
    assert len(privkey) == 32
    assert privkey == "\x11"*32
    pubkey, C1, C2 = split_into(token, [32,32,32])
    message = C2
    tokenid = sha256(message).digest()
    return tokenid

def randomize(token):

    pubkey, C1, C2 = split_into(token, [32,32,32])