class Nova::Starbound::Encryptors::RbNaCl

Provides encryption using RbNaCl.

Public Class Methods

available?() click to toggle source

(see Encryptor.available?)

# File lib/nova/starbound/encryptors/rbnacl.rb, line 12
def self.available?
  @_available ||= begin
    require 'rbnacl'
    true
  rescue LoadError
    false
  end
end

Public Instance Methods

decrypt(packet) click to toggle source

(see Encryptor#decrypt)

# File lib/nova/starbound/encryptors/rbnacl.rb, line 33
def decrypt(packet)
  packet = packet.clone
  box = Crypto::Box.new(options[:public_key], options[:private_key])
  packet.body = box.decrypt(packet[:nonce], packet[:body])

  packet
rescue Crypto::CryptoError => e
  raise EncryptorError, e
end
encrypt(packet) click to toggle source

(see Encryptor#encrypt)

# File lib/nova/starbound/encryptors/rbnacl.rb, line 22
def encrypt(packet)
  packet = packet.clone
  packet[:nonce] = Crypto::Random.random_bytes(24)
  box = Crypto::Box.new(options[:public_key], options[:private_key])
  enc = box.encrypt(packet[:nonce], packet[:body])

  packet.body = enc
  packet
end
other_public_key=(public_key) click to toggle source

Sets the other public key to the given value.

@return [void]

# File lib/nova/starbound/encryptors/rbnacl.rb, line 60
def other_public_key=(public_key)
  options[:public_key] = Crypto::PublicKey.new(public_key)
end
private_key!() click to toggle source

Generates a private key.

@return [void]

# File lib/nova/starbound/encryptors/rbnacl.rb, line 46
def private_key!
  options[:private_key] = Crypto::PrivateKey.generate
end
public_key() click to toggle source

Returns the public key for this remote.

@return [String]

# File lib/nova/starbound/encryptors/rbnacl.rb, line 53
def public_key
  options[:private_key].public_key.to_bytes
end