Power of Structures 
and Functions in Cybersecurity (Part 2)

Photo by Ilya Pavlov on Unsplash

Power of Structures and Functions in Cybersecurity (Part 2)

Introduction:

In the realm of programming, structures and functions serve as fundamental building blocks for solving complex problems. One such problem is user authentication, a critical aspect of secure systems. In this blog, I will delve into the world of structures and functions, specifically focusing on their application in user authentication using C. Get ready to unravel the secrets of creating robust and secure authentication systems!

Understanding User Authentication:

Before we dive into the technical aspects, let's explain user authentication in simple terms. Imagine you have a secret vault that only opens with a special key. User authentication is like verifying the identity of individuals before granting them access to the vault. We'll explore the importance of authentication and its significance in securing sensitive information.

Defining Structures:

In C, structures allow us to define custom data types that can hold multiple pieces of related information. I'll explain structures using real-life examples, such as creating a user structure that stores details like username, password, and permissions. With structures, we can organize and manage user data efficiently.

#include <stdio.h>

// Define the User structure
struct User {
    char username[50];
    char password[50];
    char permissions[50];
};

int main() {
    // Create an instance of the User structure
    struct User user1;

    // Set user details
    strcpy(user1.username, "Abel S");
    strcpy(user1.password, "pa$$w0rd");
    strcpy(user1.permissions, "read, write");

    // Accessing and printing user details
    printf("Username: %s\n", user1.username);
    printf("Password: %s\n", user1.password);
    printf("Permissions: %s\n", user1.permissions);

    return 0;
}

In the above code:

  • We define a User structure using the struct keyword.

  • Inside the structure, we declare character arrays (char[]) to store the username, password, and permissions.

  • In the main function, we create an instance of the User structure called user1.

  • We use the strcpy function to copy the values into the respective fields of the user1 structure.

  • Finally, we access and print the user details using printf statements.

    Utilizing Functions:

Functions act as powerful tools that perform specific tasks within a program. In the context of user authentication, we'll create functions that handle operations like verifying credentials, granting access, and managing user accounts. We'll explore the concept of function parameters, return values, and the role they play in authentication systems.

#include <stdio.h>
#include <stdbool.h>

// Define the User structure
struct User {
    char username[50];
    char password[50];
};

// Function to verify user credentials
bool verifyCredentials(struct User user, char inputUsername[50], char inputPassword[50]) {
    if (strcmp(user.username, inputUsername) == 0 && strcmp(user.password, inputPassword) == 0) {
        return true; // Credentials match
    } else {
        return false; // Credentials do not match
    }
}

// Function to grant access
void grantAccess(struct User user) {
    printf("Access granted for user: %s\n", user.username);
    // Add code here to perform actions after granting access
}

int main() {
    // Create a user structure
    struct User user1;
    strcpy(user1.username, "Abel S");
    strcpy(user1.password, "pa$$w0rd");

    // Prompt user for input
    char inputUsername[50];
    char inputPassword[50];
    printf("Enter username: ");
    scanf("%s", inputUsername);
    printf("Enter password: ");
    scanf("%s", inputPassword);

    // Verify user credentials
    bool credentialsMatch = verifyCredentials(user1, inputUsername, inputPassword);

    // Grant or deny access based on credentials
    if (credentialsMatch) {
        grantAccess(user1);
    } else {
        printf("Access denied. Invalid credentials.\n");
    }

    return 0;
}

In the above code:

  • We define a User structure to store the username and password.

  • The verifyCredentials function takes a user structure, input username, and input password as parameters. It compares the input credentials with the stored credentials and returns true if they match, and false otherwise.

  • The grantAccess function takes a user structure as a parameter and grants access by printing a message.

  • In the main function, we create a user structure and prompt the user to enter their username and password.

  • We then call the verifyCredentials function to check if the entered credentials match the stored credentials.

  • Depending on the result, we either call the grantAccess function to grant access or print an "Access denied" message.

These functions demonstrate how specific tasks like credential verification and granting access can be encapsulated into reusable functions, enhancing code organization and readability.

Conclusion:

User authentication is a vital aspect of secure systems, and structures and functions provide the necessary tools to implement robust authentication mechanisms. By delving into the world of C, we've explored how to define structures, create functions, and apply them to user authentication. Armed with this knowledge, you can now build your own secure systems, protecting valuable data and ensuring authorized access. Embrace the power of structures and functions and unlock the realm of secure user authentication!
Learn to solve real problems, Follow me on:

Linkedin, Github, Twitter, and Medium for more insights.