class Voynich::AES

Constants

AUTH_TAG_BITS
CIPHER_MODE
DEFAULT_SERIALIZER

Public Class Methods

new(secret, adata, serializer: DEFAULT_SERIALIZER) click to toggle source
# File lib/voynich/aes.rb, line 10
def initialize(secret, adata, serializer: DEFAULT_SERIALIZER)
  @secret = secret
  @auth_data = adata
  @serializer = serializer
end

Public Instance Methods

decrypt(content, iv:, tag:) click to toggle source
# File lib/voynich/aes.rb, line 32
def decrypt(content, iv:, tag:)
  cipher = OpenSSL::Cipher.new(CIPHER_MODE)
  cipher.decrypt
  cipher.key = @secret
  cipher.iv = Base64.decode64(iv)
  cipher.auth_tag = Base64.decode64(tag)
  cipher.auth_data = @auth_data
  decrypted_data = cipher.update(Base64.decode64(content)) + cipher.final
  deserialize(decrypted_data)
end
deserialize(data) click to toggle source
# File lib/voynich/aes.rb, line 47
def deserialize(data)
  @serializer.load(data)
end
encrypt(plaintext) click to toggle source
# File lib/voynich/aes.rb, line 16
def encrypt(plaintext)
  cipher = OpenSSL::Cipher.new(CIPHER_MODE)
  cipher.encrypt
  cipher.key = @secret
  iv = cipher.random_iv
  cipher.auth_data = @auth_data
  encrypted_data = cipher.update(serialize(plaintext)) + cipher.final
  tag = cipher.auth_tag(AUTH_TAG_BITS / 8)
  {
    content: Base64.strict_encode64(encrypted_data),
    tag:     Base64.strict_encode64(tag),
    iv:      Base64.strict_encode64(iv),
    auth_data: @auth_data
  }
end
serialize(data) click to toggle source
# File lib/voynich/aes.rb, line 43
def serialize(data)
  @serializer.dump(data)
end