Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 23rd, 2005, 9:32 PM   #1
2roll4life7
Programmer
 
2roll4life7's Avatar
 
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4 2roll4life7 is on a distinguished road
Password Generators

I noticed some people had their password generators posted. So I wanted to post mine for contrast and anybody else that was interested.

In Java:
package misc;

import java.util.Random;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class PasswordGenerator {
    public static void main(String args[])throws IOException {
		final char characters[] = {
			'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'
		};
		Random prng = new Random();
		int length = 0;
		try {
			length = Integer.parseInt(args[0]);
		}
		catch(NumberFormatException nfx) {}
		catch(ArrayIndexOutOfBoundsException ax) {}
		finally {
			while(length < 8 || length > 32) {
				System.out.print("password length(8-32): ");
				try {
					length = Integer.parseInt(
						new BufferedReader(
							new InputStreamReader(System.in)
						).readLine()
					);
				}
				catch(NumberFormatException nfx) {
					return;
				}
			}
			StringBuffer password = new StringBuffer(length);
			for(int i=0; i < password.capacity(); ++i)
				password.append(characters[prng.nextInt(characters.length)]);
			System.out.println(password);
		}
        return;
    }
}

In Perl:
use strict;

my $length;
do {
    print "password length(8-32): ";
    chomp($length = <STDIN>);
} while($length =~ m/[^0-9]/ || ($length > 32 || $length < 8));
my @chars = ('a'..'z', 'A'..'Z', '0'..'9');
my $password;
while($length > 0) {
    $password .= @chars[int(rand($#chars))];
    $length--;
}
print "$password\n";

This one makes "easy to remember" passwords given an input file (preferable a huge one) to read a random line from and obfuscate it. A quick search on google (dictionary filetype:txt) can prove useful for this. Here it is in C:
/*
Copyright 2005(c) Ian Treyball

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include "linkedlist.h"

 
//the bigger the file, the more secure
void rndLn(const char *const filename, char *s);
void obfsct(char *s);
//a password this big is almost useless...
const size_t buf_capcty = 1024L;

int main(int argc, char **argv) {
   if(argc != 2 || argv[1][0] != '-') {
       printf("usage: %s -wordlist.txt\n", argv[0]);
       return 1;
   }
   char *s = (char *)calloc(buf_capcty, sizeof(char));
   rndLn(&argv[1][1], s);
   obfsct(s);
   printf("%s\n", s);
   free(s);
   return 0;
}
void rndLn(const char *const filename, char *s) {
   //shouts to The Dark and Lopez for
   //helping me fix the former bug in the following line
   FILE *file = fopen(filename, "rb");
   if(file == NULL) {
       fprintf(stderr, "could not open file: %s\n", filename);
       exit(1);
   }
   int c = 0;
   add_node(0);
   while((c = fgetc(file)) != EOF) {
       if(((char)c) == '\n')
           add_node(ftell(file));
   }
   srand((time(NULL)));
   unsigned int rnd = (unsigned)(rand() % count_nodes());
   assert(rnd < count_nodes());
   fseek(file, get_node(rnd)->data, SEEK_SET);
   int i;
   for(i = 0; (c = fgetc(file)) != EOF && c != '\n' && i < buf_capcty;s++)
       *s = c;
   *s = '\0';
   return;
}
void obfsct(char *s) {
   srand(time(NULL));
   for(;*s != '\0'; ++s) {
       if((rand() & 0x01) != 0) {
           switch(*s) {
               case 'e':
                   *s = '3';
                   break;
               case ' ':
                   *s = '_';
                   break;
               case 'a':
                   *s = '@';
                   break;
               case 's':
                   *s = '$';
                   break;
               default: break;
           }
       }
   }
   return;
}
And it's linkedlist implementation (linkedlist.h):
/*
Copyright 2005(c) Ian Treyball

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdio.h>
#include <stdlib.h>

#ifndef _linked_list
#define _linked_list

void add_node(long data);
struct list_node *lookup_node(long data);
unsigned int count_nodes(void);
void print_nodes(void);
struct list_node *get_node(unsigned int right);


struct list_node {
   struct list_node *next_node;
   fpos_t data;
};
struct list_node *first_node = NULL;
void add_node(long data) {
   struct list_node *new_node = NULL;
   new_node = malloc(sizeof(struct list_node));
   new_node->data = data;
   new_node->next_node = first_node;
   first_node = new_node;
   return;
}
struct list_node *lookup_node(long data) {
   struct list_node *current_node = first_node;
   while(current_node != NULL) {
       if(current_node->data != data) {
           current_node = current_node->next_node;
       } else {
           return current_node;
       }
   }
   return NULL;
}
unsigned int count_nodes(void) {
   struct list_node *current_node = first_node;
   unsigned int found = 0;
   while(current_node != NULL) {
       ++found;
       current_node = current_node->next_node;
   }
   return found;
}
void print_nodes(void) {
   struct list_node *current_node = first_node;
   printf("%s", "{ ");
   while(current_node != NULL) {
       printf("%d ", current_node->data);
       current_node = current_node->next_node;
   }
   printf("%s\n", "}");
   return;
}
struct list_node *get_node(unsigned int right) {
   struct list_node *current_node = first_node;
   if(right > count_nodes())
       return NULL;
   for(;right > 0; --right)
       current_node = current_node->next_node;
   return current_node;
}
#endif
__________________
#if 0 /* in case someone actually tries to compile this */
- libpng version 1.2.8 (example.c)

<Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode?
2roll4life7 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC