Simple Password Generator in AS 3.0 (Tutorial)

Welcome again to my website, I am here with a new and fresh lesson Simple Password Generator in AS 3.0 (Tutorial), I hope it will be helpful for many of you. In this tutorial I will show you how to make a simple password generator in Flash Builder using AS 3.0 and Flex.

Final Result Preview




First of all, you will need to create a new Flex Project, by clicking File -> New – > Flex Project. Name the project whatever you want, and then click Finish.

Code

Now, let’s take a look at this code:


<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
			   xmlns:s="library://ns.adobe.com/flex/spark"
			   xmlns:mx="library://ns.adobe.com/flex/mx"
			   width="450" height="150" minWidth="955" minHeight="600" backgroundColor="#ACACAC"
			   preloaderChromeColor="#D2D2D2">
	
	
	<fx:Script>
		<![CDATA[
			
			
			
			protected function generatePass(event:MouseEvent):void
			{
				// TODO Auto-generated method stub
				var PassLenght:int = lenght.value;
			    var passArray:Array =  new Array();
			    var password:String;
			
				for(var i:int = 0; i < PassLenght; i++)
				{
					var r:int;
					if(numbers.selected == false)
					{
						r = Math.floor(Math.random()*(1+122-65))+65;
					}
					else
					{
						r = Math.floor(Math.random()*(1+122-48))+48
					}
					
					passArray.push(String.fromCharCode(r));
					password = passArray.join("");
					
					txt.text = password;
				}
			}
			
			
			protected function savePass(event:MouseEvent):void
			{
				// TODO Auto-generated method stub
				var saveFile:FileReference =  new FileReference();
				
				saveFile.save(txt.text, "myPass");
			}
			
		]]>
	</fx:Script>
	
	
	
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	<s:TextArea x="6" y="10" id="txt" height="32" editable="false" fontFamily="Georgia" fontSize="20"/>
	<s:Button x="336" y="9" width="100" height="32" label="Generate" click="generatePass(event)"/>
	<s:CheckBox x="10" y="66" id="numbers" width="101" height="32" label="Numbers" enabled="true" selected="true"/>
	<s:Label x="9" y="115" id="Passlenght" fontSize="15" text="Password lenght:"/>
	<s:HSlider id="lenght" x="134" y="110" height="21"
			   liveDragging="true" maximum="24" minimum="4"/>
	<s:Button x="336" y="112" width="100" height="32" label="Save" enabled="true"
			  skinClass="spark.skins.spark.DefaultButtonSkin" id="saveBtn" click="savePass(event)"/>
</s:Application>


So, first of all I will take the last part of my code and I’ll explain it, this part is where our components are added to the stage.

<s:TextArea x="6" y="10" id="txt" height="32" editable="false" fontFamily="Georgia" fontSize="20"/>

This is the text field where our password is generated, as you can see, the parameters that were used are very simple to understand(id, x ,y, etc.). I don’t think you have any problem in understanding this.

<s:Button x="336" y="9" width="100" height="32" label="Generate" click="generatePass(event)"/>

Next is the button that generate our random password. We have a CLICK event that generate a function called generatePass. In fact this function makes our application works, she is the main function that generate the password.

<s:CheckBox x="10" y="66" id="numbers" width="101" height="32" label="Numbers" enabled="true" selected="true"/>

If you want any number in your password, check this box, if not then unchecked this box. Is very simple.

<s:Label x="9" y="115" id="Passlenght" fontSize="15" text="Password lenght:"/>
	<s:HSlider id="lenght" x="134" y="110" height="21"
			   liveDragging="true" maximum="24" minimum="4"/>

Here, we have a Label that contains a string and a HSlider that return the number oh characters that the password will have.

<s:Button x="336" y="112" width="100" height="32" label="Save" enabled="true"
			  skinClass="spark.skins.spark.DefaultButtonSkin" id="saveBtn" click="savePass(event)"/>

And finally another Button, that saves the password in your computer, in order not to forget it.

Now, let’s take a look at our AS 3.0 code:

protected function generatePass(event:MouseEvent):void
			{
				// TODO Auto-generated method stub
				var PassLenght:int = lenght.value;
			    var passArray:Array =  new Array();
			    var password:String;
			
				for(var i:int = 0; i < PassLenght; i++)
				{
					var r:int;
					if(numbers.selected == false)
					{
						r = Math.floor(Math.random()*(1+122-65))+65;
					}
					else
					{
						r = Math.floor(Math.random()*(1+122-48))+48
					}
					
					passArray.push(String.fromCharCode(r));
					password = passArray.join("");
					
					txt.text = password;
				}
			}

The first function, is that one that generate the password. At first I declare three variables, PassLenght => return the number that HSlider gives, passArray => this is only an array that will hold the password and password that is a String and finally this will be our password.

This loop is very easy to understand as well:

for(var i:int = 0; i < PassLenght; i++)
				{
					var r:int;
					if(numbers.selected == false)
					{
						r = Math.floor(Math.random()*(1+122-65))+65;
					}
					else
					{
						r = Math.floor(Math.random()*(1+122-48))+48
					}
					
					passArray.push(String.fromCharCode(r));
					password = passArray.join("");
					
					txt.text = password;
				}

What I’m doing here is something like : “If the CheckBox is unchecked => generate a password without any number if the CheckBox is checked => generate a password with numbers”. In order to do that I looked in ASCII code, and I saw that the letters are between (65 – 122) and the numbers are between (48 – 57), and this is how my “r” variable works. If the CheckBox is unchecked, my “r” variable generate numbers between (65 – 122), if the CheckBox is checked my “r” variable generate numbers between (48 – 122).
After that, I transformed my numbers into characters, and I push them into an array.

This line of code:

password = passArray.join("");

remove the comma between the characters.

Next function is a very very simple one:

protected function savePass(event:MouseEvent):void
			{
				// TODO Auto-generated method stub
				var saveFile:FileReference =  new FileReference();
				
				saveFile.save(txt.text, "myPass");
			}

This, save your password in your computer.

I hope this tutorial will help you, and if you have any questions don’t hesitate to ask.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

WordPress SEO