Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <string.h>
- #include <limits.h>
- #include <unistd.h>
- #include <bsd/stdlib.h>
- #include <openssl/evp.h>
- #include <stdbool.h>
- #include <time.h>
- #include <ctype.h>
- #define ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+~`|}{[]\\:;?><,./-="
- #define ALPHABET_LEN (sizeof(ALPHABET) - 1)
- #define SALT_LEN 16
- #define ITERATIONS 100000
- #define KEY_LEN 32
- int get_password_length() {
- int len;
- printf("Enter length of password (min 8, max 64): ");
- if (scanf("%d", &len) != 1) {
- printf("Invalid input.\n");
- exit(EXIT_FAILURE);
- }
- if (len < 8 || len > 64) {
- printf("Password length must be between 8 and 64 characters.\n");
- exit(EXIT_FAILURE);
- }
- return len;
- }
- void generate_salt(unsigned char *salt, size_t len) {
- arc4random_buf(salt, len);
- }
- void derive_key(unsigned char *key, size_t key_len, const unsigned char *password,
- size_t password_len, const unsigned char *salt, size_t salt_len) {
- PKCS5_PBKDF2_HMAC((const char *)password, password_len, salt, salt_len, ITERATIONS,
- EVP_sha256(), key_len, key);
- }
- void generate_password(char *password, size_t len) {
- unsigned char salt[SALT_LEN];
- generate_salt(salt, sizeof(salt));
- unsigned char key[KEY_LEN];
- char password_str[PATH_MAX];
- bool valid_password = false;
- while (!valid_password) {
- printf("Enter master password: ");
- if (fgets(password_str, PATH_MAX, stdin) == NULL) {
- printf("Error reading password.\n");
- exit(EXIT_FAILURE);
- }
- size_t password_len = strlen(password_str) - 1;
- password_str[password_len] = '\0'; /* remove newline character */
- if (password_len < 8) {
- printf("Password must be at least 8 characters long.\n");
- } else {
- valid_password = true;
- }
- }
- derive_key(key, sizeof(key), (const unsigned char *)password_str, strlen(password_str),
- salt, sizeof(salt));
- size_t i = 0;
- size_t j = 0;
- size_t k = 0;
- unsigned char rand_byte;
- srand(time(NULL));
- while (i < len) {
- if (j == KEY_LEN) {
- /* regenerate key if exhausted */
- derive_key(key, sizeof(key), (const unsigned char *)password_str, strlen(password_str),
- salt, sizeof(salt));
- j = 0;
- }
- rand_byte = key[j];
- j++;
- if (rand_byte < UCHAR_MAX - (UCHAR_MAX % ALPHABET_LEN)) {
- password[i] = ALPHABET[rand_byte % ALPHABET_LEN];
- i++;
- } else {
- k = rand() % len;
- password[k] = ALPHABET[rand_byte % ALPHABET_LEN];
- }
- }
- }
- int count_lowercase(char *str) {
- int count = 0;
- for (int i = 0; str[i]; i++) {
- if (str[i] >= 'a' && str[i] <= 'z') {
- count++;
- }
- }
- return count;
- }
- int count_uppercase(char *str) {
- int count = 0;
- for (int i = 0; str[i]; i++) {
- if (str[i] >= 'A' && str[i] <= 'Z') {
- count++;
- }
- }
- return count;
- }
- int count_digits(char *str) {
- int count = 0;
- for (int i = 0; str[i]; i++) {
- if (str[i] >= '0' && str[i] <= '9') {
- count++;
- }
- }
- return count;
- }
- int count_symbols(char *str) {
- int count = 0;
- for (int i = 0; str[i]; i++) {
- if (!isalnum(str[i])) {
- count++;
- }
- }
- return count;
- }
- void print_strength_meter(int strength) {
- printf("Password strength: ");
- if (strength < 25) {
- printf("Very weak\n");
- } else if (strength < 50) {
- printf("Weak\n");
- } else if (strength < 75) {
- printf("Moderate\n");
- } else if (strength < 100) {
- printf("Strong\n");
- } else {
- printf("Very strong\n");
- }
- }
- int main() {
- int len = get_password_length();
- char *password = malloc((len + 1) * sizeof(char));
- if (password == NULL) {
- perror("malloc");
- exit(EXIT_FAILURE);
- }
- generate_password(password, len);
- int lowercase_count = count_lowercase(password);
- int uppercase_count = count_uppercase(password);
- int digit_count = count_digits(password);
- int symbol_count = count_symbols(password);
- int strength = ((lowercase_count > 0) + (uppercase_count > 0) + (digit_count > 0) + (symbol_count > 0)) * 25;
- printf("Generated password: %s\n", password);
- print_strength_meter(strength);
- free(password);
- return 0;
- }
- // compile
- // gcc -o passgen passgen.c -lcrypto -lbsd
Advertisement
Comments
-
Comment was deleted
-
- updated version that checks password strength
-
- Here's a brief summary of the functions used in the program:
- get_password_length() prompts the user to enter a password length and validates the input.
- generate_salt() generates a random salt of a specified length.
- derive_key() uses the PBKDF2 function from OpenSSL to derive a key from a master password and a salt.
- generate_password() generates a password of a specified length using a pseudo-random sequence of characters from a defined alphabet and a key derived from a master password and a salt.
- count_lowercase(), count_uppercase(), count_digits(), and count_symbols() count the number of lowercase letters, uppercase letters, digits, and symbols in a given string.
- print_strength_meter() prints a description of the password strength based on the number of lowercase letters, uppercase letters, digits, and symbols in a given password.
- The program requires the OpenSSL and BSD libraries to compile and run.
Add Comment
Please, Sign In to add comment
Advertisement