Class: S7::SecretGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/s7/secret_generator.rb

Overview

パスワードや暗証番号などの機密情報を自動生成する処理を表現する。

Constant Summary collapse

SYMBOL_CHARACTERS =

文字の集合を示すシンボルから実際の文字の配列へのハッシュ。

{
  :alphabet => ("a".."z").to_a + ("A".."Z").to_a,
  :upper_alphabet => ("A".."Z").to_a,
  :lower_alphabet => ("a".."z").to_a,
  :number => ("0".."9").to_a,
  # TODO: アスキーコードの表を確認する。
  :symbol => '!"#$%&\'()-=^~\\|@`[{;+:*]},<.>/?_'.scan(%r"."),
}

Class Method Summary collapse

Class Method Details

.generate(options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/s7/secret_generator.rb', line 16

def self.generate(options)
  res = ""
  characters = []
  (options[:characters] || [:alphabet, :number]).each do |sym|
    if SYMBOL_CHARACTERS.key?(sym)
      characters.concat(SYMBOL_CHARACTERS[sym])
    end
  end
  options[:length].times do
    res << characters[rand(characters.length)]
  end
  return res
end