Java Requires Chilkat v11.0.0+
Chilkat2 Java
Duplicate openssl dgst -sha256 -sign private.pem -out sha256.sig in.dat
See more OpenSSL Examples
Demonstrates how to duplicate this OpenSSL command:openssl dgst -sha256 -sign private.pem -out sha256.sig in.datThe in.dat file can contain text or binary data of any type. The OpenSSL command does the following:
- Creates a SHA256 digest of the contents of the input file
- Signs the SHA256 digest using the private key.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.PrivateKey;
import com.chilkatsoft.chilkat2.Rsa;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
PrivateKey pkey = new PrivateKey();
// Load the private key from an PEM file:
try {
pkey.loadPemFile("private.pem");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Rsa rsa = new Rsa();
// Import the private key into the RSA component:
try {
rsa.usePrivateKey(pkey);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// OpenSSL uses big-endian.
rsa.setLittleEndian(false);
// Load the file to be signed.
BinData bdFileData = new BinData();
bdFileData.loadFile("in.dat");
BinData bdSig = new BinData();
try {
rsa.signBd(bdFileData, "sha256", bdSig);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Save the binary signature to a file.
try {
bdSig.writeFile("signature.sig");
} catch (ChilkatException e) {
System.out.println("Failed to write signature.sig.");
return;
}
System.out.println("Success.");
}
}