TestKeyGeneration.java

package com.hsbc.baas;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;

public class TestKeyGeneration {

    public static void main(String[] args) {

        // identity is the name to be associated with the public key
        String identity = "test";

        // passphrase is the password to be used for the private key
        String passphrase = "1password";

        // keySize is the size of the key to be generated
        Integer keySize = 2048;

        ByteArrayOutputStream privateOut = new ByteArrayOutputStream();
        ByteArrayOutputStream publicOut = new ByteArrayOutputStream();
        PgpKeyPairGenerator.generateKeyPair(identity, passphrase, keySize, publicOut, privateOut);

        HashMap<String, String> pgpKeyPairDto = new HashMap<>();
        pgpKeyPairDto.put("publicKey", Base64.getEncoder().encodeToString(publicOut.toByteArray()));
        pgpKeyPairDto.put("privateKey", Base64.getEncoder().encodeToString(privateOut.toByteArray()));

        // Print keys to be saved to file.
        System.out.println(pgpKeyPairDto);
        pgpKeyPairDto.keySet().forEach(key -> System.out.println(key + ": " + new String(Base64.getDecoder().decode(pgpKeyPairDto.get(key)))));

    }
}