Demo entry 6789499

Authentication

   

Submitted by Jack Honour on Apr 29, 2019 at 15:59
Language: C. Code size: 851 Bytes.

#include "Auth.h"
#include <stdio.h>
#include <string.h>

int checkAuth(char* username, char* password) {
	//returns 1 for success, 0 for fail (true/false)
	FILE *ptr = fopen("auth.txt", "r");
	if (ptr == NULL) {
		return 0;
	}
	int found;
	found = 0; //because we loop round for every space/line in file we have to see if line/space before was a valid username if so check password NOW.
	char buf[100];
	while (fscanf(ptr, "%s", buf) == 1){
		if (found == 1 && strlen(buf) == strlen(password) && strcmp(buf,password) == 0) {
			//strcmp is not enough on its own, as it can have whole parts of the string missing and still be valid, so check string length as well.
			return 1;
		} else {
			if(found == 1) return 0;
		}
		if (strlen(buf) == strlen(username) && strcmp(buf,username) == 0) {
			found = 1;
		}
	}

	return 0;
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).