44 |
44 |
raise "Cannot read #{options[:private_key]}"
|
45 |
45 |
end
|
46 |
46 |
end
|
|
47 |
@private_key_rsa = OpenSSL::PKey::RSA.new(@private_key) unless @private_key.nil?
|
47 |
48 |
|
48 |
49 |
if options[:public_key]
|
49 |
50 |
@public_key = options[:public_key]
|
50 |
51 |
elsif @private_key != nil
|
51 |
52 |
# Init ssh keys using private key. public key is extracted in a
|
52 |
53 |
# format compatible with openssl. The public key does not contain
|
53 |
|
# "---- BEGIN/END RSA PUBLIC KEY ----" and is in a single line
|
54 |
|
key = OpenSSL::PKey::RSA.new(@private_key)
|
55 |
|
|
56 |
|
@public_key = key.public_key.to_pem.split("\n")
|
57 |
|
@public_key = @public_key.reject {|l| l.match(/RSA PUBLIC KEY/) }.join('')
|
|
54 |
# "---- BEGIN/END PUBLIC KEY ----" and is in a single line
|
|
55 |
@public_key = @private_key_rsa.public_key.to_pem.split("\n")
|
|
56 |
@public_key = @public_key.reject {|l| l.match(/PUBLIC KEY/) }.join('')
|
58 |
57 |
end
|
|
58 |
@public_key_rsa = OpenSSL::PKey::RSA.new(Base64::decode64(@public_key)) unless @public_key.nil?
|
59 |
59 |
|
60 |
60 |
if @private_key.nil? && @public_key.nil?
|
61 |
61 |
raise "You have to define at least one of the keys"
|
... | ... | |
127 |
127 |
# Encrypts data with the private key of the user and returns
|
128 |
128 |
# base 64 encoded output in a single line
|
129 |
129 |
def encrypt(data)
|
130 |
|
rsa=OpenSSL::PKey::RSA.new(@private_key)
|
131 |
|
Base64::encode64(rsa.private_encrypt(data)).gsub!(/\n/, '').strip
|
|
130 |
Base64::encode64(@private_key_rsa.private_encrypt(data)).gsub!(/\n/, '').strip
|
132 |
131 |
end
|
133 |
132 |
|
134 |
133 |
# Decrypts base 64 encoded data with pub_key (public key)
|
135 |
134 |
def decrypt(data)
|
136 |
|
rsa=OpenSSL::PKey::RSA.new(Base64::decode64(@public_key))
|
137 |
|
rsa.public_decrypt(Base64::decode64(data))
|
|
135 |
@public_key_rsa.public_decrypt(Base64::decode64(data))
|
138 |
136 |
end
|
139 |
137 |
end
|
140 |
|
-
|