cybersecurity · beginner · ~12 min · safe pentest lab
Per-character pass over a string, tally character classes, threshold against a policy struct.
Implement:
typedef struct {
int min_length;
int require_upper;
int require_lower;
int require_digit;
int require_special;
} pw_policy_t;
int pw_check(const char *pw, const pw_policy_t *p);
Return 0 when the password meets the policy. Return a bitmask of failed
requirements otherwise:
| Bit | Meaning |
|---|---|
1 << 0 |
length too short |
1 << 1 |
missing required uppercase |
1 << 2 |
missing required lowercase |
1 << 3 |
missing required digit |
1 << 4 |
missing required special (non-alphanumeric) |
pw (use strlen).pw == NULL → return all 5 bits set (0x1f).has_upper, has_lower, has_digit, has_special.isupper(c), islower(c), isdigit(c) from <ctype.h>; "special" = !isalnum(c).char is signed on some platforms — cast to unsigned char before passing to isupper etc.The defensive cure to brute-force is policy enforcement, not faster crackers.
A C string + a pointer to a pw_policy_t.
0 on pass, or a bitmask of failed requirements.
No hashing. No online tests. Pure validation.
#include <stddef.h>
typedef struct {
int min_length;
int require_upper;
int require_lower;
int require_digit;
int require_special;
} pw_policy_t;
int pw_check(const char *pw, const pw_policy_t *p) {
/* TODO */
(void)pw; (void)p;
return 0;
}
Forgetting the unsigned char cast. Returning 1 for every failure (no caller can tell which). Setting bits for unrequired classes.
NULL pw → 0x1f. Empty string → length bit set. Length exactly equal to min_length → pass.
O(n) where n is the password length.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.