Module: Rmega::Crypto::AesCbc

Included in:
Rmega::Crypto
Defined in:
lib/rmega/crypto/aes_cbc.rb

Instance Method Summary collapse

Instance Method Details

#aes_cbc_cipherObject



4
5
6
# File 'lib/rmega/crypto/aes_cbc.rb', line 4

def aes_cbc_cipher
  OpenSSL::Cipher::AES.new(128, :CBC)
end

#aes_cbc_decrypt(key, data) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rmega/crypto/aes_cbc.rb', line 16

def aes_cbc_decrypt(key, data)
  cipher = aes_cbc_cipher
  cipher.decrypt
  cipher.padding = 0
  cipher.key = key
  return cipher.update(data) + cipher.final
end

#aes_cbc_encrypt(key, data) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/rmega/crypto/aes_cbc.rb', line 8

def aes_cbc_encrypt(key, data)
  cipher = aes_cbc_cipher
  cipher.encrypt
  cipher.padding = 0
  cipher.key = key
  return cipher.update(data) + cipher.final
end

#aes_cbc_mac(key, data, iv) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rmega/crypto/aes_cbc.rb', line 24

def aes_cbc_mac(key, data, iv)
  cipher = aes_cbc_cipher
  cipher.encrypt
  cipher.padding = 0
  cipher.iv = iv if iv
  cipher.key = key

  # n = 0
  # mac = nil

  # loop do
  #   block = data[n..n+15]
  #   break if !block or block.empty?
  #   block << "\x0"*(16-block.size) if block.size < 16
  #   n += 16
  #   mac = cipher.update(block)
  # end

  # return mac

  block = data + "\x0" * ((16 - data.bytesize % 16) % 16)
  return cipher.update(block)[-16..-1]
end