Class: RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey

Inherits:
Object
  • Object
show all
Extended by:
Sodium
Includes:
KeyComparator, Serializable
Defined in:
lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb

Overview

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.

Constant Summary collapse

BYTES =

The size of the key, in bytes

Boxes::Curve25519XSalsa20Poly1305::PRIVATEKEYBYTES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sodium

sodium_constant, sodium_function, sodium_function_with_return_code, sodium_primitive, sodium_type

Methods included from Serializable

#inspect, #to_s, #to_str

Methods included from KeyComparator

#<=>, #==

Constructor Details

#initialize(private_key) ⇒ Object

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

Parameters:

  • private_key (String)

    The private key

Raises:

  • (TypeError)

    If the key is nil

  • (RbNaCl::LengthError)

    If the key is not valid after decoding.



44
45
46
# 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

Class Method Details

.generateRbNaCl::PrivateKey

Generates a new keypair

Returns:

Raises:



53
54
55
56
57
58
# 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

Instance Method Details

#primitiveSymbol

The crypto primitive this PrivateKey is to be used for.

Returns:

  • (Symbol)

    The primitive



77
78
79
# File 'lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb', line 77

def primitive
  self.class.primitive
end

#public_keyPublicKey

the public key associated with this private key

Returns:



70
71
72
# 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_bytesString

The raw bytes of the key

Returns:

  • (String)

    the raw bytes.



63
64
65
# File 'lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb', line 63

def to_bytes
  @private_key
end