1 // printing functions for shallot
11 #include <openssl/pem.h>
13 // endian crap for htobe16() [only needed
14 // for base32_onion which should be moved] {
15 #include <stdint.h> // OpenBSD needs this included before sys/endian.h
17 #if defined(LINUX_PORT) || defined(OSX) || defined(GENERIC)
20 #include <sys/param.h> // OpenBSD needs this early on too
21 #include <sys/endian.h>
25 // TODO: Move to math.c?
26 void base32_onion(char *dst
, unsigned char *src
) { // base32 encode hash
27 uint8_t byte
= 0, // dst location
28 offset
= 0; // bit offset
29 for(; byte
< BASE32_ONIONLEN
; offset
+= 5) {
34 dst
[byte
++] = BASE32_ALPHABET
[(htobe16(*(uint16_t*)src
) >> (11-offset
))
40 void print_onion(char *onion
) { // pretty print hash
44 s
= malloc(PRINT_ONION_MAX
);
45 snprintf(s
, PRINT_ONION_MAX
, PRINT_ONION_STR
, loop
, onion
);
47 asprintf(&s
, PRINT_ONION_STR
, loop
, onion
);
49 for(i
=0; i
<strlen(s
); i
++)
50 printf("-"); // TODO: use fputc()?
52 for(i
=0; i
<strlen(s
); i
++)
53 printf("-"); // TODO: use fputc()?
58 void print_prkey(RSA
*rsa
) { // print PEM formated RSA key
60 BIO
*b
= BIO_new(BIO_s_mem());
61 PEM_write_bio_RSAPrivateKey(b
, rsa
, NULL
, NULL
, 0, NULL
, NULL
);
62 BIO_get_mem_ptr(b
, &buf
);
63 BIO_set_close(b
, BIO_NOCLOSE
);
65 char *dst
= malloc(buf
->length
+1);
66 strncpy(dst
, buf
->data
, buf
->length
);
67 dst
[buf
->length
] = '\0';