class EasyCrypto::Crypto

Constants

AES_MODE
AUTH_TAG_LEN
IV_LEN
KEY_BITS

Public Class Methods

new(salt_length = DEFAULT_SALT_LENGTH) click to toggle source
# File lib/easycrypto/crypto.rb, line 12
def initialize(salt_length = DEFAULT_SALT_LENGTH)
  @salt_length = salt_length
end

Public Instance Methods

decrypt(password, ciphertext) click to toggle source
# File lib/easycrypto/crypto.rb, line 34
def decrypt(password, ciphertext)
  salt = get_salt_from_ciphertext(ciphertext)
  key = EasyCrypto::Key.generate_with_salt(password, salt)

  decrypt_with_key(key, ciphertext)
end
decrypt_with_key(key, ciphertext) click to toggle source
# File lib/easycrypto/crypto.rb, line 41
def decrypt_with_key(key, ciphertext)
  validate_key_type(key)

  raw_ciphertext = Base64.strict_decode64(ciphertext)

  iv = raw_ciphertext[key.salt.length, IV_LEN]
  encrypted = raw_ciphertext[(key.salt.length + IV_LEN)..-(AUTH_TAG_LEN + 1)]
  auth_tag = raw_ciphertext[-AUTH_TAG_LEN..-1]

  decipher = create_decipher(key, iv, auth_tag)

  decipher.update(encrypted) + decipher.final
end
encrypt(password, plaintext) click to toggle source
# File lib/easycrypto/crypto.rb, line 16
def encrypt(password, plaintext)
  key = EasyCrypto::Key.generate(password, @salt_length)

  encrypt_with_key(key, plaintext)
end
encrypt_with_key(key, plaintext) click to toggle source
# File lib/easycrypto/crypto.rb, line 22
def encrypt_with_key(key, plaintext)
  validate_key_type(key)
  validate_plaintext(plaintext)

  iv = OpenSSL::Random.random_bytes(Crypto::IV_LEN)
  cipher = create_cipher(key, iv)

  encrypted = cipher.update(plaintext) + cipher.final

  Base64.strict_encode64(key.salt + iv + encrypted + cipher.auth_tag)
end

Private Instance Methods

create_cipher(key, iv) click to toggle source
# File lib/easycrypto/crypto.rb, line 66
def create_cipher(key, iv)
  cipher = OpenSSL::Cipher::AES.new(Crypto::KEY_BITS, Crypto::AES_MODE).encrypt
  cipher.key = key.key
  cipher.iv = iv
  cipher
end
create_decipher(key, iv, auth_tag) click to toggle source
# File lib/easycrypto/crypto.rb, line 73
def create_decipher(key, iv, auth_tag)
  decipher = OpenSSL::Cipher::AES.new(Crypto::KEY_BITS, Crypto::AES_MODE).decrypt
  decipher.key = key.key
  decipher.iv = iv
  decipher.auth_tag = auth_tag
  decipher
end
get_salt_from_ciphertext(ciphertext) click to toggle source
# File lib/easycrypto/crypto.rb, line 81
def get_salt_from_ciphertext(ciphertext)
  raw_ciphertext = Base64.strict_decode64(ciphertext)
  raw_ciphertext[0, @salt_length]
end
validate_key_type(key) click to toggle source
# File lib/easycrypto/crypto.rb, line 57
def validate_key_type(key)
  raise TypeError, 'key must have Key type' unless key.is_a?(EasyCrypto::Key)
end
validate_plaintext(plaintext) click to toggle source
# File lib/easycrypto/crypto.rb, line 61
def validate_plaintext(plaintext)
  raise TypeError, 'Encryptable data must be a string' unless plaintext.is_a?(String)
  raise ArgumentError, 'Encryptable data must not be empty' if plaintext.empty?
end