<commit_before>package net.lshift.spki.suiteb.demo;

import static net.lshift.spki.convert.openable.OpenableUtils.read;
import static net.lshift.spki.convert.openable.OpenableUtils.write;
import static net.lshift.spki.suiteb.SequenceUtils.sequence;
import static net.lshift.spki.suiteb.Signed.signed;

import java.io.IOException;

import net.lshift.spki.InvalidInputException;
import net.lshift.spki.convert.ReadInfo;
import net.lshift.spki.convert.openable.ByteOpenable;
import net.lshift.spki.suiteb.Action;
import net.lshift.spki.suiteb.EncryptionCache;
import net.lshift.spki.suiteb.PrivateEncryptionKey;
import net.lshift.spki.suiteb.PrivateSigningKey;
import net.lshift.spki.suiteb.PublicEncryptionKey;
import net.lshift.spki.suiteb.PublicSigningKey;
import net.lshift.spki.suiteb.Sequence;

public class Server {
    private static ReadInfo R = ReadInfo.BASE.extend(Service.class);
    private EncryptionCache ephemeral;

    private final PrivateSigningKey signingKey;
    private ByteOpenable certificate;

    public Server() {
        signingKey = PrivateSigningKey.generate();
        ephemeral = new EncryptionCache(PrivateEncryptionKey.generate());
    }

    public PublicSigningKey getPublicSigningKey() {
        return signingKey.getPublicKey();
    }

    public void setCertificate(ByteOpenable certificate) {
        this.certificate = certificate;
    }

    public ByteOpenable generateMessage(Service service) throws IOException,
            InvalidInputException {
        return asOpenable(signedMessage(service));
    }

    public ByteOpenable generateEncryptedMessageFor(Service service,
            PublicEncryptionKey recipient) throws IOException,
            InvalidInputException {
        return asOpenable(sequence(ephemeral.getPublicKey(),
                ephemeral.encrypt(recipient, signedMessage(service))));
    }

    private Sequence signedMessage(Service service) throws IOException,
            InvalidInputException {
        Sequence message = sequence(getPublicSigningKey(),
                signed(signingKey, new Action(service)));
        if (certificate != null) {
            message = sequence(read(R, certificate), message);
        }
        return message;
    }

    private ByteOpenable asOpenable(Sequence sequence) throws IOException {
        final ByteOpenable target = new ByteOpenable();
        write(target, sequence);
        return target;
    }
}<commit_msg>Change the behaviour slightly - the server only sends its public key if it has to.<commit_after>package net.lshift.spki.suiteb.demo;

import static net.lshift.spki.convert.openable.OpenableUtils.read;
import static net.lshift.spki.convert.openable.OpenableUtils.write;
import static net.lshift.spki.suiteb.SequenceUtils.sequence;
import static net.lshift.spki.suiteb.Signed.signed;

import java.io.IOException;

import net.lshift.spki.InvalidInputException;
import net.lshift.spki.convert.ReadInfo;
import net.lshift.spki.convert.openable.ByteOpenable;
import net.lshift.spki.suiteb.Action;
import net.lshift.spki.suiteb.EncryptionCache;
import net.lshift.spki.suiteb.PrivateEncryptionKey;
import net.lshift.spki.suiteb.PrivateSigningKey;
import net.lshift.spki.suiteb.PublicEncryptionKey;
import net.lshift.spki.suiteb.PublicSigningKey;
import net.lshift.spki.suiteb.Sequence;

public class Server {
    private static ReadInfo R = ReadInfo.BASE.extend(Service.class);
    private EncryptionCache ephemeral;

    private final PrivateSigningKey signingKey;
    private ByteOpenable certificate;

    public Server() {
        signingKey = PrivateSigningKey.generate();
        ephemeral = new EncryptionCache(PrivateEncryptionKey.generate());
    }

    public PublicSigningKey getPublicSigningKey() {
        return signingKey.getPublicKey();
    }

    public void setCertificate(ByteOpenable certificate) {
        this.certificate = certificate;
    }

    public ByteOpenable generateMessage(Service service) throws IOException,
            InvalidInputException {
        return asOpenable(signedMessage(service));
    }

    public ByteOpenable generateEncryptedMessageFor(Service service,
            PublicEncryptionKey recipient) throws IOException,
            InvalidInputException {
        return asOpenable(sequence(ephemeral.getPublicKey(),
                ephemeral.encrypt(recipient, signedMessage(service))));
    }

    private Sequence signedMessage(Service service) throws IOException,
            InvalidInputException {
        Sequence message = sequence(signed(signingKey, new Action(service)));
        if (certificate != null) {
            message = sequence(read(R, certificate), message);
        } else {
            message = sequence(getPublicSigningKey(), message);
        }
        return message;
    }

    private ByteOpenable asOpenable(Sequence sequence) throws IOException {
        final ByteOpenable target = new ByteOpenable();
        write(target, sequence);
        return target;
    }
}
{'fraction_non_alphanumeric': 0.08459214501510574, 'fraction_numerical': 0.0, 'mean_word_length': 3.7115749525616697, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4622727', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1347, 'n_words': 279}