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