Module: Crypto

Defined in:
lib/appswarm/crypt/crypto.rb

Overview

Defined Under Namespace

Classes: Key

Class Method Summary collapse

Class Method Details

.create_keys(priv = "rsa_key", pub = "#{priv}.pub", bits = 1024) ⇒ Object



7
8
9
10
11
12
# File 'lib/appswarm/crypt/crypto.rb', line 7

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

.doHmac(secret, payload) ⇒ Object



28
29
30
31
32
# File 'lib/appswarm/crypt/crypto.rb', line 28

def self.doHmac(secret, payload)
  key = Base64.decode64(secret)
  hashmac = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new(key), key, payload)
  Base64.encode64(hashmac).chomp
end

.doSha1(text) ⇒ Object



34
35
36
# File 'lib/appswarm/crypt/crypto.rb', line 34

def self.doSha1(text)
  Base64.encode64(OpenSSL::Digest::SHA1.digest(text)).chomp
end

.generate_secret_with_opensslObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/appswarm/crypt/crypto.rb', line 14

def self.generate_secret_with_openssl
  if !File.exist?("/dev/urandom")
     # OpenSSL transparently seeds the random number generator with
     # data from /dev/urandom. On platforms where that is not
     # available, such as Windows, we have to provide OpenSSL with
     # our own seed. Unfortunately there's no way to provide a
     # secure seed without OS support, so we'll have to do with
     # rand() and Time.now.usec().
     OpenSSL::Random.seed(rand(0).to_s + Time.now.usec.to_s)
  end
  data = OpenSSL::BN.rand(2048, -1, false).to_s
  return Base64.encode64(OpenSSL::Digest::SHA1.digest(data)).chomp
end