In this gist I'll show you how to encrypt messages using Ruby built-in and external libraries. Whenever possible I'll show you how to define in the most simplest way the encryption and the decryption methods.
- Openssl
def self.encrypt(input_word)
key = 'secret'
cipher = OpenSSL::Cipher::Cipher.new('DES-EDE3-CBC').encrypt
cipher.key = Digest::SHA1.hexdigest key
cipher.update(input_word) + cipher.final
end
def self.decrypt(input_word)
key = 'secret'
cipher = OpenSSL::Cipher::Cipher.new('DES-EDE3-CBC').decrypt
cipher.key = Digest::SHA1.hexdigest key
cipher.update(input_word) + cipher.final
end
- Message Encryptor
def encrypt(input_word)
crypt = ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base)
crypt.encrypt_and_sign(input_word)
end
def decrypt(input_word)
crypt = ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base)
crypt.decrypt_and_verify(input_word)
end
- Base64
- Encryptor https://github.com/attr-encrypted/encryptor
- Symmetric-encryption https://github.com/rocketjob/symmetric-encryption
- Gibberish https://github.com/mdp/gibberish