Automatic password suggestion for your Rails app
Posted by Jeremy Voorhis Thu, 06 Apr 2006 06:17:00 GMT
Note that this will only work on UNIX systems.
inapp/helpers/admin/accounts_helper.rb:
module Admin::AccountsHelper
def password_recommendation
return "<p>Suggested password: <code>#{`apg`.split("\n").first}</code></p>" if use_apg?
end
# $ apg -v
# APG (Automated Password Generator)
# version 2.2.3 (PRNG: X9.17/CAST)
# Copyright (c) 1999, 2000, 2001, 2002, 2003 Adel I. Mirzazhanov
def use_apg?
`apg -v`.grep( /APG \(Automated Password Generator\)/ ) ? true : false
end
end
In app/views/admin/accounts/_form.rhtml:
...
<h3><label for="user[password]">Password</label></h3>
<div>
<%= password_recommendation %>
<%= password_field :user, :password %>
</div>
...
If apg is available on your system, you should see something like the following in your form:
Suggested password:
JidIlvEywr
Update
try this instead ;)
def use_apg?
`apg -v`.grep( /APG \(Automated Password Generator\)/ ).blank?? false : true
end
The original implementation was just plain incorrect, as #grep will return an empty Array when nothing is found. Empty Arrays are #blank?, but will evaluate to true.

Here’s something I use, and it’s all ruby. Need more variation? Just add more source characters! Longer length? 1 up to whatever…
Because generated passwords are impersonal, I try to generate ones that are easy to speak out loud, in order to remember them:
<code> def generate_password new_password = ”” consonants = “bcdfghjklmnprstv”; vowels = “aeiou”; </code>
Because generated passwords are impersonal, I like to generate ones that are easy to speak out loud, in order to make them easier to remember:
<pre><code> def generate_password new_password = ”” consonants = “bcdfghjklmnprstv”; vowels = “aeiou”; 3.times do new_password << consonants[rand(consonants.size-1)] new_password << vowels[rand(vowels.size-1)] end new_password << (rand(89)+10).to_s self.password = new_password end </code></pre>
Ruby/Password is also great for working with passwords..
It can check for weak passwords too:
Yes, a pure Ruby solution would have been excellent, but our systems administrator David has been using
apglately for things like subversion passwords, etc. The strength of it is that it produces random passwords containing letters and numbers – with an optional pronunciation guide!I admittedly did not spend enough time on the subject to research a pure ruby solution, but
apghelped me Get Things Done in a pinch.By the way, this is getting pushed down from the helper into the controller ;)
@Tomas Jogin,
Your code has a bug. rand(x) will return a random integer in the range 0..x-1. So you should not be subtracting 1 from the size of your strings, and for your final two-digit number, the parameter to rand should be 90.
Your technique (when implemented correctly) can generate 46 million different passwords, which is 25 bits of randomness. Depending on your adversary, this might not be much at all. If you wanted this to be a front end for 128 bit AES , you would be well short of what would be required, wasting much of the power of AES .
Bob
@Tomas Jogin,
Your code has a bug. rand(x) will return a random integer in the range 0..x-1. So you should not be subtracting 1 from the size of your strings, and for your final two-digit number, the parameter to rand should be 90.
Your technique (when implemented correctly) can generate 46 million different passwords, which is 25 bits of randomness. Depending on your adversary, this might not be much at all. If you wanted this to be a front end for 128 bit AES , you would be well short of what would be required, wasting much of the power of AES .
Bob
@Bob: This technique is not used for a front end of a 128 bit AES , so it’s cool. Thanks for the tip btw.
{{{{ def use_apg? `apg -v`.grep( /APG \(Automated Password Generator\)/ ).any? end
def use_apg? !`apg -v`.grep( /APG \(Automated Password Generator\)/ ).empty? end [/code] }}}}
{{{{ def use_apg? `apg -v`.grep( /APG \(Automated Password Generator\)/ ).any? end
def use_apg? !`apg -v`.grep( /APG \(Automated Password Generator\)/ ).empty? end [/code] }}}}
Ok, sorry for the double post (triple now). You really need a little box that says how to format.
Use #any?, or use !#empty?
Shelling out to a command? No thanks… I’ll stick with a pure Ruby solution.