root/openpgpsdk/trunk/examples/encrypt.c

Revision 444 (checked in by rachel, 6 years ago)

Do proper error checking, not just an assert

Line 
1 #include <openpgpsdk/create.h>
2 #include <openpgpsdk/crypto.h>
3 #include <openpgpsdk/keyring.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <assert.h>
7
8 int main(int argc,char **argv)
9     {
10     const char *keyfile;
11     const char *user_id;
12     ops_keyring_t keyring;
13     const ops_key_data_t *key;
14     ops_create_info_t *info;
15
16     if(argc != 3)
17         {
18         fprintf(stderr, "%s <keyfile> <user_id>\n", argv[0]);
19         exit(1);
20         }
21
22     keyfile = argv[1];
23     user_id = argv[2];
24
25     ops_init();
26     ops_keyring_read(&keyring, keyfile);
27
28     key = ops_keyring_find_key_by_userid(&keyring, user_id);
29     if (!key)
30         {
31         printf("No key found for user %s in keyring %s\n", user_id, keyfile);
32         exit(1);
33         }
34
35     info = ops_create_info_new();
36     ops_writer_set_fd(info, 1);  // stdout for now
37     ops_writer_push_encrypt(info, key);
38
39     for( ; ; )
40         {
41         unsigned char buf[8192];
42         int n;
43        
44         n=read(0,buf,sizeof buf);
45         if(!n)
46             break;
47         if(n < 0)
48             {
49             perror("stdin");
50             exit(4);
51             }
52         ops_write(buf,n,info);
53         }
54
55     return 0;
56     }
Note: See TracBrowser for help on using the browser.