Class: Crypto::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/appswarm/crypt/crypto.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Key

Returns a new instance of Key.



39
40
41
42
43
# File 'lib/appswarm/crypt/crypto.rb', line 39

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

Class Method Details

.from_file(filename) ⇒ Object



45
46
47
# File 'lib/appswarm/crypt/crypto.rb', line 45

def self.from_file(filename)
self.new File.read( filename )
end

Instance Method Details

#decrypt(text) ⇒ Object



73
74
75
# File 'lib/appswarm/crypt/crypto.rb', line 73

def decrypt(text)
  @key.send("#{key_type}_decrypt", Base64.decode64(text))
end

#encrypt(text) ⇒ Object



49
50
51
# File 'lib/appswarm/crypt/crypto.rb', line 49

def encrypt(text)
  Base64.encode64(@key.send("#{key_type}_encrypt", text))
end

#key_typeObject



85
86
87
# File 'lib/appswarm/crypt/crypto.rb', line 85

def key_type
@public ? :public : :private
end

#private?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/appswarm/crypt/crypto.rb', line 77

def private?
!@public  
end

#public?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/appswarm/crypt/crypto.rb', line 81

def public?
@public   
end

#sign(text) ⇒ Object



53
54
55
# File 'lib/appswarm/crypt/crypto.rb', line 53

def sign(text)
  Base64.encode64(@key.send("sign", OpenSSL::Digest::SHA1.new, text))
end

#sign_other(text) ⇒ Object



57
58
59
60
# File 'lib/appswarm/crypt/crypto.rb', line 57

def sign_other(text)
  digest = OpenSSL::Digest::SHA1.new(text).to_s
  encrypt(digest)
end

#sign_other1(text) ⇒ Object



62
63
64
65
66
67
# File 'lib/appswarm/crypt/crypto.rb', line 62

def sign_other1(text)
  digest = OpenSSL::Digest::SHA1.new
  text.each_byte{ |c| digest.update(c.to_s) }
  digest = digest.hexdigest
  encrypt(digest)
end

#verify(sig, text) ⇒ Object



69
70
71
# File 'lib/appswarm/crypt/crypto.rb', line 69

def verify(sig, text)
  @key.send("verify", OpenSSL::Digest::SHA1.new, Base64.decode64(sig), text)
end