root/openpgpsdk/trunk/examples/sign-inline.c

Revision 371 (checked in by ben, 7 years ago)

Make sure dmalloc happens last.

Line 
1 /* Sign an ASCII plaintext message with a key, producing a
2    dash-escaped message with inline signature. */
3
4 #include "common.h"
5 #include <openpgpsdk/crypto.h>
6 #include <openpgpsdk/signature.h>
7 #include <openpgpsdk/armour.h>
8 #include <stdlib.h>
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12
13 #include <openpgpsdk/final.h>
14
15 int main(int argc,char **argv)
16     {
17     const char *keyfile;
18     const char *plainfile;
19     const char *user_id;
20     const char *hashstr;
21     const char *sigfile;
22     ops_secret_key_t *skey;
23     ops_create_signature_t *sig;
24     ops_hash_algorithm_t alg;
25     int fd;
26     ops_create_info_t *info;
27     unsigned char keyid[OPS_KEY_ID_SIZE];
28
29     if(argc != 6)
30         {
31         fprintf(stderr,"%s <secret key file> <user_id> <hash> <plaintext file>"
32                 " <signature file>\n",argv[0]);
33         exit(1);
34         }
35
36     keyfile=argv[1];
37     user_id=argv[2];
38     hashstr=argv[3];
39     plainfile=argv[4];
40     sigfile=argv[5];
41
42     ops_init();
43
44     skey=get_secret_key(keyfile);
45     assert(skey);
46
47     alg=ops_hash_algorithm_from_text(hashstr);
48     if(alg == OPS_HASH_UNKNOWN)
49         {
50         fprintf(stderr,"Unkonwn hash algorithm: %s\n",hashstr);
51         exit(2);
52         }
53
54     sig=ops_create_signature_new();
55     ops_signature_start_plaintext_signature(sig,skey,alg,OPS_SIG_BINARY);
56
57     fd=open(sigfile,O_CREAT|O_TRUNC|O_WRONLY,0666);
58     if(fd < 0)
59         {
60         perror(sigfile);
61         exit(5);
62         }
63
64     info=ops_create_info_new();
65     ops_writer_set_fd(info,fd);
66     ops_writer_push_dash_escaped(info,sig);
67
68     fd=open(plainfile,O_RDONLY);
69     if(fd < 0)
70         {
71         perror(plainfile);
72         exit(3);
73         }
74
75     for( ; ; )
76         {
77         unsigned char buf[8192];
78         int n;
79        
80         n=read(fd,buf,sizeof buf);
81         if(!n)
82             break;
83         if(n < 0)
84             {
85             perror(plainfile);
86             exit(4);
87             }
88         ops_write(buf,n,info);
89         }
90
91     close(fd);
92
93     ops_writer_switch_to_signature(info);
94
95     ops_signature_add_creation_time(sig,time(NULL));
96
97     ops_keyid(keyid,&skey->public_key);
98     ops_signature_add_issuer_key_id(sig,keyid);
99
100     ops_signature_hashed_subpackets_end(sig);
101
102     ops_write_signature(sig,&skey->public_key,skey,info);
103
104     ops_writer_close(info);
105
106     ops_secret_key_free(skey);
107
108     return 0;
109     }
Note: See TracBrowser for help on using the browser.