// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package encrypt;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class TestSigning {
    // sign: byte[] + byte[] private key -> byte[]
    // verify: byte[] + byte[] signature + byte[] public key -> boolean
    public static byte[] sign(String alghash, String alg, byte[] content, byte[] serprivkey) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException  {
        KeyFactory kf = KeyFactory.getInstance(alg);
        PrivateKey privkey = kf.generatePrivate(new PKCS8EncodedKeySpec(serprivkey));
        Signature s = Signature.getInstance(alghash);
        s.initSign(privkey);
        s.update(content);
        return s.sign();
    }
    public static boolean verify(String alghash, String alg, byte[] content, byte[] signature, byte[] serpubkey) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {
        KeyFactory kf = KeyFactory.getInstance(alg);
        PublicKey pubkey = kf.generatePublic(new X509EncodedKeySpec(serpubkey));
        Signature s = Signature.getInstance(alghash);
        s.initVerify(pubkey);
        s.update(content);
        return s.verify(signature);
    }
    // Base64 encode: byte[] -> String
    // Base64 decode: String -> byte[]
    private static String encode(byte[] b) {
        return Base64.getEncoder().encodeToString(b);
    }
    private static byte[] decode(String s) {
        return Base64.getDecoder().decode(s);
    }
    // sign: String + byte[] private key -> Base64 String
    // verify: String + Base64 String signature + byte[] public key -> boolean
    public static String sign(String alghash, String alg, String content, byte[] serprivkey) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, SignatureException, UnsupportedEncodingException {
        return encode(sign(alghash, alg, content.getBytes("UTF-8"), serprivkey));
    }
    public static boolean verify(String alghash, String alg, String content, String signature, byte[] serpubkey) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, SignatureException, UnsupportedEncodingException {
        return verify(alghash, alg, content.getBytes("UTF-8"), decode(signature), serpubkey);
    }
    // test
    private static final String TEXT = "This is some random text to be used to test encryption.";
    private static void test(String label, String alghash, String alg, int bits) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, SignatureException, UnsupportedEncodingException {
        System.out.println(label + ":");
        // generate keys
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(alg);
        kpg.initialize(bits);
        KeyPair kp = kpg.generateKeyPair();
        PrivateKey privkey = kp.getPrivate();
        PublicKey pubkey = kp.getPublic();
        byte[] serprivkey = privkey.getEncoded();
        byte[] serpubkey = pubkey.getEncoded();
        // real test
        System.out.println("  " + TEXT);
        String sig = sign(alghash, alg, TEXT, serprivkey);
        System.out.println("  " + sig);
        boolean ok = verify(alghash, alg, TEXT, sig, serpubkey);
        System.out.println("  " + ok);
        boolean ok2 = verify(alghash, alg, TEXT + "X", sig, serpubkey);
        System.out.println("  " + ok2);
    }
    public static void main(String[] args) throws Exception {
        test("SHA-1 160 bit with RSA 3072 bit", "SHA1withRSA", "RSA", 3072);
        test("SHA-2 256 bit with RSA 3072 bit", "SHA256withRSA", "RSA", 3072);
        test("SHA-2 512 bit with RSA 3072 bit", "SHA512withRSA", "RSA", 3072);
        test("SHA-1 160 bit with ECDSA 384 bit", "SHA1withECDSA", "EC", 384);
        test("SHA-2 256 bit with ECDSA 384 bit", "SHA256withECDSA", "EC", 384);
        test("SHA-2 512 bit with ECDSA 384 bit", "SHA512withECDSA", "EC", 384);
    }
}
