Commercial Products
Sep '08
16

Simple Encryption (in Ruby)

posted by delano

I occasionally get questions on how we do authentication in Ruby so I thought I'd write this post so I could direct people here. A lot of the authentication stuff we do is based on this simple implementation of RSA key support. There were a couple minor syntax errors in their crypto-key.rb though so here's a fixed version:

require 'openssl'
require 'base64'    # Added 

module Crypto

def self.create_keys(priv = "rsa_key", pub = "#{priv}.pub", bits = 1024) private_key = OpenSSL::PKey::RSA.new(bits) File.open(priv, "w+") { |fp| fp << private_key.to_s } File.open(pub, "w+") { |fp| fp << private_key.public_key.to_s }
private_key end

class Key def initialize(data) @public = (data =~ /^-----BEGIN (RSA|DSA) PRIVATE KEY-----$/).nil? @key = OpenSSL::PKey::RSA.new(data) end

<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">from_file</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>    
  <span class="nb">self</span><span class="p">.</span><span class="nf">new</span> <span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span> <span class="n">filename</span> <span class="p">)</span>
<span class="k">end</span>

<span class="k">def</span> <span class="nf">encrypt</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
  <span class="no">Base64</span><span class="p">.</span><span class="nf">encode64</span><span class="p">(</span><span class="vi">@key</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="s2">"</span><span class="si">#{</span><span class="n">key_type</span><span class="si">}</span><span class="s2">_encrypt"</span><span class="p">,</span> <span class="n">text</span><span class="p">))</span>
<span class="k">end</span>

<span class="k">def</span> <span class="nf">decrypt</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
  <span class="vi">@key</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="s2">"</span><span class="si">#{</span><span class="n">key_type</span><span class="si">}</span><span class="s2">_decrypt"</span><span class="p">,</span> <span class="no">Base64</span><span class="p">.</span><span class="nf">decode64</span><span class="p">(</span><span class="n">text</span><span class="p">))</span>
<span class="k">end</span>

def private?() !@public; end # Added () and ;

def public?() @public; end # Added () and ;

<span class="k">def</span> <span class="nf">key_type</span>
  <span class="vi">@public</span> <span class="p">?</span> <span class="ss">:public</span> <span class="p">:</span> <span class="ss">:private</span>
<span class="k">end</span>

end end

I'm Delano Mandelbaum, the founder of Solutious Inc. I've worked for companies large and small and now I'm putting everything I've learned into building great tools. I recently launched a monitoring service called Stella.

You can also find me on:

-       Delano (@solutious.com)

Solutious is a software company based in Montréal. We build testing and development tools that are both powerful and pleasant to use. All of our software is on GitHub.

This is our blog about performance, development, and getting stuff done.

-       Solutious