FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

string generator (a.k.a try to crack me)

 
Post new topic   Reply to topic     Forum Index -> Potential Projects
View previous topic :: View next topic  
Author Message
4lphax



Joined: 19 Apr 2011
Posts: 1
Location: 209

PostPosted: Tue Apr 19, 2011 1:57 am    Post subject: string generator (a.k.a try to crack me) Reply with quote

Hi all!

This is my first D-"program". Didn't know where else to post my creation Smile so I opted for this forum.

It's really a small script-like program which creates pretty strong password strings. The length of the string is defined from cli args. I use dmd v2.052.

Feel free to comment (constructively, plx) Wink

To run the thing:
Code:
./str_generator.d -l<length-of-output-string>


Code:

#!/usr/bin/rdmd

/+
    Class that generates strings depending upon length arg

    -checks that all characters are unique (string cannot be longer than ~75)
    -checks that no two characters of same type are next to each other
   
    LICENSE
   
    Copyright [2011] Björn Roberg <bjorn.roberg@gmail.com>

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
+/

import std.stdio;
import std.random;
import std.array;
import std.conv;
import std.datetime;
import std.string;
import std.getopt;

class Generator
{
    // Array of characters  to be used in generation
    private static const string ALLCHARS="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#%&=?+._-*";
   
    // Arrays that are used to check for similarity
    private static char[] ALPHA = std.string.letters.dup;
    private static char[] NUMERIC = std.string.digits.dup;

    // Ivars
    private uint _length;
    private Random rnd;
    private string lastResult;
    private char lastAdded;

    // ctor. Takes length of output string as parameter
    this(uint length)
    {
        _length = length;
        rnd = Random(cast(uint)Clock.currStdTime());
    }

    // The method that does the heavy lifting.
    public void generate()
    {
        auto result = appender!string();

        //WHILE#################################################################
        int i;
        int iterations; // for statistics
        while (i < _length)
        {
            auto a = uniform(0, ALLCHARS.length, rnd);

            if (inPattern(ALLCHARS[a], result.data) != true && likeLastAdded(ALLCHARS[a]) != true)
            {
                result.put(ALLCHARS[a]);
                lastAdded = ALLCHARS[a];
                i++; // if we found a char that can be added, increment counter
            }
            iterations++;
        }
        //WHILE#################################################################

        // Output
        this.lastResult = result.data;
        writefln( "This is the randomly generated string:\n [[[   %s   ]]]", result.data );
        writefln("Amount of iterations: %d", iterations);
       
        float it = cast(float) iterations;
        float ln = cast(float)_length;
        writefln("Iterations/char: %f", it/ln);
    }

    // Check to see if there are too many similarities with the potential newChar
    // and the last added char
    private bool likeLastAdded(char newChar)
    {
        bool found = false;

        // Not two numerals next to each other
        if (inPattern(newChar, NUMERIC) && inPattern(lastAdded, NUMERIC))
        {
            found = true;
        }
        // Not two letters next to each other
        if (inPattern(newChar, ALPHA) && inPattern(lastAdded, ALPHA))
        {
            found = true;
        }
        // Not two other chars next to each other
        if (!inPattern(newChar, ALPHA) && !inPattern(lastAdded, ALPHA))
        {
            found = true;
        }
        return found;
    }
}

// To run the thing
void main (string[] args)
{
    uint length;
    bool help;
   
    // parse the cli args
    getopt  (args, "length|l", &length
            , "help|h", &help);
   
    // if you need help, then you shall have it! :)
    if (help == true || length == 0)
    {
        writeln("execute with an -l flag and specify length of string, like this:");
        writeln("./generator.d -l10");
        writeln("for a string with 10 (ten) chars");
    }
    else // Otherwise, go for it
    {
      Generator g = new Generator(length);
      g.generate();   
    }
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Potential Projects All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group