@ TheMassiveChipmunk

So I noticed this: http://www.cplusplus.com/articles/1hvU7k9E/
Since you thanked me for an idea I never intentionally gave you, here I am making up for it.

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
/******************************************************************************/
/*                      Catfishes Password Creator                            */
/*                                                                            */
/* inspired by TheMassiveChipmunk's... password generating... thing...        */
/* dedicated to TMC, with the following message:                              */
/* "whatever you ever code, keep it simple"                                   */
/*                                                                            */
/*               C90-style code, just because it looks cool                   */
/******************************************************************************/

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_PASSWORD_LENGTH     128UL

int main(int argc, char **argv)
{
    char ingredients[] =
        "abcdefghijklmnopqrstuvwxyz"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "0123456789";

    size_t ingredients_length, i;
    unsigned long int password_length;
    char *password;

    if (argc != 2)
    {
        fprintf(stderr, "\nUsage:\n\tprogram.exe password_length\n");
        return EXIT_FAILURE;
    }

    srand(time(NULL));
    password_length = strtoul(argv[1], NULL, 10);

    if (password_length == 0 || password_length > MAX_PASSWORD_LENGTH)
    {
        fprintf(stderr, "\nCan't have a password of length %lu!\n", password_length);
        return EXIT_FAILURE;
    }

    password = malloc((password_length + 1) * sizeof (char));

    if (password == NULL)
    {
        fprintf(stderr, "\nCould not allocate memory for password creation.\n");
        return EXIT_FAILURE;
    }

    /* repeatedly calling strlen() in a loop wastes time */
    ingredients_length = strlen(ingredients);

    for (i=0; i < password_length; ++i)
        password[i] = ingredients[rand() % ingredients_length];

    password[password_length] = '\0'; /* ensure NIL-termination */
    printf("%s\n", password);
    free(password);
    return EXIT_SUCCESS;
}


Notice how we don't mess around with writing the password to a file.
That's because if the user wants to, he can easily redirect the Standard Output to a file, using > and >>.

cpc.exe 13 > passwords.txt
cpc.exe 20 >> passwords.txt
cpc.exe 3 >> passwords.txt
cpc.exe 100 >> passwords.txt
type passwords.txt
V0JIGIoWDxNyH
vU5T9S560MynrnnYCAA7
LGI
5Pr352MgU1jbHr5Z0uSIRxqDXErDIZMN8RzIcPl1C2dBEgyezWQwGV577YD4q8YkSZguCqmrzoHBPpBb4REdemdDEcVEwLADHgBi


Here's a couple of ideas, for any C programmer*:

1) C is a simple language. So C programs should be simple too.
2) Fail early, and fail ruthlessly.
3) If you don't want fools to use your program, don't try to make it foolproof.

Edit:
* pros excluded.
Last edited on
You should stick this in the source code article section :D
Since Twicker was nice enough to add that in for us
Thanks for the tip sorry for the misunderstanding, next I'll ask you before I use your code. I'll bookmark this as a reminder. Hope I didn't hurt your feelings :).
Last edited on
Whoa, chill. I just thought you were posting some code you wanted to share. Figured that's what the source code section is for, ya know? Guess someone's easily offended though
I made something like this once. I still use it. The only differences are that the length is 16 by default and I send the output to the clipboard.
I though he was mad sorry and I am not offended I rarely am.
No worries :) A lot of language is lost over the Internet.
@ TCM: Any code I ever post on this forum is free for all to use, and I couldn't care less about being given credits. This is quite a misunderstanding, maybe I should add silly smileys to prevent such things from happening in the future? ;)

My point was: keep simple programs simple. Now it's your turn to say "no, because... [arguments]" or do the right thing and rewrite your C++ version to a shorter form. Mwhuahoohahaha!

@ helios: Just curious, did you use the Windows API for that?
Of course.
I dislike giving advice to anybody, without having taken it myself at one point.
So I wrote the C++ version of my Password Generator.
I hope you all hate reading it, as much as I hated writing it. ;)
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
//
// Catfishes Password Creator, C++11 version (as supported by VS2010)
//

#include <algorithm>
#include <array>
#include <iostream>
#include <random>
#include <regex>
#include <sstream>
#include <string>

const size_t maxPasswordSize = 128;

int main(int argc, char **argv)
{
    if (argc != 2)
    {
        std::cerr << "\nUsage:\n\tprogram.exe passwordLength" << std::endl;
        return 1;
    }

    const std::string   userParameter(argv[1]);
    const std::regex    naturalNumber("\\+?[[:digit:]]+");

    if (!std::regex_match(userParameter, naturalNumber))
    {
        std::cerr << "\nExpected a positive integer, got this: `" << userParameter << "'." << std::endl;
        return 1;
    }

    size_t passwordSize;
    std::istringstream getPasswordSize(userParameter);

    getPasswordSize >> passwordSize;

    if (passwordSize == 0 || passwordSize > maxPasswordSize)
    {
        std::cerr << "\nCan't have a password of size " << passwordSize << "!" << std::endl;
        return 1;
    }

    const std::array<char, 62> ingredients = {
        'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
        '0','1','2','3','4','5','6','7','8','9'
    };

    std::string         password(passwordSize, ' ');
    std::random_device  rd;
    std::mt19937_64     generator(rd());
    std::uniform_int_distribution<size_t> indexFarm(0, ingredients.size() - 1);

    std::generate(password.begin(), password.end(), [&](){
        return ingredients.at(indexFarm(generator));
    });

    std::cout << password << std::endl;
    return 0;
}


Edit: removed a const.
Last edited on
How fun was writing ingredients?
1
2
3
4
char ingredients[62];
for(char i = 'a'; i <= 'z'; ++i) ingredients[i-'a'] = i;
for(char i = 'A'; i <= 'Z'; ++i) ingredients[i-'A'+'z'] = i;
for(char i = '0'; i <= '9'; ++i) ingredients[i-'0'+'z'+'Z'] = i;
Moderately fun. And full of errors.
Last edited on
How fun was writing ingredients?
... coming from a biscuit...

@ LB: that code's so beautiful, and yet so scary.
Last edited on
I'll just leave this here, for interested time travelers from the future.

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
/*
    Catfishes Password Creator, D version
*/

import std.ascii;
import std.conv;
import std.random;
import std.regex;
import std.stdio;

immutable size_t maxPasswordLength = 128;

int main(string[] args)
{
    if (args.length != 2)
    {
        stderr.writeln("\nUsage:\n\tprogram.exe passwordLength");
        return 1;
    }

    auto naturalNumber  = regex("^[0-9]+$");
    auto inputFrisk     = match(args[1], naturalNumber);

    if (inputFrisk.empty)
    {
        stderr.writeln("\nExpected a positive integer, got `", args[1], "'.");
        return 1;
    }

    auto passwordLength = to!size_t(inputFrisk.hit);

    if (passwordLength == 0 || passwordLength > maxPasswordLength)
    {
        stderr.writeln("\nCannot have a password of length ", passwordLength, "!");
        return 1;
    }

    // ~ is the concatenation operator, letters and digits are from std.ascii
    immutable string ingredients = letters ~ digits;
    string password;

    while (passwordLength-- != 0)
        password ~= ingredients[uniform(0, ingredients.length)];

    writeln(password);
    return 0;
}
Topic archived. No new replies allowed.