-
-
Save Epictetus/1050652 to your computer and use it in GitHub Desktop.
Thorを使ったパスワード生成Rubyスクリプトサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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