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