Module: Bixby::Agent::Crypto
- Included in:
- Handshake
- Defined in:
- lib/bixby-agent/agent/crypto.rb
Instance Method Summary collapse
-
#create_keypair ⇒ Object
create crypto keypair and save in config folder.
- #crypto_enabled? ⇒ Boolean
-
#decrypt_from_server(data) ⇒ String
Decrypt data that was encrypted with our public key.
-
#encrypt_for_server(data) ⇒ String
Encrypt data using the server’s public key.
- #have_server_key? ⇒ Boolean
- #keypair ⇒ Object
- #private_key ⇒ Object
- #private_key_file ⇒ Object
- #public_key ⇒ Object
- #server_key ⇒ Object
- #server_key_file ⇒ Object
Instance Method Details
#create_keypair ⇒ Object
create crypto keypair and save in config folder
11 12 13 14 15 |
# File 'lib/bixby-agent/agent/crypto.rb', line 11 def create_keypair init_config_dir() pair = OpenSSL::PKey::RSA.generate(2048) File.open(private_key_file, 'w') { |out| out.write(pair.to_s) } end |
#crypto_enabled? ⇒ Boolean
45 46 47 48 |
# File 'lib/bixby-agent/agent/crypto.rb', line 45 def crypto_enabled? b = ENV["BIXBY_NOCRYPTO"] !(b and %w{1 true yes}.include? b) end |
#decrypt_from_server(data) ⇒ String
Decrypt data that was encrypted with our public key
64 65 66 67 68 |
# File 'lib/bixby-agent/agent/crypto.rb', line 64 def decrypt_from_server(data) data = StringIO.new(data, 'rb') uuid = data.readline.strip # TODO throwaway the uuid for now Bixby::CryptoUtil.decrypt(data, keypair, server_key) end |
#encrypt_for_server(data) ⇒ String
Encrypt data using the server’s public key
55 56 57 |
# File 'lib/bixby-agent/agent/crypto.rb', line 55 def encrypt_for_server(data) Bixby::CryptoUtil.encrypt(data, self.uuid, server_key, keypair) end |
#have_server_key? ⇒ Boolean
37 38 39 |
# File 'lib/bixby-agent/agent/crypto.rb', line 37 def have_server_key? File.exists? server_key_file end |
#keypair ⇒ Object
21 22 23 |
# File 'lib/bixby-agent/agent/crypto.rb', line 21 def keypair @keypair ||= OpenSSL::PKey::RSA.new(File.read(private_key_file)) end |
#private_key ⇒ Object
29 30 31 |
# File 'lib/bixby-agent/agent/crypto.rb', line 29 def private_key keypair end |
#private_key_file ⇒ Object
17 18 19 |
# File 'lib/bixby-agent/agent/crypto.rb', line 17 def private_key_file File.join(self.config_dir, "id_rsa") end |
#public_key ⇒ Object
25 26 27 |
# File 'lib/bixby-agent/agent/crypto.rb', line 25 def public_key @public_key ||= keypair.public_key end |
#server_key ⇒ Object
41 42 43 |
# File 'lib/bixby-agent/agent/crypto.rb', line 41 def server_key @server_key ||= OpenSSL::PKey::RSA.new(File.read(server_key_file)) end |
#server_key_file ⇒ Object
33 34 35 |
# File 'lib/bixby-agent/agent/crypto.rb', line 33 def server_key_file File.join(self.config_dir, "server.pub") end |