Class: Cryptograpi::Cipher

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

Constant Summary collapse

CRYPTOFLAG =
0b0000001

Instance Method Summary collapse

Instance Method Details

#decryptor(obj, key, init_vector) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cryptograpi_ruby/cipher.rb', line 58

def decryptor(obj, key, init_vector)
  raise 'Invalid key length' if key.length != obj[:key_length]

  raise 'Invalid initialization vector length' if !init_vector.nil? && init_vector.length != obj[:init_vector_length]

  cipher = obj[:mode]
  cipher.decrypt
  cipher.key = key
  cipher.init_vector = init_vector

  cipher
end

#encryptor(obj, key, init_vector = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cryptograpi_ruby/cipher.rb', line 41

def encryptor(obj, key, init_vector = nil)
  # The 'key' parameter is a byte string that contains the key
  # for this encryption operation.
  # If there is an, correct length, initialization vector (init_vector)
  # then it is used. If not, the function generates one.
  raise 'Invalid key length' if key.length != obj[:key_length]

  raise 'Invalid initialization vector length' if !init_vector.nil? && init_vector.length != obj[:init_vector_length]

  cipher = obj[:mode]
  cipher.encrypt
  cipher.key = key
  init_vector = cipher.random_init_vector

  [cipher, init_vector]
end

#find_algorithm(id) ⇒ Object



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

def find_algorithm(id)
  set_algorithm.each do |k, v|
    return k if v[:id] == id
  end
  'unknown'
end

#get_algorithm(name) ⇒ Object



37
38
39
# File 'lib/cryptograpi_ruby/cipher.rb', line 37

def get_algorithm(name)
  set_algorithm[name]
end

#set_algorithmObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cryptograpi_ruby/cipher.rb', line 9

def set_algorithm
  @algorithm = {
    'aes-128-gcm' => {
      id: 0,
      algorithm: OpenSSL::Cipher::AES128,
      mode: OpenSSL::Cipher.new('aes-128-gcm'),
      key_length: 16,
      iv_length: 12,
      tag_length: 16
    },
    'aes-256-gcm' => {
      id: 1,
      algorithm: OpenSSL::Cipher::AES256,
      mode: OpenSSL::Cipher.new('aes-256-gcm'),
      key_length: 32,
      iv_length: 12,
      tag_length: 16
    }
  }
end