public
Description: Simple stripped down shoes twitter client
Homepage: http://the-shoebox.org/apps/105
Clone URL: git://github.com/dscape/rudolph.git
rudolph / src / rudolph / crypt.rb
100644 37 lines (32 sloc) 1.099 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'openssl'
require 'Base64'
 
class Rudolph
  class Crypt
    def initialize data_path
      @data_path = data_path
      @private = get_key 'id_rsa'
      @public = get_key 'id_rsa.pub'
    end
 
    def encrypt_string message
      Base64::encode64(@public.public_encrypt(message)).rstrip
    end
 
    def decrypt_string message
      @private.private_decrypt Base64::decode64(message)
    end
 
    def self.generate_keys data_path
      rsa_path = File.join(data_path, 'rsa')
      privkey = File.join(rsa_path, 'id_rsa')
      pubkey = File.join(rsa_path, 'id_rsa.pub')
      unless File.exists?(privkey) || File.exists?(pubkey)
        keypair = OpenSSL::PKey::RSA.generate(1024)
        Dir.mkdir(rsa_path) unless File.exist?(rsa_path)
        File.open(privkey, 'w') { |f| f.write keypair.to_pem } unless File.exists? privkey
        File.open(pubkey, 'w') { |f| f.write keypair.public_key.to_pem } unless File.exists? pubkey
      end
    end
 
    private
    def get_key filename
      OpenSSL::PKey::RSA.new File.read(File.join(@data_path, 'rsa', filename))
    end
  end
end