r/dailyprogrammer • u/nottoobadguy • Feb 12 '12
[2/12/2012] Challenge #4 [easy]
You're challenge for today is to create a random password generator!
For extra credit, allow the user to specify the amount of passwords to generate.
For even more extra credit, allow the user to specify the length of the strings he wants to generate!
26
Upvotes
5
u/KnottedSurface Feb 12 '12
http://xkcd.com/936/
This is all I have to say to this challenge.
11
u/Steve132 0 1 Feb 12 '12 edited Feb 12 '12
Python, 4 lines, double extra credit.
9
u/m_geist Feb 12 '12
Python: http://pastebin.com/gaMAnBpP
Generates x number of passwords of y length, outputs to a .txt file.
Sample output, 16 passwords of length 25: http://pastebin.com/4HMM845T
It might generate some less-than-ideal characters, I didn't spend much time tweaking what range it'd generate.
4
u/JerMenKoO 0 0 Feb 12 '12
4
u/nikoma Feb 12 '12 edited Feb 12 '12
code (python 3.2):
sample:
How many passwords do you wish to generate? 5
How many characters should each password have? 15
KNPgQoGycZtFOLc
touoaC28WcTARFP
iFtL4NmLn00eyf8
MpLBXaUTSDykhXS
OLDzeeBDoozDRkn
4
u/[deleted] Feb 12 '12 edited Feb 12 '12
Haven't actually done one of my own yet, here it is in Java
2
u/robin-gvx 0 2 Feb 12 '12
Double bonus in Déjà Vu: http://hastebin.com/raw/paxehedaxo
I needed to add rand to the stdlib to make it work (and that is a good thing).
2
u/Kealper Feb 12 '12 edited Feb 12 '12
AutoIt 3.3, going above-and-beyond the extra credit:
Allows user to choose what characters to use, the length of the passwords, and the number of passwords to output.
Includes comments to explain it, for anyone interested.
EDIT: Output of above code:
2
u/synaptk Feb 12 '12 edited Feb 12 '12
C++:
updated: http://codepad.org/RZCVI6ZH
Runs from the command line and generates x passwords of optional y length. The default password length is 8 characters.
For example:
passgen 16 - generates 16 passwords of 8 characters.
passgen 16 25 - generates 16 passwords of 25 characters.
Sample output:
http://codepad.org/DoxEVSch
2
u/Phridge Feb 13 '12
Javascript: Uses mouse movements as a seed (Robbed some bits from LunarWillies answer for it):
http://jsfiddle.net/ACZgP/10/
3
u/LunarWillie Feb 12 '12
Did it in Javascript :)
1
u/lnxaddct Feb 12 '12 edited Feb 12 '12
Command-line based Python solution (with both extra credits): https://gist.github.com/1810703
The non-command-line version here:
1
u/cooper6581 Feb 12 '12
37 lines in C with both extra credits: http://codepad.org/cs3APvEU
1
u/HazzyPls 0 0 Feb 12 '12
Really messy argument handling. Really messy way to limit the characters. But they work.
Sample output of "./ez 14 10"
884VH*BxApdU5F
7v3ICLgjiyQljH
c#7!eXE5^@uq1q
$VN!#dfHGB27N*
%xT6&lDX0-pATP
Bg&meR-W^EZhml
Q2LKI@zPxG3&E^
xquZMExZ28nBaG
mJuNs*wvHY_qI6
$PHWEgb5xH1_J9
C:
1
u/Arlaine Feb 13 '12 edited Feb 13 '12
C#
still learning so I wasn't sure how I could make it shorter :p
1
u/Haruhi_Suzumiya Feb 13 '12
PHP version
1
u/Cosmologicon 2 3 Feb 13 '12
Unix command version (generates 5 passwords of length 12):
That version just uses alphanumeric characters. To use punctuation as well, do this:
Fun fact, this is actually how I generate my passwords. :)
1
u/drb226 0 0 Feb 13 '12
20 lines of Haskell: http://hpaste.org/63600
Has worst case runtime of big-oh infinity; the random selection of alphaNums is rather hacky :) It's annoying that Haskell doesn't provide a nice, fast "select randomly from this set". I asked a question about this on stackoverflow a while ago: http://stackoverflow.com/questions/7349080/select-random-element-from-a-set-faster-than-linear-time-haskell
1
u/UnreasonableSteve Feb 13 '12
http://pastebin.com/5XuiXznB - PHP both extra credits Figured why not.
Didn't actually test this because I don't have a webserver readily accessible but I think it should work.
1
u/funny_falcon Feb 13 '12 edited Feb 13 '12
https://gist.github.com/1814635
$ ruby genpas.rb
Length of passwords[12]: 13
Number of passwords[3]: 4
Passwords:
ebamazofifira
lufahavelozak
pikisolocamiz
uwosesonymaje
1
u/imtrew Feb 13 '12
List Comprehension in Python <3
1
u/[deleted] Feb 13 '12 edited Feb 13 '12
-Edit- I fixed the code. It no longer calls srand(time(NULL)) in the for loop :O
Output:
1
u/bigmell Feb 13 '12
Here is a perl one liner, the first arg is the number of passwords to generate, the second is the length of the password. Hard to follow but the block generates an array of available chars (62) and selects one randomly. It prints the randomly selected char $nchar times with the List portion of the map. I wouldnt put something like this in production but wanted to try to solve with a one liner. Its cool I never explicitly generated a list using the ".." operator, the (a..z,A..Z,0..9)[rand 62] part. I could easily add special characters on the end of that as well.
1
u/mymainmanbrown Feb 22 '12
Python
1
u/ginolomelino Mar 18 '12
Javascript implementation with all extra credit.
1
u/jarjarbinks77 0 0 Apr 06 '12
Here is my overdone C++ code that goes above and beyond. Asks for length and amount and also lets you choose a combination of whether letters, capital letters, numbers, and symbols should be used in the password. Small amount of error checking built in. I try to stay with standard C++, this code compiled successfully on MSVC++ 2010.
http://pastebin.com/i149HQc2
Now that I have completed it I will now try to shorten the code. If I managed too ill add a link later.
1
u/savagecub Apr 10 '12
C++ asks for desired length.
// password gen.cpp : main project file.
include "stdafx.h"
include <iostream>
include <string>
include <cstring>
include <cctype>
include <fstream>
include <ctime>
include <cstdlib>
using namespace std;
int main() { int num;
}
1
u/netbyte 0 0 Apr 23 '12
Symbols and double alphabet for added randomness.
1
u/Jatak 0 0 Jun 20 '12 edited Jun 20 '12
In Python 3.2.3
1
u/[deleted] Jun 30 '12
Python 3 double EC:
http://pastebin.com/d2MyfpPx
1
u/[deleted] Jul 20 '12
My attempt in Java. Double extra credit, 40 lines http://pastebin.com/A8bnZmKR
1
u/[deleted] Jul 20 '12
learning to use new libraries, fuck yeah
1
u/programmingcaffeine 0 0 Jul 25 '12
Do you think having a. (which is the ISO-8859-1 alphabet) makes this too easy?
1
u/ragtag_creature Jul 09 '22
R - the Random library has a built-in feature that makes this pretty easy. There isn't an option for special characters
1
u/ARMIGER1 Apr 26 '12
Learning Ruby, so here's my version:
I definitely want to improve this, though. Any suggestions on doing so would definitely be appreciated.
0
u/[deleted] Feb 12 '12
[deleted] — view deleted comment