Class: S7n::Cipher

Inherits:
Object
  • Object
show all
Defined in:
lib/s7n/cipher.rb

Overview

秘密鍵暗号化方式を用いた暗号化処理を表現する。

Direct Known Subclasses

Aes256CbcCipher, BfCbcCipher, PlainCipher

Constant Summary collapse

@@cipher_classes =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Cipher

Returns a new instance of Cipher.



28
29
30
# File 'lib/s7n/cipher.rb', line 28

def initialize(options)
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



26
27
28
# File 'lib/s7n/cipher.rb', line 26

def options
  @options
end

Class Method Details

.create_instance(type, options) ⇒ Object



11
12
13
# File 'lib/s7n/cipher.rb', line 11

def create_instance(type, options)
  return @@cipher_classes[type].new(options)
end

Instance Method Details

#decrypt(io) ⇒ Object

io で指定した IO からデータを読み込み、復号する。



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/s7n/cipher.rb', line 33

def decrypt(io)
  begin
    cipher = create_cipher
    cipher.decrypt
    parse_options(cipher, options)
    s = ""
    while d = io.read(cipher.block_size)
      s << cipher.update(d)
    end
    s << cipher.final
  rescue OpenSSL::Cipher::CipherError
    raise InvalidPassphrase
  end
  return s
end

#encrypt(io) ⇒ Object

io で指定した IO からデータを読み込み、暗号化する。



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/s7n/cipher.rb', line 50

def encrypt(io)
  cipher = create_cipher
  cipher.encrypt
  parse_options(cipher, options)
  s = ""
  while d = io.read(cipher.block_size)
    s << cipher.update(d)
  end
  s << cipher.final
  return s
end