Created
September 9, 2010 16:19
-
-
Save trevmex/572089 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Extend String to include an encryption method | |
# | |
# For this to work, you must place this in the lib directory of your Rails project. | |
# You will also need three files in your config directory: | |
# * public.pem: Your public key file | |
# * private.pem: Your private key file | |
# * passphrase.txt: Your passphrase | |
class String | |
require 'openssl' | |
require 'base64' | |
def encrypt | |
public_key_file = File.join(File.dirname(__FILE__), '..', 'config', 'public.pem') | |
public_key = OpenSSL::PKey::RSA.new(File.read(public_key_file)) | |
Base64.encode64(public_key.public_encrypt(self)) | |
end | |
def decrypt | |
private_key_file = File.join(File.dirname(__FILE__), '..', 'config', 'private.pem') | |
passphrase_file = File.join(File.dirname(__FILE__), '..', 'config', 'passphrase.txt') | |
private_key = OpenSSL::PKey::RSA.new(File.read(private_key_file), File.read(passphrase_file).rstrip) | |
private_key.private_decrypt(Base64.decode64(self)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment