GnuPG Ruby on Rails Plugin

January 1st, 2007

I have another alpha-level plugin for you folks. This one has also saved me arse on many an occasion. Keep in mind, encryption/decryption setups on servers vary like the color of leaves in autumn. This particular plugin works on the paradigm of having a web-accessible public key that encrypts user data, and a temporarily available secret key that is used for decryption. Its entirely up to you to ensure that things are kept secure, but, as a result, the plugin is tiny!


./script/plugin install svn://ahgsoftware.com/gnupg/trunk

Here’s a quick example, assuming you have a pub key—imported

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# We're using the same workdir for pub and sec, they could differ!
workdir = File.join(RAILS_ROOT,"tmp","gnupg")
passphrase = "uglydonkeys"

# This is a darwin-ports on Mac OS X version of gpg
gnupg = GnuPG.new :binary=>"/opt/local/bin/gpg",
   :workdir=>workdir,
   :homedir_pub=>workdir,
   :homedir_sec=>workdir,
   :recipient=>"your uid"

plain_message = "no more mustachoed tyrants, muhwahwahwa, buhwahwahwa"
encrypted_message = gnupg.encrypt(plain_message)

# Load ascii sec key, from wherever you might have it
gnupg.load_key File.read("sec_key.asc")
decrypted_message = gnupg.decrypt(encrypted_message, passphrase)
gnupg.drop_key

puts plain_message
puts encrypted_message
puts decrypted_message

Leave a Reply

 

Michael Cerna Chicago-based Rails Developer and Avid Musician. More ...

Search

Categories

  • Home (15)
  • Rails Plugins (5)
  • Pages (9)
  • Archives

    Tags

    BlogRoll