Creates an encrypted, base64 encoded url containing signup form parameters. Used for database-less email verification.
class UsersController < ApplicationController | |
# Creates an encrypted, base64 encoded url. http://localhost/confirm?_=hBRCGVqie5PetQhjiagq9F6kmi7luVxpcpEYMWaxrtSHIPA3rF0Hufy6EgiH%0A%2BL3t9dcgV9es9Zywkl4F1lcMyA%3D%3D%0A | |
@to_encrypt = save_space(params[:user], {:firstname => :fn,:lastname => :ln,:email => :el,:username => :un,:password => :pd}) | |
encrypted_params = CGI::escape(Base64.encode64(encrypt(Marshal.dump(@to_encrypt), "secret"))) | |
render :text => "#{encrypted_params}" | |
def confirm | |
@decrypted = Marshal.load(decrypt(Base64.decode64(params[:_]), "secret")) | |
@data = unsave_space(@decrypted, {:firstname => :fn,:lastname => :ln,:email => :el,:username => :un,:password => :pd}) | |
render :text => "#{@data}" | |
end | |
private | |
def save_space(input,hash) | |
@output = Hash.new | |
hash.each {|k,v| @output[v] = input[k]} | |
@output | |
end | |
def unsave_space(input,hash) | |
output = Hash.new | |
hash.each {|k,v| output[k] = input[v]} | |
output | |
end | |
def aes(m,t,k) | |
(aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc').send(m)).key = Digest::SHA256.digest(k) | |
aes.update(t) << aes.final | |
end | |
def encrypt(text, key) | |
aes(:encrypt, text, key) | |
end | |
def decrypt(text, key) | |
aes(:decrypt, text, key) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment