ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
keymask.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define KEYMASK_INTERIOR_ROWS (KEYMASK_ROWS - 2)
4#define KEYMASK_INTERIOR_COLUMNS (KEYMASK_WIDTH - 2)
5
8#include <sodium.h>
9#include <string.h>
10
14 if (!public_key || !digest)
16 "Missing public key or fingerprint output");
17 if (crypto_hash_sha256(digest, public_key, ED25519_PUBLIC_KEY_SIZE) != 0)
18 return SET_ERRNO(ERROR_CRYPTO, "Cannot compute public-key fingerprint");
19 return ASCIICHAT_OK;
20}
21
22// Independent feature hashes keep each choice stable as other features evolve.
23static void feature_seed(const uint8_t digest[KEYMASK_DIGEST_SIZE],
24 const char *feature,
26 static const char domain[] = "ascii-chat/keymask/v2";
27 crypto_hash_sha256_state state;
28 crypto_hash_sha256_init(&state);
29 crypto_hash_sha256_update(&state, (const unsigned char *)domain,
30 sizeof(domain));
31 crypto_hash_sha256_update(&state, digest, KEYMASK_DIGEST_SIZE);
32 crypto_hash_sha256_update(&state, (const unsigned char *)feature,
33 strlen(feature));
34 crypto_hash_sha256_final(&state, seed);
35}
36
38 unsigned row, unsigned col, const char *text) {
39 for (; *text && col < KEYMASK_INTERIOR_COLUMNS; ++col, ++text)
40 cells[row][col] = *text;
41}
42
43static void
45 unsigned row, const char *text) {
46 stamp(cells, row, (unsigned)(KEYMASK_INTERIOR_COLUMNS - strlen(text)) / 2,
47 text);
48}
49
51 bool unicode, char *out, size_t size) {
52 if (!out || !size)
53 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing Keymask output buffer");
54 out[0] = '\0';
55 if (!digest)
56 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing Keymask digest");
57
59 memset(cells, ' ', sizeof(cells));
63 feature_seed(digest, "outline", outline);
64 feature_seed(digest, "crown", crown);
65 feature_seed(digest, "eyes", eyes);
66 feature_seed(digest, "center", center);
67 feature_seed(digest, "mouth", mouth);
68 feature_seed(digest, "markings", marks);
69
70 // Half-widths from forehead to chin. Each family has a distinct silhouette.
71 static const unsigned widths[8][12] = {
72 {8, 10, 12, 12, 12, 11, 10, 9, 8, 6, 4, 2},
73 {6, 7, 8, 8, 8, 8, 8, 8, 7, 6, 5, 4},
74 {11, 13, 14, 14, 13, 12, 11, 10, 9, 7, 5, 3},
75 {9, 10, 11, 12, 13, 14, 13, 11, 9, 6, 3, 1},
76 {12, 12, 12, 12, 12, 12, 12, 12, 12, 10, 8, 6},
77 {5, 7, 9, 11, 12, 12, 11, 10, 8, 6, 4, 2},
78 {10, 11, 12, 11, 10, 9, 10, 11, 10, 8, 6, 4},
79 {7, 9, 11, 13, 14, 13, 12, 11, 10, 9, 8, 7},
80 };
81 const unsigned *profile = widths[outline[0] % 8];
82 for (unsigned r = 0; r < 12; ++r) {
83 unsigned left = 16 - profile[r], right = 15 + profile[r];
84 cells[r + 3][left] = '#';
85 cells[r + 3][right] = '#';
86 if ((outline[1] & 1) && r > 0 && r < 10) {
87 cells[r + 3][left + 1] = '#';
88 cells[r + 3][right - 1] = '#';
89 }
90 if (r == 0 || r == 11)
91 for (unsigned c = left; c <= right; ++c)
92 cells[r + 3][c] = '#';
93 }
94 static const char *crowns[8][3] = {
95 {"/\\ /\\", "/ \\ / \\", "\\ \\________/ /"},
96 {" /\\ ", " /##\\ ", "____/####\\____"},
97 {"o o", "| |", "/\\____________/\\"},
98 {" [] [] [] ", " || || || ", "__||__||__||__"},
99 {" ________ ", " /========\\ ", " /__________\\ "},
100 {"\\\\ //", " \\\\ // ", " \\\\______// "},
101 {" /||\\ ", " /||||\\ ", "___/||||||\\___"},
102 {" /\\ /\\ ", "< > < >", " \\/________\\/ "},
103 };
104 for (unsigned r = 0; r < 3; ++r)
105 centered(cells, r, crowns[crown[0] % 8][r]);
106
107 static const char *eye_rows[8][2] = {
108 {"[###] [###]", " V V "},
109 {" /--\\ /--\\ ", " \\__/ \\__/ "},
110 {" \\##\\ /##/ ", " \\_/ \\_/ "},
111 {"============", "[--O----O--]"},
112 {" .--. .--. ", " (XX) (XX) "},
113 {" /^^\\ /^^\\ ", " |..| |..| "},
114 {" <> <> ", " /__\\ /__\\ "},
115 {" [::] [::] ", " -- -- "},
116 };
117 centered(cells, 5, eye_rows[eyes[0] % 8][0]);
118 centered(cells, 6, eye_rows[eyes[0] % 8][1]);
119 static const char *noses[6][2] = {{"/\\", "\\/"}, {"||", "||"},
120 {"##", "VV"}, {"/\\", "/__\\"},
121 {"::", "[]"}, {"\\/", "||"}};
122 centered(cells, 8, noses[center[0] % 6][0]);
123 centered(cells, 9, noses[center[0] % 6][1]);
124 static const char *mouths[8][2] = {
125 {"[||||||]", " ------ "}, {"\\ VV VV /", " \\____/ "},
126 {" ====== ", " "}, {" /----\\ ", " \\____/ "},
127 {" [++++] ", " ---- "}, {" \\____/ ", " V V "},
128 {" /\\/\\ ", " \\/\\/ "}, {" [####] ", " |||| "},
129 };
130 centered(cells, 11, mouths[mouth[0] % 8][0]);
131 centered(cells, 12, mouths[mouth[0] % 8][1]);
132 // Cheek ornaments stay outside the facial features and inside the outline.
133 static const char *ornaments[] = {"/", "=", ":", "+", "<", "*", "o", "-"};
134 for (unsigned r = 7; r <= 10; ++r) {
135 unsigned col = 18 - profile[r - 3];
136 char mark = ornaments[marks[r] % 8][0];
137 cells[r][col] = mark;
138 cells[r][31 - col] = mark == '<' ? '>' : mark == '/' ? '\\' : mark;
139 }
140 // One deliberate asymmetric cheek stripe, on a hash-selected side.
141 unsigned accent_row = 7 + marks[0] % 3;
142 unsigned accent_col = 19 - profile[accent_row - 3];
143 if (marks[1] & 1)
144 accent_col = 31 - accent_col;
145 cells[accent_row][accent_col] = '!';
146 cells[accent_row + 1][accent_col] = '!';
147
148 size_t required = KEYMASK_ROWS * (KEYMASK_WIDTH + 1) + 1;
149 for (unsigned r = 0; r < KEYMASK_INTERIOR_ROWS; ++r)
150 for (unsigned c = 0; c < KEYMASK_INTERIOR_COLUMNS; ++c)
151 if (unicode && cells[r][c] == '#')
152 required += 2;
153 if (size < required)
154 return SET_ERRNO(ERROR_INVALID_PARAM, "Keymask output needs %zu bytes",
155 required);
156 char *p = out;
157 const char border[] = "+--------------------------------+\n";
158 memcpy(p, border, sizeof(border) - 1);
159 p += sizeof(border) - 1;
160 for (unsigned r = 0; r < KEYMASK_INTERIOR_ROWS; ++r) {
161 *p++ = '|';
162 for (unsigned c = 0; c < KEYMASK_INTERIOR_COLUMNS; ++c) {
163 if (unicode && cells[r][c] == '#') {
164 memcpy(p, "\xe2\x96\x91", 3);
165 p += 3;
166 } else {
167 *p++ = cells[r][c];
168 }
169 }
170 *p++ = '|';
171 *p++ = '\n';
172 }
173 memcpy(p, border, sizeof(border));
174 return ASCIICHAT_OK;
175}
#define SET_ERRNO(code,...)
char cells[(512 *256 *2)]
Definition fft.c:22
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_CRYPTO
@ ERROR_INVALID_PARAM
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
#define KEYMASK_INTERIOR_ROWS
Definition keymask.c:3
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_INTERIOR_COLUMNS
Definition keymask.c:4
#define KEYMASK_WIDTH
Definition keymask.h:14
#define KEYMASK_ROWS
Definition keymask.h:15
#define KEYMASK_DIGEST_SIZE
Definition keymask.h:13
static bool size_t gotel_query_t * out
Definition queries.h:10
#define ED25519_PUBLIC_KEY_SIZE
Ed25519 public key size in bytes.
Definition sizes.h:38
🔤 String Manipulation and Shell Escaping Utilities