Class: RacoonEncrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/racoon_encrypt.rb,
lib/racoon_encrypt/version.rb

Overview

たぬき暗号のクラス

Author:

  • as-is-prog

Constant Summary collapse

VERSION =

バージョン

"0.1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enc_char = "た") ⇒ RacoonEncrypt

コンストラクタ

Parameters:

  • enc_char (String) (defaults to: "た")

    暗号化に使う文字



10
11
12
# File 'lib/racoon_encrypt.rb', line 10

def initialize(enc_char="")
  self.enc_char = enc_char
end

Instance Attribute Details

#enc_charString

暗号化に使う文字

Returns:

  • (String)

    the current value of enc_char



7
8
9
# File 'lib/racoon_encrypt.rb', line 7

def enc_char
  @enc_char
end

Instance Method Details

#decrypt(str) ⇒ String

復号メソッド

Examples:

たぬきうどん

racoon_encrypt.decrypt("たうたどたん") #=>"うどん"

Parameters:

  • str (String)

    暗号化された文字列

Returns:

  • (String)

    暗号化後の文字列



27
28
29
30
# File 'lib/racoon_encrypt.rb', line 27

def decrypt(str)
  regex = Regexp.compile(@enc_char)
  str.gsub(regex,'')
end

#encrypt(str) ⇒ String

暗号化メソッド

Examples:

貯蓄は大事。お金はたくわえたい。

racoon_encrypt.decrypt("おかね") #=> ランダムにたくわえられた文字列

Parameters:

  • str (String)

    暗号化したい文字列

Returns:

  • (String)

    複合された文字列

Raises:

  • 既にenc_charが文字列に含まれている場合



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/racoon_encrypt.rb', line 38

def encrypt(str)
  raise "文字列に既に[#{@enc_char}]が含まれています!" if str.include?(@enc_char)
  ta_count = str.length * 2 / 3
  ta_array = (@enc_char*ta_count).split("")
  (str.length - ta_count).times{ta_array << ""}
  ta_array.shuffle!

  retstr = ""
  str.length.times do |i|
    retstr << (ta_array[i] + str[i])
  end
  retstr
end