Files
pcp/tests/sample.c

77 lines
2.2 KiB
C
Raw Normal View History

2014-02-19 20:38:56 +01:00
#include <pcp.h>
int main() {
Buffer *inbuf;
pcp_key_t *alice, *bob;
pcp_pubkey_t *alicepub, *bobpub, *pubhash;
Pcpstream *clear_in, *crypt_out, *clear_out;
PCPCTX *ptx;
2014-02-19 20:38:56 +01:00
char message[] = "hello world";
printf("hh: %ld\n", sizeof(UT_hash_handle));
/* we always need a context */
ptx = ptx_new();
2014-02-19 20:38:56 +01:00
/* generate the keypairs for both */
alice = pcpkey_new();
bob = pcpkey_new();
pcpkey_setowner(alice, "alicia", "alicia@local");
2014-02-19 20:38:56 +01:00
/* get the public parts of them */
alicepub = pcpkey_pub_from_secret(alice);
bobpub = pcpkey_pub_from_secret(bob);
/* put the clear text message into the stream */
inbuf = buffer_new(32, "a");
buffer_add_str(inbuf, message);
clear_in = ps_new_inbuffer(inbuf);
/* create the output stream as buffer */
crypt_out = ps_new_outbuffer();
/* prepare the pubkey recipient list (only 1 recipient: Bob) */
pubhash = NULL;
2014-02-20 15:36:49 +01:00
strncpy(bobpub->id, pcp_getpubkeyid(bobpub), 17);
2014-02-19 20:38:56 +01:00
HASH_ADD_STR( pubhash, id, bobpub);
/* actually encrypt the message, don't sign it
Alice is the sender, Bob is the recipient */
pcp_encrypt_stream(ptx, clear_in, crypt_out, alice, pubhash, 0, 0);
2014-02-19 20:38:56 +01:00
/* now, print the encrypted result */
fprintf(stderr, "Alice encrypted %"FMT_SIZE_T" bytes for Bob:\n", (SIZE_T_CAST)strlen(message));
2014-02-19 20:38:56 +01:00
buffer_dump(ps_buffer(crypt_out));
/* ---- encryption don, now decrypt ---- */
/* prepare the output buffer stream */
clear_out = ps_new_outbuffer();
/* in order for the decryptor find the senders public key,
we need to put it into the context hash. this step can be
omitted when using a Vault. */
pcphash_add(ptx, alicepub, alicepub->type);
2014-02-19 20:38:56 +01:00
/* try to decrypt the message */
if(pcp_decrypt_stream(ptx, crypt_out, clear_out, bob, NULL, 0, 0) == 0)
fatals_ifany(ptx);
2014-02-19 20:38:56 +01:00
else {
/* and finally print out the decrypted message */
fprintf(stderr, "Bob decrypted %"FMT_SIZE_T" bytes from Alice:\n", (SIZE_T_CAST)buffer_size(ps_buffer(crypt_out)));
2014-02-19 20:38:56 +01:00
printf("Decrypted message: %s\n", buffer_get_str(ps_buffer(clear_out)));
}
ps_close(clear_in);
ps_close(crypt_out);
ps_close(clear_out);
ptx_clean(ptx);
2014-02-19 20:38:56 +01:00
free(alice);
free(alicepub);
free(bob);
free(bobpub);
return 0;
}