module Legion::Crypt::Cipher

Public Instance Methods

decrypt(message, iv) click to toggle source
# File lib/legion/crypt/cipher.rb, line 17
def decrypt(message, iv)
  until cs.is_a?(String) || Legion::Settings[:client][:shutting_down]
    Legion::Logging.debug('sleeping Legion::Crypt.decrypt due to CS not being set')
    sleep(0.5)
  end

  decipher = OpenSSL::Cipher.new('aes-256-cbc')
  decipher.decrypt
  decipher.key = cs
  decipher.iv = Base64.decode64(iv)
  message = Base64.decode64(message)
  decipher.update(message) + decipher.final
end
decrypt_from_keypair(message:, **_opts) click to toggle source
# File lib/legion/crypt/cipher.rb, line 37
def decrypt_from_keypair(message:, **_opts)
  private_key.private_decrypt(Base64.decode64(message))
end
encrypt(message) click to toggle source
# File lib/legion/crypt/cipher.rb, line 9
def encrypt(message)
  cipher = OpenSSL::Cipher.new('aes-256-cbc')
  cipher.encrypt
  cipher.key = cs
  iv = cipher.random_iv
  { enciphered_message: Base64.encode64(cipher.update(message) + cipher.final), iv: Base64.encode64(iv) }
end
encrypt_from_keypair(message:, pub_key: public_key) click to toggle source
# File lib/legion/crypt/cipher.rb, line 31
def encrypt_from_keypair(message:, pub_key: public_key)
  rsa_public_key = OpenSSL::PKey::RSA.new(pub_key)

  Base64.encode64(rsa_public_key.public_encrypt(message))
end
private_key() click to toggle source
# File lib/legion/crypt/cipher.rb, line 45
def private_key
  @private_key ||= if Legion::Settings[:crypt][:read_private_key] && File.exist?('./legionio.key')
                     OpenSSL::PKey::RSA.new File.read './legionio.key'
                   else
                     OpenSSL::PKey::RSA.new 2048
                   end
end
public_key() click to toggle source
# File lib/legion/crypt/cipher.rb, line 41
def public_key
  @public_key ||= private_key.public_key.to_s
end