-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathutils.c
72 lines (69 loc) · 2.44 KB
/
utils.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "utils.h"
/**
* generateRandomNonce - Generates a random nonce.
* @nonce: The array to store the generated nonce.
*/
void generateRandomNonce(unsigned char nonce[16])
{
srand((unsigned int)time(NULL));
for (int i = 0; i < 16; ++i)
{
nonce[i] = (unsigned char)rand();
}
}
/**
* getHexRepresentation - Converts a buffer to its hexadecimal representation.
* @buffer: Pointer to the input buffer.
* @size: Size of the input buffer.
* @hexString: Pointer to the output string for the hexadecimal representation.
*
* This function converts the content of the input buffer to its hexadecimal
* representation and stores it in the output string. The resulting string is
* null-terminated.
*/
void getHexRepresentation(const unsigned char *buffer, size_t size, char *hexString)
{
for (size_t i = 0; i < size; ++i)
{
sprintf(hexString + i * 2, "%02x", buffer[i]);
}
}
/**
* isStringNotEmpty - Checks if a C string is not empty.
* @str: Pointer to the C string to be checked.
*
* This function determines whether the given C string is not empty.
* It returns 1 if the string is not empty and not NULL, and 0 otherwise.
*
* Returns: 1 if the string is not empty, 0 otherwise.
*/
int isStringNotEmpty(const char *str)
{
return (str != NULL && strlen(str) > 0);
}
/**
* isauth - Check authentication based on user and hash.
* @opt_authuser: User-provided authentication username.
* @username: Server's expected username for comparison.
* @receivedhash: User-provided received hash for comparison.
* @serverdigest: Server's expected hash for comparison.
*
* It compares opt_authuser with username and receivedhash with serverdigest.
* Returns: 1 if both username and hash match, 0 otherwise.
*/
int isauth(const char *opt_authuser, const unsigned char *username,
const char *receivedhash, const char *serverdigest)
{
// Check if opt_authuser is not empty and equal to username
int userMatch = (opt_authuser != NULL && *opt_authuser != '\0' &&
strncmp(opt_authuser, (const char *)username, USERNAME_SIZE) == 0);
// Check if receivedhash is not empty and equal to serverdigest
int hashMatch = (receivedhash != NULL && *receivedhash != '\0' &&
strcmp(receivedhash, serverdigest) == 0);
// Return 1 if both conditions are true, 0 otherwise
return (userMatch && hashMatch);
}