Simple Password Manager in C – Dead Handy, Innit

simple password manager in c – dead handy, innit

intro

Oi, you lot! New to C and fancy summat a bit useful? We’re gonna sling together a lil program to make a simple password manager—ya know, a spot to chuck yer login names and passwords so ya don’t forget ‘em. It’s dead basic but proper cracking—like when I was tryna remember my pub Wi-Fi password after a sesh (scribbled it on a napkin, lost it). Great for getting yer head round structs and arrays in C without a right faff. Let’s have a butcher’s!

code bit

Here’s the code—nowt posh, just a quick job I chucked together. Shoved it in a box so it don’t look like a total shambles on yer blog. take a gander:

#include
#include

#define MAX 5 // max entries, innit
#define NAME_LEN 20 // max name length
#define PASS_LEN 20 // max pass length

struct Password { // lil password box
char username[NAME_LEN];
char password[PASS_LEN];
};

void addPassword(struct Password manager[], int* count) {
if(*count==MAX) {
printf("oi, manager’s full!\n");
return;
}
printf("gimme username: ");
scanf("%s",manager[*count].username); // grab name
printf("gimme password: ");
scanf("%s",manager[*count].password); // grab pass
(*count)++; // tick it up
printf("chucked in!\n");
}

void showPasswords(struct Password manager[], int count) { // show em
if(count==0) {
printf("oi, manager’s empty!\n");
return;
}
printf("here’s yer stash:\n");
for(int i=0;i printf("username: %s, password: %s\n",
manager[i].username,manager[i].password);
}
}

int main() { // nearly did void, ha!
struct Password manager[MAX]; // lil stash
int count=0; // how many we got

addPassword(manager,&count); // chuck a couple in
addPassword(manager,&count);
showPasswords(manager,count); // show em off

return 0; // forgot ; once, ugh!
}

what ya get

Here’s what it chucks out when ya run it. I tested it with a couple entries—worked bang on, mate! (Yer input’ll vary, this is just a sample.)

gimme username: jimbo
gimme password: beer123
chucked in!
gimme username: sally
gimme password: pub456
chucked in!
here’s yer stash:
username: jimbo, password: beer123
username: sally, password: pub456

how it works, kinda

Right, let’s have a natter bout this like we’re slumped at the boozer with a pint—or a bag of pork scratchings, coz I’m Hank Marvin again. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a doddle once ya clock it, swear down!

1. them `#include` bits
This is me nicking my toolkit. `` for `printf` and `scanf`, `` for string stuff (not used much here, but handy). Forgot the first one once, and my code just sat there like a plonker. Total mug move!

2. `#define` malarky
`MAX 5` sets the stash size—5 entries tops. `NAME_LEN 20` and `PASS_LEN 20` cap usernames and passwords at 20 chars—like keeping yer pub nicknames short and sweet!

3. `struct Password` bit
`struct Password` is my lil box—`char username[NAME_LEN]` for the name, `char password[PASS_LEN]` for the pass. Like a lil note for each login!

4. `int main()` funtion
Gotta have a “main” to kick off—it’s like the door to my local. `int` means I’m lobbing a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a right numpty!

5. boxes: `struct Password manager[MAX];`
`manager[MAX]` is the stash—room for 5 `Password` structs. `count=0` tracks how many we’ve chucked in. Starts empty, innit!

6. `addPassword` funtion
`addPassword(struct Password manager[], int* count)` chucks a new entry in. `if(*count==MAX)` checks if it’s full—yells “manager’s full!” if so. Else, `scanf("%s",manager[*count].username)` grabs the name, same for `password`, `*count++` ticks it up, and `printf` brags about it. Like scribbling a new login on yer napkin!

7. `showPasswords` funtion
`showPasswords(struct Password manager[], int count)` spills the stash. `if(count==0)` says “empty!” if nowt’s there. Else, a `for` loop prints each `username` and `password` with a `\n`—like flashing yer secret list!

8. chucking it out: `addPassword` and `showPasswords`
`main` calls `addPassword(manager,&count)` twice to chuck in a couple, then `showPasswords(manager,count)` shows em. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!

9. `return 0` to scarper
That `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.

And that’s yer lot! A cracking lil program to manage passwords. Faff with it—chuck in daft logins, tweak it to say “secret stash” if ya fancy. First time I ran this, I put “pint” and “lager”—kept it proper pubby, eh! You’re a star already, mate—keep smashing it! 😊