<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: password code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 14:35:22 GMT</pubDate>
    <description>DZone Snippets: password code</description>
    <item>
      <title>Random password generator in PHP</title>
      <link>http://snippets.dzone.com/posts/show/5583</link>
      <description>A little function which generates random password of a certain length. It uses all letters, both lowercase and uppercase and all numbers.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//generates a random password which contains all letters (both uppercase and lowercase) and all numbers&lt;br /&gt;function generatePassword($length) {&lt;br /&gt;$password='';&lt;br /&gt;for ($i=0;$i&lt;=$length;$i++) {&lt;br /&gt;	$chr='';&lt;br /&gt;	switch (mt_rand(1,3)) {&lt;br /&gt;		case 1:&lt;br /&gt;			$chr=chr(mt_rand(48,57));&lt;br /&gt;			break;&lt;br /&gt;		case 2:&lt;br /&gt;			$chr=chr(mt_rand(65,90));&lt;br /&gt;			break;&lt;br /&gt;		case 3:&lt;br /&gt;			$chr=chr(mt_rand(97,122));&lt;br /&gt;			&lt;br /&gt;	}&lt;br /&gt;	$password.=$chr;&lt;br /&gt;}	&lt;br /&gt;return $password;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 04 Jun 2008 14:08:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5583</guid>
      <author>fest (Reinis Veips)</author>
    </item>
    <item>
      <title>Get a Jaiku personal_key given a username and password</title>
      <link>http://snippets.dzone.com/posts/show/5539</link>
      <description>Logs into Jaiku and then gets the personal_key for using the API.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def GetJaikuPersonalKey(user, password):&lt;br /&gt;  """Finds the Jaiku API personal_key from a username and password.&lt;br /&gt;&lt;br /&gt;  One day I'll learn to use urllib2 properly and cookie parsing and stuff.&lt;br /&gt;  """&lt;br /&gt;&lt;br /&gt;  # login and find a cookie&lt;br /&gt;  login_url = "http://jaiku.com/login"&lt;br /&gt;  request_body = urllib.urlencode({'log': user,&lt;br /&gt;                                   'pwd': password,&lt;br /&gt;                                   'rememberme': '1'})&lt;br /&gt;  # Open a connection to the authentication server.&lt;br /&gt;  auth_connection = httplib.HTTPConnection('jaiku.com')&lt;br /&gt;  # Begin the POST request to the client login service.&lt;br /&gt;  auth_connection.putrequest('POST', '/login')&lt;br /&gt;  # Set the required headers for an Account Authentication request.&lt;br /&gt;  auth_connection.putheader('Content-type',&lt;br /&gt;                            'application/x-www-form-urlencoded')&lt;br /&gt;  auth_connection.putheader('Content-Length', str(len(request_body)))&lt;br /&gt;  auth_connection.endheaders()&lt;br /&gt;  auth_connection.send(request_body)&lt;br /&gt;  auth_response = auth_connection.getresponse()&lt;br /&gt;  if auth_response.status == 303:&lt;br /&gt;    cookie_str = auth_response.getheader("set-cookie")&lt;br /&gt;    # TODO(ark) parse this properly!&lt;br /&gt;    res = re.search("(jaikuuser_[^;]*).*(jaikupass_[^;]*)", cookie_str)&lt;br /&gt;    if res:&lt;br /&gt;      auth_cookie = "%s; %s" % (res.group(1), res.group(2))&lt;br /&gt;      apikey_url = "http://api.jaiku.com/key"&lt;br /&gt;      req = urllib2.Request(url=apikey_url)&lt;br /&gt;      req.add_header('Cookie', auth_cookie)&lt;br /&gt;      f = urllib2.urlopen(req)&lt;br /&gt;      return f.read()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 25 May 2008 23:21:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5539</guid>
      <author>ark (ark)</author>
    </item>
    <item>
      <title>Encode simple passwords</title>
      <link>http://snippets.dzone.com/posts/show/5104</link>
      <description>This code was used to demonstrate how to translate easy to remember simple (weak) passwords into more difficult to guess (strong) passwords.  Example: Using Gmail I like an easy to remember password, I submit the password 'jr123' to the password_lookup.html page and what's returned to me is a stronger password 'NCC2SI1T'.&lt;br /&gt;&lt;br /&gt;file: passwd_lookup.rb (generates an xml file containing an alphanumeric index with corresponding cryptic values)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class PasswordLookup&lt;br /&gt;&lt;br /&gt;  def initialize()&lt;br /&gt;    chars =  (0..9).to_a  + Array.new(7) + ('A'..'Z').to_a + Array.new(6) + ('a'..'z').to_a &lt;br /&gt;    @chars = (0..9).to_a  + ('A'..'Z').to_a + ('a'..'z').to_a &lt;br /&gt;    @doc = Document.new()&lt;br /&gt;    root = Element.new('codes')&lt;br /&gt;    @doc.add_element(root)&lt;br /&gt;&lt;br /&gt;    chars.each do |char|&lt;br /&gt;      node = Element.new('code')&lt;br /&gt;      if not char.nil? &lt;br /&gt;        node.attributes['index'] = char&lt;br /&gt;        node.attributes['value'] = get_random_chars(2)&lt;br /&gt;      end&lt;br /&gt;      root.add_element(node)&lt;br /&gt;    end&lt;br /&gt;    puts root&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;  def save(filepath)&lt;br /&gt;    file = File.new(filepath,'w')&lt;br /&gt;    file.puts @doc&lt;br /&gt;    file.close&lt;br /&gt;  end&lt;br /&gt;        &lt;br /&gt;  def get_random_chars(vword_size)&lt;br /&gt;    newpass = Array.new(rand(vword_size) + 1, '').collect{@chars[rand(@chars.size)]}.join&lt;br /&gt;    # return the encryption providing it doesn't already exist in the lookup table.&lt;br /&gt;    if not /value=\'#{newpass}\'/.match @doc.root.elements.to_a.to_s &lt;br /&gt;     return newpass &lt;br /&gt;    else&lt;br /&gt;     return get_random_chars(vword_size) &lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  private :get_random_chars&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;output extract: (codes - see also http://rorbuilder.info/pl/codes)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;codes&gt;&lt;br /&gt;&lt;code value='4h' index='a'/&gt;&lt;code value='B' index='b'/&gt;&lt;code value='m' index='c'/&gt;&lt;br /&gt;&lt;code value='qf' index='d'/&gt;&lt;br /&gt;&lt;/codes&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: password_lookup.js&lt;br /&gt;&lt;code&gt;&lt;br /&gt;var t;&lt;br /&gt;var m_doc;&lt;br /&gt;&lt;br /&gt;function loadXml() {&lt;br /&gt;  url = 'http://rorbuilder.info/pl/codes';&lt;br /&gt;  m_doc = XML.load(url);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function getCode(val,i) {&lt;br /&gt;  pos = val.charCodeAt(i) - 48;&lt;br /&gt;  node = m_doc.documentElement.childNodes[pos]&lt;br /&gt;  return node.getAttribute('value');&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function timed_update(keyCode,  val) {&lt;br /&gt;  if (val.length &gt; 0 &amp;&amp; ((keyCode &gt; 40) || (keyCode == 8)) ) {&lt;br /&gt;    clearTimeout(t);&lt;br /&gt;    t = setTimeout("revealCode('" + val + "')", 1000);&lt;br /&gt;  }&lt;br /&gt;  else&lt;br /&gt;  {  &lt;br /&gt;    o = document.getElementById('out1');&lt;br /&gt;    if (val.length &lt;= 0 &amp;&amp; o.value.length &gt; 0) {&lt;br /&gt;      o.value = '';&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function revealCode(val) {&lt;br /&gt;  var iEnd = val.length;&lt;br /&gt;  var newcode = '';&lt;br /&gt;  for (i=0;i&lt;iEnd;i++) {&lt;br /&gt;      &lt;br /&gt;    var codex = getCode(val,i);&lt;br /&gt;    newcode += codex;&lt;br /&gt;  }&lt;br /&gt;  update(newcode);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function update(val){&lt;br /&gt;  o = document.getElementById('out1');&lt;br /&gt;  o.value = val;&lt;br /&gt;  /*var o_copied = document.getElementById('out1').createTextRange();&lt;br /&gt;  o_copied.exeCommand("Copy");*/&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: password_lookup.html&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  &lt;body onload="loadXml()"&gt;&lt;br /&gt;    &lt;h1&gt;Password lookup&lt;/h1&gt;&lt;br /&gt;    &lt;dl&gt;&lt;br /&gt;    &lt;dt&gt;&lt;label for="in1"&gt;Enter password:&lt;/label&gt;&lt;/dt&gt;    &lt;br /&gt;    &lt;dd&gt;&lt;input type="text" name="in1" id="in1" value="" &lt;br /&gt;  onkeyup="timed_update(event.keyCode, this.value)" /&gt;&lt;/dd&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;dt&gt;&lt;label for="out1"&gt;Generated password&lt;/label&gt;&lt;/dt&gt;&lt;br /&gt;    &lt;dd&gt;&lt;input type="text" name="out1" id="out1" value=""/&gt;&lt;/dd&gt;&lt;br /&gt;    &lt;dd&gt;&lt;input type="button" name="clear1" id="clear1" onclick="clearPassword()" value="clear"/&gt;&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/dl&gt;&lt;br /&gt;    &lt;p&gt;see also: &lt;a href="codes.xml" title="password code lookup table"&gt;codes.xml&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;  &lt;/body&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Try out the  &lt;a href="http://rorbuilder.info/pl/password_lookup.html"&gt;encode a simple password demo&lt;/a&gt; [rorbuilder.info]. &lt;br /&gt;&lt;br /&gt;see also: &lt;a href="http://snippets.dzone.com/posts/show/5091"&gt;Reading an XML file usng JavaScript&lt;/a&gt; [snippets.dzone.com]</description>
      <pubDate>Mon, 04 Feb 2008 17:30:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5104</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Ruby password strength calculator</title>
      <link>http://snippets.dzone.com/posts/show/4698</link>
      <description>This method returns the password lifetime in years. Based on this:&lt;br /&gt;http://www.codeandcoffee.com/2007/06/27/how-to-make-a-password-strength-meter-like-google&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class String&lt;br /&gt;  PASSWORD_SETS = {&lt;br /&gt;    /[a-z]/ =&gt; 26,&lt;br /&gt;    /[A-Z]/ =&gt; 26,&lt;br /&gt;    /[0-9]/ =&gt; 10,&lt;br /&gt;    /[^\w]/ =&gt; 32&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  def password_strength&lt;br /&gt;    set_size = 0&lt;br /&gt;    PASSWORD_SETS.each_pair {|k,v| set_size += v if self =~ k}&lt;br /&gt;    &lt;br /&gt;    combinations = set_size ** length&lt;br /&gt;    &lt;br /&gt;    # assuming 1000 tries per second&lt;br /&gt;    days = combinations.to_f / 1000 / 86400&lt;br /&gt;    &lt;br /&gt;    days / 365&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 25 Oct 2007 14:35:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4698</guid>
      <author>ciconia (Sharon Rosner)</author>
    </item>
    <item>
      <title>Simple user model with password crypting</title>
      <link>http://snippets.dzone.com/posts/show/4676</link>
      <description>A simple user model. It's using the virtual password attribute 'password' to store the clear-text password. This is what e.g. forms use for password input. It stores this password in the password_hash column. &lt;br /&gt;&lt;br /&gt;It allows for user editing, using the same form as user creation. The password won't be updated, and validations will pass, if the user doesn't touch the password field in the form.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require "digesh/sha1"&lt;br /&gt;class User &lt; ActiveRecord::Base&lt;br /&gt;  validates_confirmation_of :password, :if =&gt; :perform_password_validation?&lt;br /&gt;  validates_presence_of :password, :if =&gt; :perform_password_validation?&lt;br /&gt;&lt;br /&gt;  before_save :hash_password&lt;br /&gt;  attr_accessor :password&lt;br /&gt;&lt;br /&gt;  # Returns true if the password passed matches the password in the DB&lt;br /&gt;  def valid_password?(password)&lt;br /&gt;    self.password_hash == self.class.hash_password(password)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  private&lt;br /&gt;&lt;br /&gt;  # Performs the actual password encryption. You want to change this salt to something else.&lt;br /&gt;  def self.hash_password(password, salt = "meeQue8Zucijoo7")&lt;br /&gt;    Dihest::SHA1.hexdigest(password, salt)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  # Sets the hashed version of self.password to password_hash, unless it's blank.&lt;br /&gt;  def hash_password&lt;br /&gt;    self.password_hash = self.class.hash_password(self.password) unless self.password.blank?&lt;br /&gt;  end&lt;br /&gt; &lt;br /&gt;  # Assert wether or not the password validations should be performed. Always on new records, only on existing&lt;br /&gt;  # records if the .password attribute isn't blank.&lt;br /&gt;  def perform_password_validation?&lt;br /&gt;    self.new_record? ? true : !self.password.blank?&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 19 Oct 2007 12:50:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4676</guid>
      <author>leethal (August Lilleaas)</author>
    </item>
    <item>
      <title>Random Characters with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/4667</link>
      <description>Use for a password or salt or whatever...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;(0..25).inject('') { |r, i| r &lt;&lt; rand(93) + 33 }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 18 Oct 2007 03:24:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4667</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Simple ASP page to reset passwords</title>
      <link>http://snippets.dzone.com/posts/show/4559</link>
      <description>// This page allows to reset an AD account password.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Reinitialisation de mot de passe&lt;/TITLE&gt;&lt;br /&gt;&lt;META http-equiv=Content-Type content="text/html; charset=iso-8859-1"&gt;&lt;LINK &lt;br /&gt;href="files/v2006.css" type=text/css rel=stylesheet&gt;&lt;br /&gt;&lt;BODY leftMargin=0 topMargin=30 marginwidth="0" marginheight="0"&gt;&lt;br /&gt;&lt;FORM method=post&gt;&lt;br /&gt;&lt;CENTER&gt;&lt;br /&gt;&lt;TABLE cellSpacing=0 cellPadding=1 width=500 align=center bgColor=#26333e &lt;br /&gt;border=0&gt;&lt;br /&gt;  &lt;TBODY&gt;&lt;br /&gt;  &lt;TR&gt;&lt;br /&gt;    &lt;TD&gt;&lt;br /&gt;      &lt;TABLE cellSpacing=0 cellPadding=1 width=500 align=center border=0&gt;&lt;br /&gt;        &lt;TBODY&gt;&lt;br /&gt;        &lt;TR bgColor=#f0f3f5&gt;&lt;br /&gt;          &lt;TD width=160&gt;&lt;IMG height=116 src="files/man_portable.jpg" &lt;br /&gt;            width=160&gt; &lt;/TD&gt;&lt;br /&gt;          &lt;TD width=302 bgColor=#e5ebef&gt;&lt;br /&gt;            &lt;TABLE cellSpacing=4 cellPadding=4 width="100%" align=center &lt;br /&gt;            border=0&gt;&lt;br /&gt;              &lt;TBODY&gt;&lt;br /&gt;              &lt;TR&gt;&lt;br /&gt;                &lt;TD class=medium colSpan=3 height=22&gt;&lt;br /&gt;                &lt;%&lt;br /&gt;                If Request.Form("login") = "" Then&lt;br /&gt;                	Response.Write("&lt;B&gt;Saisir le compte &#224; reinitialiser&lt;/B&gt;")&lt;br /&gt;                Else&lt;br /&gt;                    Dim Group, Member, Domain, UserFound&lt;br /&gt;                    ' &lt;br /&gt;                    Domain ="FR-ERM"&lt;br /&gt;                    '&lt;br /&gt;                    UserFound=0&lt;br /&gt;                    &lt;br /&gt;	                Set Group = GetObject("WinNT://" &amp; Domain &amp; "/Domain Users")&lt;br /&gt;    	            For Each Member In Group.Members&lt;br /&gt;    	            	' Response.Write(Member.Name &amp; "&lt;BR&gt;")&lt;br /&gt;        	        	If UCase(Member.Name) = UCase(Request.Form("login")) Then&lt;br /&gt;        	        		UserFound=1&lt;br /&gt;        	        		If Member.AccountDisabled Then &lt;br /&gt;        	        			Response.Write(" " &amp; Request.Form("login") &amp;" est un compte desactive&lt;/B&gt;")&lt;br /&gt;        	        			Exit For&lt;br /&gt;        	        		Else&lt;br /&gt;        	        			' Essai de reinit de mot de passe&lt;br /&gt;        	        			Dim res&lt;br /&gt;        	        			res=Member.SetPassword(Request.Form("pass"))&lt;br /&gt;        	        			'Member.Put "pwdLastSet", CLng(0)&lt;br /&gt;        	        			Member.Put "PasswordExpired", 1&lt;br /&gt;								Member.SetInfo&lt;br /&gt;        	        			Response.Write("&lt;B&gt;L'utilisateur "&amp; Request.Form("login") &amp; " a chang&#233; de mot de passe !&lt;BR&gt;&lt;BR&gt;&lt;/B&gt;Il devra changer de mot de passe au prochain login.")&lt;br /&gt;        	        			Exit For&lt;br /&gt;        	        		End If&lt;br /&gt;        	        	End If &lt;br /&gt;            	    Next      &lt;br /&gt;       	        	If UserFound = 0 Then&lt;br /&gt;       	        		Response.Write(Request.Form("login") &amp;" non trouv&#233; dans le domaine "&amp; Domain)&lt;br /&gt;       	        	End If           	            	&lt;br /&gt;                	&lt;br /&gt;                End If&lt;br /&gt;                %&gt;&lt;br /&gt;              &lt;br /&gt;                &lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;              &lt;TR&gt;&lt;br /&gt;                &lt;TD class=td11 width="1%" height=22&gt;Identifiant&lt;br /&gt;               &lt;br /&gt;                &lt;/TD&gt;&lt;br /&gt;                &lt;TD class=td11 colSpan=2&gt;Nouveau mot de passe&lt;/TD&gt;&lt;/TR&gt;&lt;br /&gt;              &lt;TR&gt;&lt;br /&gt;                &lt;TD width="25%"&gt;&lt;INPUT maxLength=20 name=login&gt;&lt;br /&gt; &lt;br /&gt;                &lt;/TD&gt;&lt;br /&gt;                &lt;TD width="20%"&gt;&lt;INPUT type=password maxLength=10 size=10 &lt;br /&gt;                  name=pass&gt; &lt;/TD&gt;&lt;br /&gt;                &lt;TD&gt;&lt;INPUT type=image height=18 alt="Reset !" width=15 &lt;br /&gt;                  src="files/submit.gif" value=login border=0 name=ok&gt; &lt;br /&gt;                &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/CENTER&gt;&lt;/FORM&gt;&lt;/BODY&gt;&lt;/HTML&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 20 Sep 2007 07:01:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4559</guid>
      <author>bouffon69 (Sylvain Le Courtois)</author>
    </item>
    <item>
      <title>PHP password generator</title>
      <link>http://snippets.dzone.com/posts/show/4558</link>
      <description>This is a random &lt;A href="http://www.webtoolkit.info/php-password-generator.html"&gt;php password generator&lt;/a&gt;. This function is a complete, working password generator implementation for PHP. It allows the developer to customize the password: set password length and strength. Just include this function anywhere in your code and then use it.&lt;br /&gt;&lt;br /&gt;More code snippets you can find on &lt;a href="http://www.webtoolkit.info/"&gt;free code and tutorials website&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function generatePassword($length=9, $strength=0) {&lt;br /&gt;    $vowels = 'aeuy';&lt;br /&gt;    $consonants = 'bdghjmnpqrstvz';&lt;br /&gt;    if ($strength &amp; 1) {&lt;br /&gt;        $consonants .= 'BDGHJLMNPQRSTVWXZ';&lt;br /&gt;    }&lt;br /&gt;    if ($strength &amp; 2) {&lt;br /&gt;        $vowels .= "AEUY";&lt;br /&gt;    }&lt;br /&gt;    if ($strength &amp; 4) {&lt;br /&gt;        $consonants .= '23456789';&lt;br /&gt;    }&lt;br /&gt;    if ($strength &amp; 8) {&lt;br /&gt;        $consonants .= '@#$%';&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    $password = '';&lt;br /&gt;    $alt = time() % 2;&lt;br /&gt;    srand(time());&lt;br /&gt;    for ($i = 0; $i &lt; $length; $i++) {&lt;br /&gt;        if ($alt == 1) {&lt;br /&gt;            $password .= $consonants[(rand() % strlen($consonants))];&lt;br /&gt;            $alt = 0;&lt;br /&gt;        } else {&lt;br /&gt;            $password .= $vowels[(rand() % strlen($vowels))];&lt;br /&gt;            $alt = 1;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return $password;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 19 Sep 2007 18:34:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4558</guid>
      <author>justas (Justas)</author>
    </item>
    <item>
      <title>CGI script for collecting username and password and storing them in a database table</title>
      <link>http://snippets.dzone.com/posts/show/4092</link>
      <description>// CGI script for collecting username and password and storing them in a database table&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/perl&lt;br /&gt;&lt;br /&gt;# $Id$&lt;br /&gt;&lt;br /&gt;# CGI script for collecting username and password and storing them in a database&lt;br /&gt;# table. The password is encrypted with Crypt::PasswdMD5 ready for passing to&lt;br /&gt;# useradd.&lt;br /&gt;&lt;br /&gt;use strict;&lt;br /&gt;use warnings;&lt;br /&gt;&lt;br /&gt;## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)&lt;br /&gt;our ($VERSION) = '$Revision$' =~ m{ \$Revision: \s+ (\S+) }xms;&lt;br /&gt;## use critic&lt;br /&gt;&lt;br /&gt;use CGI::Pretty qw(:standard -nosticky);&lt;br /&gt;use DBI;&lt;br /&gt;use Crypt::PasswdMD5;&lt;br /&gt;&lt;br /&gt;# Schema for database table to store account details:&lt;br /&gt;# &lt;br /&gt;# CREATE TABLE account (&lt;br /&gt;#     username varchar(50) NOT NULL,&lt;br /&gt;#     password varchar(50) NOT NULL,&lt;br /&gt;#     date_created datetime NOT NULL&lt;br /&gt;# );&lt;br /&gt;&lt;br /&gt;my $DBNAME = 'database';&lt;br /&gt;my $DBHOST = 'localhost';&lt;br /&gt;my $DBPORT = 3306;&lt;br /&gt;my $DBUSER = 'username';&lt;br /&gt;my $DBPASS = 'password';&lt;br /&gt;&lt;br /&gt;# Header&lt;br /&gt;my $q = new CGI;&lt;br /&gt;print $q-&gt;header(),&lt;br /&gt;      $q-&gt;start_html(&lt;br /&gt;          -title =&gt; 'New Account',&lt;br /&gt;          -lang  =&gt; 'en',&lt;br /&gt;      ),&lt;br /&gt;      $q-&gt;h1('New Account');&lt;br /&gt;&lt;br /&gt;my $submit    = $q-&gt;param('submit')    || q{};&lt;br /&gt;my $username  = $q-&gt;param('username')  || q{};&lt;br /&gt;my $password1 = $q-&gt;param('password1') || q{};&lt;br /&gt;my $password2 = $q-&gt;param('password2') || q{};&lt;br /&gt;&lt;br /&gt;my %ERROR = (&lt;br /&gt;    no_username         =&gt; 'You must specify a username.',&lt;br /&gt;    no_password         =&gt; 'You must specify a password.',&lt;br /&gt;    password_not_twice  =&gt; 'You must specify your password twice.',&lt;br /&gt;    passwords_not_match =&gt; 'Both passwords must match.',&lt;br /&gt;);&lt;br /&gt;&lt;br /&gt;my $error = (!$submit)                   ? undef                       :&lt;br /&gt;            (!$username)                 ? $ERROR{no_username}         :&lt;br /&gt;            (!$password1 &amp;&amp; !$password2) ? $ERROR{no_password}         :&lt;br /&gt;            (!$password1 || !$password2) ? $ERROR{password_not_twice}  :&lt;br /&gt;            ( $password1 ne  $password2) ? $ERROR{passwords_not_match} :&lt;br /&gt;                                           undef&lt;br /&gt;            ;&lt;br /&gt;&lt;br /&gt;if (!$submit) {&lt;br /&gt;    # Form not submitted, so display empty form&lt;br /&gt;    form($q);&lt;br /&gt;}&lt;br /&gt;elsif ($error) {&lt;br /&gt;    # Show error and redisplay form&lt;br /&gt;    print $q-&gt;p($error);&lt;br /&gt;    form($q, $username);&lt;br /&gt;}&lt;br /&gt;else {&lt;br /&gt;    # Enter account details into database&lt;br /&gt;    my $dsn = "DBI:mysql:database=$DBNAME;host=$DBHOST;port=$DBPORT";&lt;br /&gt;    my $dbh = DBI-&gt;connect($dsn, $DBUSER, $DBPASS);&lt;br /&gt;    &lt;br /&gt;    my $username_quoted = $dbh-&gt;quote(param('username'));&lt;br /&gt;    my $password_quoted = $dbh-&gt;quote(unix_md5_crypt(param('password1')));&lt;br /&gt;    &lt;br /&gt;    $dbh-&gt;do("&lt;br /&gt;        INSERT INTO account&lt;br /&gt;        (username, password, date_created)&lt;br /&gt;        VALUES ($username_quoted, $password_quoted, NOW())&lt;br /&gt;    ");&lt;br /&gt;    &lt;br /&gt;    print $q-&gt;p('Your username and password have been recorded.');&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;# Footer&lt;br /&gt;print $q-&gt;end_html();&lt;br /&gt;&lt;br /&gt;sub form {&lt;br /&gt;    my $q = shift;&lt;br /&gt;    my $username = shift || q{};&lt;br /&gt;    &lt;br /&gt;    print start_form(),&lt;br /&gt;          p('Username:', br(), textfield(&lt;br /&gt;              -name  =&gt; 'username',&lt;br /&gt;              -value =&gt; $username,&lt;br /&gt;          )),&lt;br /&gt;          p('Password:', br(), password_field(&lt;br /&gt;              -name =&gt; 'password1',&lt;br /&gt;          )),&lt;br /&gt;          p('Password (again):', br(), password_field(&lt;br /&gt;              -name =&gt; 'password2',&lt;br /&gt;          )),&lt;br /&gt;          p(submit(&lt;br /&gt;              -name  =&gt; 'submit',&lt;br /&gt;              -value =&gt; 'Submit',&lt;br /&gt;          )),&lt;br /&gt;          end_form();&lt;br /&gt;    &lt;br /&gt;    return;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 02 Jun 2007 07:35:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4092</guid>
      <author>iansealy (Ian Sealy)</author>
    </item>
    <item>
      <title>Password authentication without revealing your password</title>
      <link>http://snippets.dzone.com/posts/show/3975</link>
      <description>The majority of personalized web sites use some kind of form-based password authentication where you have two form fields for username and password, and a login button. When you submit your authentication, the password is sent in the clear to the server for verification against a user database.&lt;br /&gt;&lt;br /&gt;Using a Javascript SHA library and one simple onsubmit protects the password in transit and also inside the user database:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;form onsubmit="pwField.value = b64_sha256(pwField.value);"&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://blog.asgeirnilsen.com/2005/11/password-authentication-without.html"&gt;Read this&lt;/a&gt; for more elaborations with increased security.</description>
      <pubDate>Wed, 09 May 2007 19:59:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3975</guid>
      <author>asgeirn (Asgeir S. Nilsen)</author>
    </item>
  </channel>
</rss>
