ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
lib/crypto/key_identity/display.c
Go to the documentation of this file.
1#include <ascii-chat/common.h>
8#include <options/options.h>
9#include <string.h>
10
12 bool unicode, int cols, char *out,
13 size_t size) {
14 if (!out || !size)
15 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing identity output buffer");
16 out[0] = '\0';
17 if (!key)
18 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing public identity");
19 const char *type;
20 switch (key->type) {
22 type = "Ed25519";
23 break;
24 case KEY_TYPE_GPG:
25 type = "Ed25519 (GPG)";
26 break;
27 case KEY_TYPE_X25519:
28 type = "X25519";
29 break;
30 default:
31 return SET_ERRNO(ERROR_INVALID_PARAM, "Unsupported public identity type");
32 }
34 asciichat_error_t result = key_fingerprint_digest(key->key, digest);
35 if (result != ASCIICHAT_OK)
36 return result;
37 char hex[HEX_STRING_SIZE_32];
38 for (size_t i = 0; i < sizeof(digest); ++i) {
39 hex[i * 2] = "0123456789abcdef"[digest[i] >> 4];
40 hex[i * 2 + 1] = "0123456789abcdef"[digest[i] & 15];
41 }
42 hex[64] = '\0';
43 char mask[KEYMASK_BUFFER_SIZE] = "";
44 bool show = art && cols >= KEYMASK_WIDTH;
45 if (show && (result = keymask_render(digest, unicode, mask, sizeof(mask))) !=
47 return result;
48 const char *heading = show ? "Keymask v2 / SHA-256\n" : "";
49 size_t needed = strlen(type) + strlen(" public key\n") + strlen(heading) +
50 strlen(mask) + strlen("SHA256:") + 64 + 2;
51 if (size < needed)
52 return SET_ERRNO(ERROR_INVALID_PARAM, "Identity output needs %zu bytes",
53 needed);
54 safe_snprintf(out, size, "%s public key\n%s%sSHA256:%s\n", type, heading,
55 mask, hex);
56 return ASCIICHAT_OK;
57}
58
60 char *out, size_t size) {
61 bool tty = platform_isatty(STDERR_FILENO);
62 bool art = tty && !GET_OPTION(snapshot_mode);
63 int cols = tty ? ui_controller_size().cols : 80;
64 // Notices reserve borders, padding, and the terminal's final column.
65 cols = (cols > 81 ? 80 : cols - 1) - 4;
66 bool unicode = terminal_should_use_utf8(GET_OPTION(force_utf8));
67 return key_identity_format(key, art, unicode, cols, out, size);
68}
69
70const char *key_identity_art_end(const char *text) {
71 static const char heading[] = "Keymask v2 / SHA-256\n";
72 static const char border[] = "+--------------------------------+\n";
73 if (!text || strncmp(text, heading, sizeof(heading) - 1) != 0)
74 return NULL;
75 const char *row = text + sizeof(heading) - 1;
76 if (strncmp(row, border, sizeof(border) - 1) != 0)
77 return NULL;
78 row += sizeof(border) - 1;
79 for (unsigned i = 0; i < KEYMASK_ROWS - 2; ++i) {
80 const char *end = strchr(row, '\n');
81 if (!end || end - row < 2 || row[0] != '|' || end[-1] != '|' ||
82 utf8_display_width_n(row, (size_t)(end - row)) != KEYMASK_WIDTH)
83 return NULL;
84 row = end + 1;
85 }
86 return strncmp(row, border, sizeof(border) - 1) == 0
87 ? row + sizeof(border) - 1
88 : NULL;
89}
90
91void key_identity_announce(const char *label, const public_key_t *key) {
92 char identity[KEY_IDENTITY_BUFFER_SIZE];
93 if (key_identity_format_terminal(key, identity, sizeof(identity)) ==
95 NOTICE_ANNOUNCE(label, "%s", identity);
96}
#define SET_ERRNO(code,...)
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
uint8_t key[CRYPTO_PUBLIC_KEY_SIZE]
Definition key_types.h:85
key_type_t type
Definition key_types.h:84
@ KEY_TYPE_ED25519
Definition key_types.h:61
@ KEY_TYPE_GPG
Definition key_types.h:64
@ KEY_TYPE_X25519
Definition key_types.h:63
#define GET_OPTION(field)
Definition options.h:1339
int utf8_display_width_n(const char *str, size_t max_bytes)
Calculate terminal display width of a UTF-8 string up to byte limit.
Definition utf8.c:106
⚙️ Common definitions, error codes, macros, and types shared throughout the application
asciichat_error_t key_fingerprint_digest(const uint8_t public_key[ED25519_PUBLIC_KEY_SIZE], uint8_t digest[KEYMASK_DIGEST_SIZE])
Definition keymask.c:12
asciichat_error_t keymask_render(const uint8_t digest[KEYMASK_DIGEST_SIZE], bool unicode, char *out, size_t size)
Definition keymask.c:50
#define KEYMASK_WIDTH
Definition keymask.h:14
#define KEYMASK_BUFFER_SIZE
Definition keymask.h:17
#define KEYMASK_ROWS
Definition keymask.h:15
#define KEYMASK_DIGEST_SIZE
Definition keymask.h:13
asciichat_error_t key_identity_format_terminal(const public_key_t *key, char *out, size_t size)
asciichat_error_t key_identity_format(const public_key_t *key, bool art, bool unicode, int cols, char *out, size_t size)
void key_identity_announce(const char *label, const public_key_t *key)
const char * key_identity_art_end(const char *text)
#define NOTICE_ANNOUNCE(title,...)
Definition notice.h:95
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
static bool size_t gotel_query_t * out
Definition queries.h:10
#define HEX_STRING_SIZE_32
Hex string size for 32-byte values (64 hex chars + null terminator).
Definition sizes.h:163
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
🔤 String Manipulation and Shell Escaping Utilities
The normalized public identity used by ascii-chat's crypto and key-management code.
Definition key_types.h:83
terminal_size_t ui_controller_size(void)
🔤 UTF-8 Encoding and Decoding Utilities
int platform_isatty(int fd)
Definition util.c:70