Questioner
Questioner

Reputation: 705

Apache Nifi Processor for Signature Verification

I have developed a JAVA application for signature verification as follows:

    package read_key_pck;

    import static java.nio.charset.StandardCharsets.UTF_8;
    import java.util.Scanner;

    import javax.xml.bind.DatatypeConverter;

    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.security.KeyFactory;
    import java.security.NoSuchAlgorithmException;
    import java.security.NoSuchProviderException;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.Security;
    import java.security.Signature;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.X509EncodedKeySpec;
    import java.util.Base64;

    import org.bouncycastle.jce.provider.BouncyCastleProvider;

    public class Main {

    public final static String RESOURCES_DIR = "C:\\Users\\KX5710\\eclipse-workspace\\read_key\\src\\read_key_pck\\";

    public static boolean verify(String plainText, String signature, PublicKey publicKey) throws Exception {
        Signature publicSignature = Signature.getInstance("SHA256withRSA");
        publicSignature.initVerify(publicKey);
        publicSignature.update(plainText.getBytes(UTF_8));

        byte[] signatureBytes = DatatypeConverter.parseHexBinary(signature); 

        return publicSignature.verify(signatureBytes);
    }

    public static void main(String[] args) throws Exception {
        Security.addProvider(new BouncyCastleProvider());

        PublicKey pub = null;
        KeyFactory factory = KeyFactory.getInstance("RSA", "BC");
        try {

            pub = generatePublicKey(factory, RESOURCES_DIR
                    + "rsa_2048_pub.pem");

        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
        String encodedPublicKey = Base64.getEncoder().encodeToString(pub.getEncoded());
        System.out.println("publickey: " + encodedPublicKey);

        boolean isCorrect = verify("         4   5.00   1.80", "54c5645616a8567d605bd990df0456913420fb630860316cf29bf57e19ef3102933ac948e672a4e3dfa8b7e20cc9eb44520aa8d8dc69143a5bc718a17a1d3f2dfc0084f172dbda6dd38e411e75100136d95d46e3d563c3a0aed062c40c6d17a5038ef0ad681622245bd2332008b1edca693b3418df1f5f86f09a6585d4af7da0d2ab44f5ce1c7ec8eed51696697b2674a881868be7163eda7c6562d02bdf069fcd66d483e8b22c5c976d27c04ffe38e62800eec17f1786b2f2161fcf44b91ba85a4486edb48a5c5a2f58dc1fa3969917eadabd7af753947de4a7f03034de09adb3b1cacb77e21444eee2d90944037606f84d0aa99f4794e8df8a2d0fbfff55c7", pub);
        System.out.println("Signature verification: " + isCorrect);
    }

    private static PublicKey generatePublicKey(KeyFactory factory,
            String filename) throws InvalidKeySpecException,
            FileNotFoundException, IOException {
        PemFile pemFile = new PemFile(filename);
        byte[] content = pemFile.getPemObject().getContent();
        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(content);
        return factory.generatePublic(pubKeySpec);
    }
}

    package read_key_pck;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;

    import org.bouncycastle.util.io.pem.PemObject;
    import org.bouncycastle.util.io.pem.PemReader;

    public class PemFile {

        private PemObject pemObject;

        public PemFile(String filename) throws FileNotFoundException, IOException {
            PemReader pemReader = new PemReader(new InputStreamReader(
                    new FileInputStream(filename)));
            try {
                this.pemObject = pemReader.readPemObject();
            } finally {
                pemReader.close();
            }
        }

        public PemObject getPemObject() {
            return pemObject;
        }
    }

Now I want to develop an Apache Nifi Processor for this JAVA application; I have a sample code here :

https://github.com/pcgrenier/nifi-examples

However I do not know that I have to modify which parts of this sample code, since it is the first time i develop an Apache Nifi Processor.

Also When I want to run .bat file in cmd i.e. run-nifi.bat I get this error :

"The JAVA_HOME environment variable is not defined correctly. Instead the PATH will be used to find the java executable."

Upvotes: 0

Views: 634

Answers (2)

Sivaprasanna Sethuraman
Sivaprasanna Sethuraman

Reputation: 4132

Great that you're writing your own custom processor on NiFi. Couple of things that you have to keep in mind:

  • The core functionality that your processor intends to do goes inside the overridden onTrigger method.
  • Handling relationships of your processor (Failure/Success)

Couple of guides that would help in your quest:

The first one provides you some overview on common terminologies & functionalities in the form of annotation and classes that are widely used in NiFi processors and also provide relevant links to the same on Apache NiFi developer guide. That tutorial itself should be able to get you started with the actual implementation since it has examples in the form of code and POM.

Upvotes: 2

Vitaliy Samofal
Vitaliy Samofal

Reputation: 75

Here is detailed guide for beginners https://community.hortonworks.com/articles/4318/build-custom-nifi-processor.html . The above article is based on this video https://www.youtube.com/watch?v=3ldmNFlelhw

Briefly explanation:

  1. You should create the new project from one of the NiFi maven archetype.
  2. Modify generated processor to your needs.
  3. Build the nar file and put it to NiFi.
  4. Use it in NiFi.

Upvotes: 2

Related Questions