Class: Xmlenc::Algorithms::AESGCM

Inherits:
Object
  • Object
show all
Defined in:
lib/xmlenc/algorithms/aes_gcm.rb

Constant Summary collapse

AUTH_TAG_LEN =
16

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ AESGCM

Returns a new instance of AESGCM.



12
13
14
# File 'lib/xmlenc/algorithms/aes_gcm.rb', line 12

def initialize(size)
  @size = size
end

Class Method Details

.[](size) ⇒ Object



7
8
9
# File 'lib/xmlenc/algorithms/aes_gcm.rb', line 7

def [](size)
  new(size)
end

Instance Method Details

#decrypt(cipher_value, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/xmlenc/algorithms/aes_gcm.rb', line 23

def decrypt(cipher_value, options = {})
  cipher.decrypt
  cipher.padding  = 0
  cipher.key      = @key
  cipher.iv       = cipher_value[0...iv_len]
  cipher.auth_tag = cipher_value[-AUTH_TAG_LEN..-1]
  cipher.update(cipher_value[iv_len..-(AUTH_TAG_LEN + 1)]) << cipher.final
end

#encrypt(data, options = {}) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/xmlenc/algorithms/aes_gcm.rb', line 32

def encrypt(data, options = {})
  cipher.encrypt
  cipher.key       = @key
  cipher.iv        = iv
  cipher.auth_data = ''
  iv << (cipher.update(data) << cipher.final) << cipher.auth_tag
end

#keyObject



40
41
42
# File 'lib/xmlenc/algorithms/aes_gcm.rb', line 40

def key
  @key
end

#setup(key = nil) ⇒ Object



16
17
18
19
20
21
# File 'lib/xmlenc/algorithms/aes_gcm.rb', line 16

def setup(key = nil)
  @cipher= nil
  @iv    = nil
  @key   = key || cipher.random_key
  self
end