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