-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpassword.c
211 lines (179 loc) · 6.77 KB
/
password.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <curses.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#define OFFSET_TOP 5
#define OFFSET_LEFT 4
void print_row(int top, int index, int left, int cell, int between_cell, int right) {
int spot = 0;
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, index);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, ' ');
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, left);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, between_cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, between_cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, between_cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, between_cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, between_cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, between_cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, cell);
mvaddch(OFFSET_TOP + top, OFFSET_LEFT + spot++, right);
}
void print_table() {
int row = 0;
// Print the headers
mvprintw(OFFSET_TOP - 1, OFFSET_LEFT, " A B C D E F G");
// Print the table
print_row(row++, ' ', ACS_ULCORNER, ACS_HLINE, ACS_TTEE, ACS_URCORNER); // Top
print_row(row++, '1', ACS_VLINE, ' ', ACS_VLINE, ACS_VLINE); // Cell row
print_row(row++, ' ', ACS_LTEE, ACS_HLINE, ACS_PLUS, ACS_RTEE); // Divider
print_row(row++, '2', ACS_VLINE, ' ', ACS_VLINE, ACS_VLINE); // Cell row
print_row(row++, ' ', ACS_LTEE, ACS_HLINE, ACS_PLUS, ACS_RTEE); // Divider
print_row(row++, '3', ACS_VLINE, ' ', ACS_VLINE, ACS_VLINE); // Cell row
print_row(row++, ' ', ACS_LTEE, ACS_HLINE, ACS_PLUS, ACS_RTEE); // Divider
print_row(row++, '4', ACS_VLINE, ' ', ACS_VLINE, ACS_VLINE); // Cell row
print_row(row++, ' ', ACS_LTEE, ACS_HLINE, ACS_PLUS, ACS_RTEE); // Divider
print_row(row++, '5', ACS_VLINE, ' ', ACS_VLINE, ACS_VLINE); // Cell row
print_row(row++, ' ', ACS_LTEE, ACS_HLINE, ACS_PLUS, ACS_RTEE); // Divider
print_row(row++, '6', ACS_VLINE, ' ', ACS_VLINE, ACS_VLINE); // Cell row
print_row(row++, ' ', ACS_LTEE, ACS_HLINE, ACS_PLUS, ACS_RTEE); // Divider
print_row(row++, '7', ACS_VLINE, ' ', ACS_VLINE, ACS_VLINE); // Cell row
print_row(row++, ' ', ACS_LLCORNER, ACS_HLINE, ACS_BTEE, ACS_LRCORNER); // Bottom
}
void print_to_grid(int x, int y, int c) {
mvaddch(OFFSET_TOP + (y*2) + 1, OFFSET_LEFT + (x*4) + 4, c);
}
// We can store 24 bits of data
void print_password(uint8_t a, uint8_t b, uint8_t c) {
// Print the table
mvprintw(17, OFFSET_LEFT, " Press any key to continue");
print_table();
// Pack into a uint32
uint32_t passcode = (a << 16 | b << 8 | c);
// Xor it to make it more random looking
passcode = passcode ^ 0x555555;
// Set each bit appropriately
int i;
// Setting the checksum to 1 means that all-blank won't be valid
int checksum = 1;
int bit = 23;
for(i = 0; i < 7; i++) {
int j;
for(j = 0; j < (i > 2 ? (6 - i) : (7 - i)); j++) {
uint8_t thischar = (passcode >> bit) & 0x01 ? 1 : 0;
bit--;
// Count the number of bits
checksum ^= thischar;
print_to_grid(j, i, thischar ? ACS_DIAMOND : ' ');
print_to_grid(6-j, 6-i, thischar ? ACS_DIAMOND : ' ');
}
}
print_to_grid(3, 3, checksum ? ACS_DIAMOND : ' ');
refresh();
timeout(-1);
getch();
}
uint8_t validate_password(uint8_t state[7][7], uint8_t *a, uint8_t *b, uint8_t *c) {
uint32_t result = 0;
int i;
int checksum = 1;
for(i = 0; i < 7; i++) {
int j;
for(j = 0; j < (i > 2 ? (6 - i) : (7 - i)); j++) {
// Check for rotational symmetry
if(state[i][j] != state[6 - i][6 - j]) {
return 0;
}
// Update the checksum
checksum ^= state[i][j];
// Read the character
result = (result << 1) | state[i][j];
}
}
// Validate the checksum
if(checksum != state[3][3]) {
return 0;
}
result ^= 0x555555;
*a = (result >> 16) & 0x0FF;
*b = (result >> 8) & 0x0FF;
*c = (result >> 0) & 0x0FF;
return 1;
}
uint8_t read_password(uint8_t *a, uint8_t *b, uint8_t *c) {
int x = 0;
int y = 0;
uint8_t state[7][7];
memset(state, 0, sizeof(state));
init_pair(1, COLOR_YELLOW, COLOR_BLACK);
init_pair(2, COLOR_RED, COLOR_BLACK);
timeout(-1);
for(;;) {
clear();
print_table();
// Print the state
int i;
for(i = 0; i < 7; i++) {
int j;
for(j = 0; j < 7; j++) {
if(state[i][j]) {
print_to_grid(j, i, ACS_DIAMOND);
}
}
}
// Print the instructions
mvprintw(OFFSET_TOP + 15, OFFSET_LEFT, " Toggle with <space>");
mvprintw(OFFSET_TOP + 16, OFFSET_LEFT, " Submit with <enter>");
// Print the cursor
attron(COLOR_PAIR(1));
if(state[y][x]) {
print_to_grid(x, y, ACS_DIAMOND);
} else {
print_to_grid(x, y, ACS_BULLET);
}
attroff(COLOR_PAIR(1));
// Go to 0,0 to keep the cursor out of the way
move(0, 0);
switch(getch()) {
case 'w': case KEY_UP: y = (y == 0) ? 6 : (y - 1); break;
case 'a': case KEY_LEFT: x = (x == 0) ? 6 : (x - 1); break;
case 's': case KEY_DOWN: y = (y + 1) % 7; break;
case 'd': case KEY_RIGHT: x = (x + 1) % 7; break;
case ' ': state[y][x] ^= 1; break;
case '\n':
if(validate_password(state, a, b, c)) {
return 1;
}
clear();
attron(COLOR_PAIR(2));
mvprintw(0 + OFFSET_TOP, 0 + OFFSET_LEFT, "Something's wrong, please check");
mvprintw(1 + OFFSET_TOP, 0 + OFFSET_LEFT, "your password and try again!");
attroff(COLOR_PAIR(2));
getch();
break;
default:
return 0;
}
}
}