Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Password generator using optparse
Index -> Programming, Ruby -> Ruby Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Null




PostPosted: Thu Jun 15, 2006 10:46 pm   Post subject: Password generator using optparse

I had fun writing it, and it might just come in handy, so I thought I'd share. Very Happy

Examples of usage:
code:

$ ruby pgen.rb
soovmuvytv
$ ruby pgen.rb --length=20
bynnugipyeeghdbihcdn
$ ruby pgen.rb --length=20 -n
v3ji3awj1trzjlda5oax
$ ruby pgen.rb --length=20 -np
:sa6vafpummpttjp?qo:


For a full list of options, do
code:

$ ruby pgen.rb --help


code:

#!/usr/bin/env ruby

# pgen.rb
# by Jesse H-K on Thursday, June 15 2006

require 'optparse'

class Array
    def random_choice
        self[rand(self.length)]
    end
end

def parse_options(args)
    options = {}
 
    options[:length] = 10

    opts = OptionParser.new do |opts|
        opts.banner = "Usage: pgen.rb [options]"

        opts.on("-n",
                "--numbers",
                "Include the digits [0-9] in the password.") do |n|
                    options[:numb] = n
                end
       
        opts.on("-p",
                "--punctuation",
                "Include common punctuation characters in the password.") do |p|
                    options[:punct] = p
                end
       
        opts.on("-u",
                "--uppercase",
                "Include uppercase letters in the password.") do |u|
                    options[:upcase] = u
                end

        opts.on("-l",
                "--length [LENGTH]",
                Integer,
                "The length of the password. Default is 10 characters.") do |l|
                    options[:length] = l
                end
    end

    opts.parse!
    options
end

class PasswordGenerator
    @@options = { :numb  => ('0'..'9').to_a,
                  :punct    => %w(. , ! ? @ & :),
                  :upcase   => ('A'..'Z').to_a }

    def initialize(length, *opts)
        @possible = *('a'..'z')
        @length = length

        parse_content_options(opts)
    end

    def parse_content_options(opts)
        opts.each { |opt| @possible.concat(@@options[opt]) if @@options.key? opt }
    end

    def generate
        (1..@length).inject("") { |passwd, e| passwd << @possible.random_choice }
    end

    private :parse_content_options
end
       
options = parse_options(ARGV)
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Jun 16, 2006 7:06 am   Post subject: (No subject)

Excellent.

code:
class Array
    def random_choice
        self[rand length]
    end
end


Wink
Null




PostPosted: Fri Jun 16, 2006 10:14 am   Post subject: (No subject)

Still not as fun as writing
code:

3.factorial


Wink
wtd




PostPosted: Fri Jun 16, 2006 10:19 am   Post subject: (No subject)

Indeed.
Null




PostPosted: Sat Jun 17, 2006 9:20 pm   Post subject: (No subject)

!!!

I just realized that the code I posted doesn't work. Somehow, the last line was not included!

here is the correct version:

code:

#!/usr/bin/env ruby

# pgen.rb
# by Jesse H-K on Thursday, June 15 2006

require 'optparse'

class Array
    def random_choice
        self[rand(self.length)]
    end
end

def parse_options(args)
    options = {}
 
    options[:length] = 10

    opts = OptionParser.new do |opts|
        opts.banner = "Usage: pgen.rb [options]"

        opts.on("-n",
                "--numbers",
                "Include the digits [0-9] in the password.") do |n|
                    options[:numb] = n
                end
       
        opts.on("-p",
                "--punctuation",
                "Include common punctuation characters in the password.") do |p|
                    options[:punct] = p
                end
       
        opts.on("-u",
                "--uppercase",
                "Include uppercase letters in the password.") do |u|
                    options[:upcase] = u
                end

        opts.on("-l [LENGTH]",
                "--length [LENGTH]",
                Integer,
                "The length of the password. Default is 10 characters.") do |l|
                    options[:length] = l
                end
    end

    opts.parse!
    options
end

class PasswordGenerator
    @@options = { :numb     => ('0'..'9').to_a,
                  :punct    => %w(. , ! ? @ & :),
                  :upcase   => ('A'..'Z').to_a }

    def initialize(length, *opts)
        @possible = *('a'..'z')
        @length = length

        parse_content_options(opts)
    end

    def parse_content_options(opts)
        opts.each { |opt| @possible.concat(@@options[opt]) if @@options.key? opt }
    end

    def generate
        (1..@length).inject("") { |passwd, e| passwd << @possible.random_choice }
    end

    private :parse_content_options
end
       
options = parse_options(ARGV)
puts PasswordGenerator.new(options[:length], *options.keys).generate


Sorry about that. Sad
Cowzero




PostPosted: Mon Feb 18, 2019 2:33 am   Post subject: RE:Password generator using optparse

I don't understand this code.
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: