Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created June 28, 2011 05:49
Show Gist options
  • Save rummelonp/1050567 to your computer and use it in GitHub Desktop.
Save rummelonp/1050567 to your computer and use it in GitHub Desktop.
Thorを使ったパスワード生成Rubyスクリプトサンプル
# -*- coding: utf-8 -*-
require 'rubygems'
require 'thor'
class Passgen < Thor
TABLE = {
:number => (0x30...0x3A).map(&:chr), # 0123456789
:upper => (0x41...0x5b).map(&:chr), # ABCDEFGHIJKLMNOPQRSTUVWXYZ
:lower => (0x61...0x7b).map(&:chr), # abcdefghijklmnopqrstuvwxyz
:symbol => (0x21...0x30).map(&:chr), # !\"\#$%&'()*+,-./
}.freeze
class_option :help, :alias => '-h', :type => :boolean, :desc => 'ヘルプを表示'
desc 'generate', 'パスワード生成'
method_option :length, :alias => '-l', :type => :numeric, :default => 8, :desc => 'パスワード文字数'
method_option :count, :alias => '-c', :type => :numeric, :default => 10, :desc => '生成個数'
method_option :number, :alias => '-n', :type => :boolean, :default => true, :desc => '数字を使用'
method_option :upper, :alias => '-u', :type => :boolean, :default => true, :desc => '大文字を使用'
method_option :lower, :alias => '-l', :type => :boolean, :default => true, :desc => '小文字を使用'
method_option :symbol, :alias => '-s', :type => :boolean, :default => false, :desc => '記号文字を使用'
def generate
prepare :generate
table = TABLE.dup.tap {|table|
TABLE.keys.each do |key|
table.delete key unless options.send key
end
}.values.flatten
rand_max = table.size
options[:count].times do
say [].tap {|r|
options[:length].times do
r << table[rand * rand_max]
end
}.join ''
end
end
desc 'help [TASK]', 'ヘルプを表示'
def help(*args)
super
end
private
def prepare(task)
if options.help?
help task
raise SystemExit
end
end
end
Passgen.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment