-
-
Save d630/43bf0cdedb3de0c99e8cd3469fb201f0 to your computer and use it in GitHub Desktop.
Pw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define LEN(arr) ((int) (sizeof(arr) / sizeof(arr)[0])) | |
#define MAX 80 | |
#define MAX_ENTRIES 2 | |
struct passwd { | |
char user[MAX]; | |
char password[MAX]; | |
}; | |
typedef struct passwd Passwd; | |
static Passwd entries[MAX_ENTRIES]; | |
static int slot; | |
void has_slot() | |
{ | |
if (slot == -1 ) { | |
puts("No empty slot."); | |
exit(1); | |
} | |
} | |
void exists(char *user) | |
{ | |
slot = -1; | |
for (int i = LEN(entries) - 1; i >= 0; i--) { | |
if (strcmp(entries[i].user, user) == 0) { | |
puts("A password for user has already been created."); | |
exit(1); | |
} | |
if (strlen(entries[i].user) == 0) | |
slot = i; | |
} | |
} | |
char * decrypt(char *password) | |
{ | |
int l = strlen(password); | |
while (*password != '\0') { | |
*password -= 1; | |
password++; | |
} | |
return password - l; | |
} | |
char * encrypt(char *password) | |
{ | |
int l = strlen(password); | |
while (*password != '\0') { | |
*password += 1; | |
password++; | |
} | |
return password - l; | |
} | |
char * fill(char *password, int len) | |
{ | |
int l = strlen(password); | |
if (len >= l) { | |
for (password += l; l < len; l++, password++) | |
*password = '?'; | |
} else { | |
l = 0; | |
} | |
*password = '\0'; | |
return password - l; | |
} | |
void create(char *user, char *password) | |
{ | |
exists(user); | |
has_slot(); | |
strcpy(entries[slot].user, user); | |
strcpy(entries[slot].password, encrypt(password)); | |
} | |
void get(char *user, char *password) | |
{ | |
char password2[MAX]; | |
printf("user: "); | |
scanf("%s", user); | |
printf("password: "); | |
scanf("%s", password); | |
printf("retype password: "); | |
scanf("%s", password2); | |
if (strcmp(password, password2) != 0) { | |
puts("Passwords are not equal."); | |
exit(1); | |
} | |
} | |
void find_password(char *password) | |
{ | |
int i; | |
for (i = 0; i < LEN(entries); i++) { | |
if (strcmp(entries[i].password, password) == 0) { | |
printf("user: %s password: %s\n", | |
entries[i].user, | |
entries[i].password); | |
break; | |
} | |
} | |
} | |
int main() | |
{ | |
// char user[MAX]; | |
// char password[MAX]; | |
// get(user, password); | |
// printf("scanned: %s\n", password); | |
// create(user, password); | |
// printf("encrypted: %s\n", entries[0].password); | |
// printf("decrypted: %s\n", decrypt(entries[0].password)); | |
// strcpy(user, "u"); | |
// strcpy(password, "abc"); | |
// create(user, password); | |
// printf("find %s …\n", password); | |
// find_password(password); | |
// strcpy(user, "u"); | |
// strcpy(password, "abc"); | |
// create(user, password); | |
// strcpy(user, "user"); | |
// strcpy(password, "123456"); | |
// printf("<%s>\n", fill(password,10)); | |
// printf("<%s>\n", fill(password,-1)); | |
// printf("<%s>\n", fill(password,6)); | |
return 0; | |
} | |
// vim: set ft=c : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment