1 /* Copyright 2006 Nick Mathewson; see LICENSE for licensing information */
4 /* id_to_fp.c : Helper for directory authority ops. When somebody sends us
5 * a private key, this utility converts the private key into a fingerprint
6 * so you can de-list that fingerprint.
9 #include <openssl/rsa.h>
10 #include <openssl/bio.h>
11 #include <openssl/sha.h>
12 #include <openssl/pem.h>
18 #define die(s) do { fprintf(stderr, "%s\n", s); goto err; } while (0)
21 main(int argc
, char **argv
)
25 unsigned char *buf
= NULL
, *bufp
;
27 unsigned char digest
[20];
31 fprintf(stderr
, "Reading key from stdin...\n");
32 if (!(b
= BIO_new_fp(stdin
, BIO_NOCLOSE
)))
33 die("couldn't read from stdin");
34 } else if (argc
== 2) {
35 if (strcmp(argv
[1], "-h") == 0 ||
36 strcmp(argv
[1], "--help") == 0) {
37 fprintf(stdout
, "Usage: %s [keyfile]\n", argv
[0]);
41 if (!(b
= BIO_new_file(argv
[1], "r")))
42 die("couldn't open file");
45 fprintf(stderr
, "Usage: %s [keyfile]\n", argv
[0]);
48 if (!(key
= PEM_read_bio_RSAPrivateKey(b
, NULL
, NULL
, NULL
)))
49 die("couldn't parse key");
51 len
= i2d_RSAPublicKey(key
, NULL
);
54 bufp
= buf
= malloc(len
+1);
57 len
= i2d_RSAPublicKey(key
, &bufp
);
61 SHA1(buf
, len
, digest
);
62 for (i
=0; i
< 20; i
+= 2) {
63 printf("%02X%02X ", (int)digest
[i
], (int)digest
[i
+1]);