Class: Cryptor::Cipher

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

Overview

Base class of all Cryptor ciphers

Constant Summary collapse

REGISTRY =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm, options = {}) ⇒ Cipher

Returns a new instance of Cipher.



16
17
18
19
# File 'lib/cryptor/cipher.rb', line 16

def initialize(algorithm, options = {})
  @algorithm = algorithm
  @key_bytes = options[:key_bytes] || fail(ArgumentError, 'key_bytes not specified')
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



6
7
8
# File 'lib/cryptor/cipher.rb', line 6

def algorithm
  @algorithm
end

#key_bytesObject (readonly)

Returns the value of attribute key_bytes.



6
7
8
# File 'lib/cryptor/cipher.rb', line 6

def key_bytes
  @key_bytes
end

Class Method Details

.[](algorithm) ⇒ Object



12
13
14
# File 'lib/cryptor/cipher.rb', line 12

def self.[](algorithm)
  REGISTRY[algorithm.to_s] || fail(ArgumentError, "no such cipher: #{algorithm}")
end

.register(algorithm, options = {}) ⇒ Object



8
9
10
# File 'lib/cryptor/cipher.rb', line 8

def self.register(algorithm, options = {})
  REGISTRY[algorithm.to_s] ||= new(algorithm, options)
end

Instance Method Details

#decrypt(_key, _ciphertext) ⇒ Object



31
32
33
34
35
# File 'lib/cryptor/cipher.rb', line 31

def decrypt(_key, _ciphertext)
  #:nocov:
  fail NotImplementedError, "'decrypt' method has not been implemented"
  #:nocov:
end

#encrypt(_key, _plaintext) ⇒ Object



25
26
27
28
29
# File 'lib/cryptor/cipher.rb', line 25

def encrypt(_key, _plaintext)
  #:nocov:
  fail NotImplementedError, "'encrypt' method has not been implemented"
  #:nocov:
end

#random_keyObject



21
22
23
# File 'lib/cryptor/cipher.rb', line 21

def random_key
  SecretKey.random_key(self)
end