I built a small program tonight to better my understanding of how strings concatenate from multiple sources. I decided a simple password generator was the way to approach this. Everything I have done before has been in Python 2.x. I have decided as of today I am making the leap into Python 3.x.
I have included my code below. Hopefully I have cleaned it up enough. When trying to learn concepts I tend to write a lot of tests in-line, run, then clean up. This isn't the best way.
I first start by creating my available characters in strings. I realize you could just put this all in one string, or array, but this was not the point of what I wanted to learn.
I then built the only user interaction, which asks how long of a password you need.
After this we build our primary random string. We first add a random number (somewhere between 10 and 1000) of lower case letters, then capital letters, followed by available symbols, and finally numbers.
This should give us a string of between 40 and 4000. From there we can use our PRNG included within python to pull a number of random values from this string by the index number within the string. Then we iterate and return the number of values needed for our password.
*Note* This is not a secure password vault/generator/etc *note*
This password is then printed for your viewing pleasure, it is also placed into your computer's clipboard so you can use it where needed.
It was a fun quick project which allowed me to learn some fun concepts. For example if I were to use
randStr = randStr.join([number[randomIndex((number.__len__() -1))] for i in range(randomGenerator())])
instead of
randStr = randStr + ''.join([number[randomIndex((number.__len__() -1))] for i in range(randomGenerator())])
Then it produced more numbers than wanted (exponentially more). This caused even this simple program to take up 100mb of ram. I also would not get a good sample of all 4 categories for the password as a result. It was often building passwords with 1 cap in it and the rest lower case.
The other big win for me from this project was the join statements themselves. I found another blog explaining how to properly concatenate strings in python. Initially I dove into this with no research and just started writing code (this seems to help me wind down as well). Though lack of planning beforehand does not always make for even decent code...
Initially I was doing the below to build and join my random string
def addStr(randomLen):
value = ''
for num in range(randomLen):
value += randomLet[randomIndex]
return value
But this post:
https://waymoot.org/home/python_string/
showed me how to improve my efficiency greatly
def addStr(randomLen):
return ''.join([randomLet[randomIndex] for i in range(randomLen)])
This allowed me to go from about 3k concatenations per second, clear up to 119k. While this honestly doesn't matter for this program, it is something to tuck away for later.
Next, after all my experience with BSAFE I think, I will look at how to write secure numbers in Python (build a PRNG that will pull data from random or urandom. But also that will keep it secured and clear everything as it completes.
Would be interesting to see what libs are out there to do so and a ton of fun to try and write a few things with the standard libs only. I have always enjoyed Python, but the more I use it, the more I love Python so much more than Java or C/C++.
Enjoy!
Friday, January 8, 2016
Code again
It has been a while since I have updated this. I received the last of the parts for my CoreXY/H-bot designed 3d printer. I should be putting that together over the next 2 weeks. I'll document this as best as I can.
Tonight I came home tired, so I decided to write a small python program just to exercise and to help me learn Python 3.x. I will put up another post specifically about this.
Tonight I came home tired, so I decided to write a small python program just to exercise and to help me learn Python 3.x. I will put up another post specifically about this.
Subscribe to:
Comments (Atom)