Class: Cryptor::SymmetricEncryption::Cipher

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

Overview

Base class of all Cryptor::SymmetricEncryption 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.



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

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.



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

def algorithm
  @algorithm
end

#key_bytesObject (readonly)

Returns the value of attribute key_bytes.



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

def key_bytes
  @key_bytes
end

Class Method Details

.[](algorithm) ⇒ Object



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

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

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



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

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

Instance Method Details

#decrypt(_key, _ciphertext) ⇒ Object



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

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

#encrypt(_key, _plaintext) ⇒ Object



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

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

#random_keyObject



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

def random_key
  SecretKey.random_key(self)
end