<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>KADIMI &#187; Security</title>
	<atom:link href="http://www.kadimi.com/en/tag/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kadimi.com/en</link>
	<description>[Web developper, Linux addict, Technical translator...]</description>
	<lastBuildDate>Tue, 08 May 2012 17:46:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Generate passwords with JavaScript</title>
		<link>http://www.kadimi.com/en/random-password-string-107</link>
		<comments>http://www.kadimi.com/en/random-password-string-107#comments</comments>
		<pubDate>Sat, 06 Jun 2009 03:56:50 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=107</guid>
		<description><![CDATA[The function generatePassword() is a simple yet customizable function that generates a random password string, the default password length is 6, you can override it by calling the function like this  generatePassword(n) where n is the password length. The possible characters are defined within the function with the variable chars The function: generatePassword([len=6]) 1 2 [...]]]></description>
			<content:encoded><![CDATA[<p><script src="http://www.kadimi.com/en/wp-content/uploads/generatePassword.js" type="text/javascript"></script><br />
The function <strong><code>generatePassword()</code></strong> is a simple yet customizable function that generates a random password string, the default password length is 6, you can override it by calling the function like this  <strong><code>generatePassword(n)</code></strong> where <strong><code>n</code></strong> is the password length. The possible characters are defined within the function with the variable <code>chars</code><span id="more-107"></span></p>
<h2>The function: generatePassword([len=6])</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> generatePassword<span style="color: #009900;">&#40;</span>len<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	len	<span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>len<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>len<span style="color: #009900;">&#41;</span>
		len <span style="color: #339933;">=</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> password <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> chars    <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> charsN   <span style="color: #339933;">=</span> chars.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> nextChar<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		nextChar <span style="color: #339933;">=</span> chars.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>charsN<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		password <span style="color: #339933;">+=</span> nextChar<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> password<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Usage</h2>
<p>The first example show the function without the optional <code>len</code> patamater(string length), and second example makes use of it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Example 1: No len</span>
window.<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;A random string of 6 characters: &quot;</span> <span style="color: #339933;">+</span> generatePassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Example 2: Using len</span>
pwdLen <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
window.<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;A random string of &quot;</span> <span style="color: #339933;">+</span> pwdLen <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;characters: &quot;</span> <span style="color: #339933;">+</span> generatePassword<span style="color: #009900;">&#40;</span>pwdLen<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Try it</h2>
<p><a onclick="pass = generatePassword(128);alert('A random string of 128 characters: '+pass);return false;" href="#">Generate</a> a random password of 128 characters.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/random-password-string-107/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

