Modules | |
| Sign File or Buffer | |
| Verify File, Buffer, Key or Keyring | |
| Encrypt or Decrypt File | |
| Keys and Keyrings | |
| Supported Algorithms | |
| Memory | |
| General | |
| Callbacks | |
If you want more fine-grained control, consider using the Core API.
A good place to look for example code which uses the High Level API is the command line application, found in openpgpsdk/src/app/openpgp.c.
Example code:
void main() { ops_uid_id_t uid; char* pubring_name="pubring.gpg"; char* secring_name="secring.gpg"; char* passphrase="mysecret"; int pplen=strlen(passphrase); ops_keyring_t* pubring=NULL; ops_keyring_t* secring=NULL; ops_keydata_t * keydata=NULL; ops_create_info_t* cinfo=NULL; ops_secret_key_t* skey=NULL; ops_validate_result* validate_result=NULL; int fd=0; ops_init(); // Create a new self-signed RSA key pair uid.user_id=(unsigned char *) "Test User (RSA 2048-bit key) <testuser@test.com>"; keydata=ops_rsa_create_selfsigned_keypair(2048,65537,&uid); if (!keydata) exit (-1); // Append to keyrings fd=ops_setup_file_append(&cinfo, pubring_name); ops_write_transferable_public_key(keydata, ARMOUR_NO, cinfo); ops_teardown_file_write(cinfo,fd) fd=ops_setup_file_append(&cinfo, secring_name); ops_write_transferable_secret_key(keydata, passphrase, pplen, ARMOUR_NO, cinfo); ops_teardown_file_write(cinfo,fd) // Load public and secret keyrings if (!ops_keyring_read_from_file(pubring, ARMOUR_NO, pubring_name)) exit(-1); if (!ops_keyring_read_from_file(secring, ARMOUR_NO, secring_name)) exit(-1); // Sign a file with the new secret key skey=ops_decrypt_secret_key_from_data(keydata,passphrase); if (!ops_sign_file("mytestfile", NULL, skey, ARMOUR_YES, OVERWRITE_YES)) exit(-1); // Verify signed file with new public key validate_result=ops_mallocz(sizeof *validate_result); if (ops_validate_file(validate_result, "mytestfile.asc", ARMOUR_YES, pubring)==ops_true) printf("OK\n") else printf("Not verified OK: %d invalid signatures, %d unknown signatures\n", validate_result->invalid_count, validate_result->unknown_signer_count); ops_validate_result_free(validate_result); // Encrypt a file with the new public key if (ops_encrypt_file("mytestfile2", NULL, keydata, ARMOUR_YES, OVERWRITE_YES)!=ops_true) exit(-1); // Decrypt encrypted file with the new secret key if (ops_decrypt_file("mytestfile2.asc",NULL, secring, ARMOUR_YES, OVERWRITE_YES, callback_cmd_get_passphrase_from_cmdline)!=ops_true) exit(-1) ops_finish(); }
1.4.6