Sample code for 30+ languages & platforms
Java

Apple Keychain - Create RSA Signature

See more Apple Keychain Examples

Using an RSA private key stored in the Apple Keychain, create a signature by signing the SHA-256 hash of the provided string, then return the signature encoded in Base64 format.

Note: This example requires Chilkat v10.0.0 or greater.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
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.

        Cert cert = new Cert();

        // On MacOS and iOS, the LoadByCommonName function will search the Apple Keychain for the matching certificate.

        // A digital certificate's Common Name (CN) is a field in the certificate that specifies the name of the entity the
        // certificate is issued to. It is often used to identify the domain name, hostname, or organization the certificate represents.

        // For example:
        // - In an SSL/TLS certificate for a website, the CN typically contains the domain name (e.g., "www.example.com").
        // - For certificates representing individuals or organizations, the CN might include the person's name or the organization's name.

        // The CN is part of the certificate's Subject field and is crucial for verifying that the certificate corresponds
        // to the intended entity, especially in SSL/TLS communications.

        try {
            cert.loadByCommonName("Example, Inc.");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Rsa rsa = new Rsa();
        String sigBase64;
        try {
            rsa.setX509Cert(cert, true);

            // Generate an RSA signature by signing the SHA-256 hash of the provided string,
            // then return the signature encoded in Base64 format.
            rsa.setEncodingMode("base64");
            sigBase64 = rsa.signStringENC("to be signed", "sha256");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("RSA Signature:");
        System.out.println(sigBase64);
    }
}