class RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey

RbNaCl::Box private key. Keep it safe

This class generates and stores NaCL private keys, as well as providing a reference to the public key associated with this private key, if that's provided.

Note that the documentation for NaCl refers to this as a secret key, but in this library its a private key, to avoid confusing the issue with the SecretBox, which does symmetric encryption.

Constants

BYTES

The size of the key, in bytes

Public Class Methods

generate() click to toggle source

Generates a new keypair

@raise [RbNaCl::CryptoError] if key generation fails, due to insufficient randomness.

@return [RbNaCl::PrivateKey] A new private key, with the associated public key also set.

# File lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb, line 53
def self.generate
  pk = Util.zeros(Boxes::Curve25519XSalsa20Poly1305::PUBLICKEYBYTES)
  sk = Util.zeros(Boxes::Curve25519XSalsa20Poly1305::PRIVATEKEYBYTES)
  box_curve25519xsalsa20poly1305_keypair(pk, sk) || raise(CryptoError, "Failed to generate a key pair")
  new(sk)
end
new(private_key) click to toggle source

Initializes a new PrivateKey for key operations.

Takes the (optionally encoded) private key bytes. This class can then be used for various key operations, including deriving the corresponding PublicKey

@param private_key [String] The private key

@raise [TypeError] If the key is nil @raise [RbNaCl::LengthError] If the key is not valid after decoding.

@return A new PrivateKey

# File lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb, line 44
def initialize(private_key)
  @private_key = Util.check_string(private_key, BYTES, "Private key")
end

Public Instance Methods

primitive() click to toggle source

The crypto primitive this PrivateKey is to be used for.

@return [Symbol] The primitive

# File lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb, line 77
def primitive
  self.class.primitive
end
public_key() click to toggle source

the public key associated with this private key

@return [PublicKey] the key

# File lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb, line 70
def public_key
  @public_key ||= PublicKey.new GroupElements::Curve25519.base.mult(to_bytes)
end
to_bytes() click to toggle source

The raw bytes of the key

@return [String] the raw bytes.

# File lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb, line 63
def to_bytes
  @private_key
end