ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
password.c File Reference

🔑 Password prompting utilities with secure input and formatting More...

Go to the source code of this file.

Functions

int prompt_password (const char *prompt, char *password, size_t max_len)
 
int prompt_password_simple (const char *prompt, char *password, size_t max_len)
 

Detailed Description

🔑 Password prompting utilities with secure input and formatting

Definition in file password.c.

Function Documentation

◆ prompt_password()

int prompt_password ( const char *  prompt,
char *  password,
size_t  max_len 
)

Definition at line 15 of file password.c.

15 {
16 if (!prompt || !password || max_len < 2) {
17 return -1;
18 }
19
20 // Check for non-interactive mode first
21 if (!platform_is_interactive()) {
22 return -1;
23 }
24
25 // Lock terminal for the entire operation
26 bool previous_terminal_state = log_lock_terminal();
27
28 // Calculate display width of prompt for alignment
29 int prompt_width = utf8_display_width(prompt);
30 if (prompt_width < 0) {
31 prompt_width = 0;
32 }
33
34 // Create separator that matches prompt width (minimum 40 chars)
35 int separator_width = (prompt_width > 40) ? prompt_width : 40;
36 char separator[BUFFER_SIZE_SMALL];
37 if (separator_width > (int)sizeof(separator) - 1) {
38 separator_width = sizeof(separator) - 1;
39 }
40 for (int i = 0; i < separator_width; i++) {
41 separator[i] = '=';
42 }
43 separator[separator_width] = '\0';
44
45 // Display formatted header
46 log_plain("\n%s", separator);
47 log_plain("%s", prompt);
48 log_plain("%s", separator);
49
50 // Unlock before prompting (prompt_question will lock again)
51 log_unlock_terminal(previous_terminal_state);
52
53 // Prompt for password with asterisk masking
54 prompt_opts_t opts = PROMPT_OPTS_PASSWORD;
55 int result = platform_prompt_question("", password, max_len, opts);
56
57 // Validate password is valid UTF-8 (should always succeed since platform_prompt_question handles it)
58 if (result == 0 && !utf8_is_valid(password)) {
59 log_warn("Password contains invalid UTF-8 sequence, input may be corrupted");
60 }
61
62 // Display footer
63 previous_terminal_state = log_lock_terminal();
64 log_plain("%s\n", separator);
65 log_unlock_terminal(previous_terminal_state);
66
67 return result;
68}
bool log_lock_terminal(void)
void log_unlock_terminal(bool previous_state)
bool utf8_is_valid(const char *str)
Definition utf8.c:158
int utf8_display_width(const char *str)
Definition utf8.c:46

References log_lock_terminal(), log_unlock_terminal(), utf8_display_width(), and utf8_is_valid().

◆ prompt_password_simple()

int prompt_password_simple ( const char *  prompt,
char *  password,
size_t  max_len 
)

Definition at line 70 of file password.c.

70 {
71 if (!prompt || !password || max_len < 2) {
72 return -1;
73 }
74
75 // Check for non-interactive mode first
76 if (!platform_is_interactive()) {
77 return -1;
78 }
79
80 // Build prompt with colon suffix, using byte-length for memcpy
81 char full_prompt[BUFFER_SIZE_SMALL];
82 size_t prompt_byte_len = strlen(prompt);
83 if (prompt_byte_len >= sizeof(full_prompt) - 2) {
84 prompt_byte_len = sizeof(full_prompt) - 3;
85 }
86 memcpy(full_prompt, prompt, prompt_byte_len);
87 full_prompt[prompt_byte_len] = ':';
88 full_prompt[prompt_byte_len + 1] = '\0';
89
90 // Prompt for password with asterisk masking, same line
91 prompt_opts_t opts = PROMPT_OPTS_PASSWORD;
92 int result = platform_prompt_question(full_prompt, password, max_len, opts);
93
94 // Validate password is valid UTF-8 (should always succeed since platform_prompt_question handles it)
95 if (result == 0 && !utf8_is_valid(password)) {
96 log_warn("Password contains invalid UTF-8 sequence, input may be corrupted");
97 }
98
99 return result;
100}

References utf8_is_valid().

Referenced by parse_ssh_private_key(), and read_password_from_stdin().