package leigh.inkan;

import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Base64;

public class Inkan {
    public static void main(String[] args)
            throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("Ed25519");
        KeyPair kp = kpg.generateKeyPair();

        byte[] msg = "Hello World!".getBytes(StandardCharsets.UTF_8);

        Signature sig = Signature.getInstance("Ed25519");
        sig.initSign(kp.getPrivate());
        sig.update(msg);
        byte[] s = sig.sign();

        byte[] privateKey = kp.getPrivate().getEncoded();
        byte[] publicKey = kp.getPublic().getEncoded();

        String encPrivate = Base64.getEncoder().encodeToString(privateKey);
        String encPublic = Base64.getEncoder().encodeToString(publicKey);

        System.out.println(encPrivate);
        System.out.println(encPublic);
        System.out.println(bytesToHex(publicKey));
    }

    private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }
}
