Ruby Fleebie

Rediscovering the joy of programming

Encryption with Alphanumeric output

What if you want to generate reasonably short alphanumeric user activation codes without having to store anything in a DB (so in this case generating random user codes won’t do) ? Why would someone need this? Think about an application where you want to print activation cards and sell them to your customers. The customer then login to your website, put the activation code that is printed on its card… and bingo : they are activated.

So, how to do this? With encryption, of course.

The thing is that most encryption algorithms will generate ciphers with non-friendly characters, like : Òèç`{&[ùỲgǛ... so bad for readability. It seems complicated at first but finally one solution turns out to be fairly simple :

Step #1

sudo gem install crypt

Step #2

blowfish = Crypt::Blowfish.new(SOME_KEY)
crypted_but_readable = blowfish.encrypt_block("12345678").unpack("H*")

Step #3

  crypted_block = params[:some_crypted_but_readable_block].to_s
  if crypted_block.length == 16
    blowfish = Crypt::Blowfish.new(THE_SAME_KEY)
    decrypted_block = blowfish.decrypt_block(crypted_block.to_a.pack(“H*”))
    #do something with decrypted block
  end

The Blowfish algorithm takes 8-bytes blocks only... so you have to take that into account when you generate your keys. The unpack("H*) and pack("H*") parts are the most important. It simply encodes/decodes the block in hexadecimal. So, here we are, you have a readable & decryptable 16-chars cipher that looks like : 9048bb8f56eddd47. You can even display the codes into chunks of 4 characters and it gives you the following friendly code : 9048-bb8f-56ed-dd47

Tip: Associate to each code a SHA-1 hashed password (which is the result of activation code + some salt) and you have a pretty safe account activation procedure that doesn't pollute your database.

Last deployment date with Rails and Capistrano

When you are developing an app or a website for your client, it is pretty common to setup a sandbox environment where your client can test and see the application while it is still in development. One thing I find useful is to display the last deployment date on the home page.

Here is a quick and easy way to automate the process :

Step #1 (we can have lots of fun)
I will consider that you use capistrano for deploying your app. The only thing you have to do in your capistrano recipe is to “touch” a dummy file to update its modification date.

namespace :deploy do
  desc 'Restart My App'
  task :restart, :roles => :app do
    run "touch #{current_path}/last_deploy"
  end
end

Step #2 (there’s so much we can do)
Now in application_controller, add a before_filter like this

before_filter :last_deploy

def last_deploy
  @last_deploy = File.new("last_deploy").atime rescue Time.now
end

Step #3 (it’s just you and me)
Display the date in the layout or in the view of your choice

<%= "Last deployment : #{@last_deploy}"%>

Edit : Oh, dear readers, I just want to let you know that RubyFleebie is now on Twitter.

5 Plugins Or Gems You Could Not Live Without

Hello everyone,

For the past few months this blog has been pretty much dead. I could give you tons of excuses as for why it has been so but that’s all they would be, mere excuses! And I assure you that you would not find any of them very convincing.

I received several comments recently telling me that you liked my blog and asking me when I plan writing again and believe me, it always goes straight to my heart. For some time now I am in the expectation that most of you will give up on me and remove this blog from your list forever but for some reason… you don’t!

Since I am a bit rusty at writing blog posts, I chose the easy way : a numbered list of Rails Gems/Plugins that I like. Hmmm, I know… it’s soooooo 2007! But, interesting nonetheless. If you don’t mind, I’d like that you share in the comment section the plugins that you like the most as well. Perhaps this post could become a reference to discover great plugins. Oh btw, from now on I will use the terms plugins and gems like they were the same thing.

  1. Paperclip by Thoughtbot

    Very easy to install & configure. I use it for thumbnails generation and it works wonder.

  2. locale_routing by Perfectline

    If you are developing a multilingual app/website (with I18n), don’t miss this simple plugin. It auto inserts the locale in the url without messing with your routes.

  3. thinking-sphinx by freelancing-gods

    A fast and reliable free-text search solution for your rails apps, using the Sphinx daemon. The only thing that is really unfortunate with sphinx is that is doesn’t index new database records live out of the box. Instead you have to build the index manually or setup a cronjob at a given interval. The workaround is to use delta indexes with the delayed-job plugin but it forces you to have a rake task always running on your server. I had so much problems with the rake task getting killed for no apparent reason that I decided that live indexing was not so important. Instead I have a cronjob running every X hours that rebuild the index from scratch

  4. will_paginate by mislav

    I guess we all know and use this one but I had to put it in my list anyway. It is such a great and easy to use pagination plugin.

  5. acts_as_state_machine by rubyist
  6. This one I use all the time. Everytime an activerecord object can be in more than a single “state” (enabled, hidden, locked, whatever)… think about using it!

That’s it! Now, it’s your turn : what is your own top 5 list?