Sample code for 30+ languages & platforms
Java

ECDSA Sign and Verify Data using Different Hash Algorithms

See more ECC Examples

Demonstrates how to create ECDSA signatures on data using different hash algorithms.

Note: This example requires Chilkat v9.5.0.85 or greater because the SignBd and VerifyBd methods were added in v9.5.0.85.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Ecc;
import com.chilkatsoft.chilkat2.PrivateKey;
import com.chilkatsoft.chilkat2.Prng;
import com.chilkatsoft.chilkat2.PublicKey;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // First load an ECDSA private key to be used for signing.
        PrivateKey privKey = new PrivateKey();
        try {
            privKey.loadEncryptedPemFile("qa_data/ecc/secp256r1-key-pkcs8-secret.pem", "secret");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Load some data to be signed.
        BinData bd = new BinData();
        try {
            bd.loadFile("qa_data/hamlet.xml");
        } catch (ChilkatException e) {
            System.out.println("Failed to load file to be hashed.");
            return;
        }

        Ecc ecdsa = new Ecc();
        Prng prng = new Prng();

        // Sign the sha256 hash of the data.  Return the ECDSA signature in the base64 encoding.
        System.out.println("ECDSA signing the sha256 hash of the data...");
        String sig = ecdsa.signBd(bd, "sha256", "base64", privKey, prng);
        System.out.println("sig = " + sig);

        // Verify the signature against the original data.
        // (We must use the same hash algorithm that was used when signing.)

        // Load the public key that corresponds to the private key used for signing.
        PublicKey pubKey = new PublicKey();
        try {
            pubKey.loadFromFile("qa_data/ecc/secp256r1-pub.pem");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Ecc ecc2 = new Ecc();
        int result = ecc2.verifyBd(bd, "sha256", sig, "base64", pubKey);
        if (result != 1) {
            System.out.println(ecc2.getLastErrorText());
            return;
        }

        System.out.println("Verified!");

        // ----------------------------------------------------------------------------------------
        // Let's do the same thing, but with sha384 hashing...

        System.out.println("--------------------------------------------");
        System.out.println("ECDSA signing the sha384 hash of the data...");

        sig = ecdsa.signBd(bd, "sha384", "base64", privKey, prng);
        System.out.println("sig = " + sig);

        result = ecc2.verifyBd(bd, "sha384", sig, "base64", pubKey);
        if (result != 1) {
            System.out.println(ecc2.getLastErrorText());
            return;
        }

        System.out.println("Verified!");
    }
}