Skip to content

Instantly share code, notes, and snippets.

@TARA24Kevin
Last active August 29, 2015 14:19
Show Gist options
  • Save TARA24Kevin/3fdbb398726a606c3cb8 to your computer and use it in GitHub Desktop.
Save TARA24Kevin/3fdbb398726a606c3cb8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include<string.h>
#include<time.h>
#include <crypt.h>
#define _XOPEN_SOURCE
struct add{
unsigned long seed[2];
char name[BUFSIZ];
char *buf;
char *password;
char bufer[BUFSIZ];
char *loadBuf, *delimiter;
}secret;
void clean(){
int x;
for ( x = 0; x < 1; x++ ){
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
}
void clear_stdin(){
int c;
while ((c = getchar()) != 0 && c != '\n');
}
int checkInput(int min, int max){
int option;
char c;
do{
printf("\nTe rog sa alegi o optiune intre %d and %d>\t",min,max);
if (scanf("%d%c",&option,&c) == 0 || c != '\n'){
clear_stdin();
printf("\nTe rog sa alegi o optiune intre %d and %d>\t",min,max);
}else if (option < min || option > max){
printf("\nTe rog sa alegi o optiune intre %d and %d>\t",min,max);
}else{
printf("\n\n");
break;
}
}while(1);
return option;
}
int checkName(char fileName[], char name[]){
int found = 0;
FILE *file;
if((file = fopen(fileName, "r")) == NULL){
printf("\nFisierul\t[%s]\tnu a fost gasit\n",fileName);
exit(1);
}else{
while (!feof(file)) {
secret.bufer[0] = '\0';
fscanf(file, "%s", secret.bufer);
if(strlen(secret.bufer) == 0){
continue;
}
secret.loadBuf = secret.bufer;
secret.delimiter = strchr(secret.bufer, ':');
secret.delimiter[0] = '\0';
secret.delimiter++;
if(strcasecmp(secret.loadBuf, name) == 0){
found = 1;
break;
}
}
}
fclose(file);
return found;
}
void checkLogin(char fileName[]){
int found = 0;
FILE *file;
if((file = fopen(fileName, "r")) == NULL){
printf("\nFisierul\t[%s]\tnu a fost gasit\n",fileName);
exit(1);
}else{
clean();
printf("\n\n\t\t\tAutentificare\n");
printf("Utilizator: ");
scanf("%s", secret.name);
secret.password = getpass("Parola: ");
while (!feof(file)) {
secret.bufer[0] = '\0';
fscanf(file, "%s", secret.bufer);
if(strlen(secret.bufer) == 0){
continue;
}
secret.loadBuf = secret.bufer;
secret.delimiter = strchr(secret.bufer, ':');
secret.delimiter[0] = '\0';
secret.delimiter++;
if(strcasecmp(secret.loadBuf, secret.name) == 0){
if(strcmp(crypt(secret.password, secret.delimiter), secret.delimiter) == 0){
clean();
printf("\n\n\n\t\t\tBine ai revenit %s\n",secret.name);
found = 1;
}else{
printf("\n\n\n\t\t\tNume sau Parola gresita\n\n");
found = 2;
}
break;
}
}
}
if(found == 0){
printf("\n\n\n\t\t\tNu sa gasit nici o Inregistrare!\n\n");
}
fclose(file);
}
void addRec(char fileName[]){
char passwordCheck1[BUFSIZ];
char passwordCheck2[BUFSIZ];
const char *const seedchars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char salt[] = {"$1$$........"};
int wrongPasswd = 0, wrongName = 0,i,a;
FILE *file;
if((file = fopen(fileName, "r")) == NULL){
file = fopen(fileName, "w");
}
secret.seed[0] = time(NULL);
secret.seed[1] = getpid() ^ (secret.seed[0] >> 14 & 0x30000);
for (i = 0; i < 8; i++){
salt[3+i] = seedchars[(secret.seed[i/5] >> (i%5)*6) & 0x3f];
}
clean();
do{
if(wrongName == 0){
printf("\n\n\t\t\tInregistrare\n");
printf("Tasteaza numele: ");
}else{
printf("Tasteaza un alt Nume: ");
}
scanf("%s", secret.name);
if((a = checkName(fileName, secret.name)) == 1){
clean();
printf("\t\t\tNumele ales de tine Exista.\n\n");
wrongName += 1;
if (wrongName == 3){
clean();
printf("Au fost mai mult de %d incarcari esuate.\n", wrongName);
printf("\n\n\t\t\tLa revedere!");
exit(1);
}
}else{
break;
}
}while((a = checkName(fileName, secret.name)) == 1);
do{
secret.buf = getpass("Tasteaza Parola: ");
sprintf(passwordCheck1, "%s",secret.buf);
secret.buf = getpass("Tasteaza parola inca odata: ");
sprintf(passwordCheck2, "%s", secret.buf);
if(strcmp(passwordCheck1, passwordCheck2) == 0){
if (strcmp(secret.name, passwordCheck2) == 0){
clean();
printf("\n\n\n\t\t\tNumele si parola sunt identice\n");
printf("\t\t\tTe rog sa tastezi o alta parola\n\n");
}
}
if (wrongPasswd == 3){
clean();
printf("Au fost mai mult de %d incarcari esuate.\n", wrongPasswd);
printf("\n\n\t\t\tLa revedere!");
exit(1);
}
}while(strcmp(passwordCheck1, passwordCheck2) != 0 || strcmp(secret.name, passwordCheck2) == 0);
secret.buf = crypt(passwordCheck2, salt);
if((file = fopen(fileName, "a+")) == NULL){
printf("\nErroare Fisier\n");
}else{
fprintf(file, "%s:%s\n",secret.name, secret.buf);
clean();
printf("\n\n\n\t\t\tContul cu numele de %s a fost creat\n\n",secret.name);
}
fclose(file);
}
void goodBye(){
printf("\n\n\n\t\t\tLa Revedere!\n\n");
}
int main(){
int option;
char *fileName = "secret.dat";
do{
printf("\n\n\t\t\tAutentificare/Inregistrare\n\n");
printf("1) Autentificare\n");
printf("2) Inregistrare\n");
printf("3) Iesire\n");
printf("\n");
option = checkInput(1,3);
if(option == 1){
checkLogin(fileName);
}else if (option == 2){
addRec(fileName);
}else if (option == 3){
goodBye();
break;
}
}while(option != 3);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment