Fix more typos.
[shallot/rransom.git] / src / print.c
blob35f35a4ae083f4a670c2c16491b929f6a2f26a9a
1 // printing functions for shallot
3 #include "config.h"
5 #include "print.h"
6 #include "defines.h"
7 #include "globals.h"
8 #include "error.h"
10 #include <stdio.h>
11 #include <string.h>
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)
19 #include "linux.h"
20 #else
21 #include <sys/param.h> // OpenBSD needs this early on too
22 #include <sys/endian.h>
23 #endif
24 // }
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) {
31 if(offset > 7) {
32 offset -= 8;
33 src++;
35 dst[byte++] = BASE32_ALPHABET[(htobe16(*(uint16_t*)src) >> (11-offset))
36 & (uint16_t)0x001F];
38 dst[byte] = '\0';
41 void print_onion(char *onion) { // pretty-print hash
42 uint8_t i;
43 char *s;
44 #ifdef GENERIC
45 s = malloc(PRINT_ONION_MAX);
46 snprintf(s, PRINT_ONION_MAX, PRINT_ONION_STR, loop, onion);
47 #else
48 if (asprintf(&s, PRINT_ONION_STR, loop, onion) == -1)
49 error(X_OUT_OF_MEMORY);
50 #endif
51 for(i=0; i<strlen(s); i++)
52 printf("-"); // TODO: use fputc()?
53 printf("\n%s\n", s);
54 for(i=0; i<strlen(s); i++)
55 printf("-"); // TODO: use fputc()?
56 printf("\n");
57 free(s);
60 void print_prkey(RSA *rsa) { // print PEM formatted RSA key
61 BUF_MEM *buf;
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);
66 BIO_free(b);
67 char *dst = malloc(buf->length+1);
68 strncpy(dst, buf->data, buf->length);
69 dst[buf->length] = '\0';
70 printf("%s", dst);
71 BUF_MEM_free(buf);