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

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_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.



42
43
44
# File 'lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb', line 42

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:



51
52
53
54
55
56
# File 'lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb', line 51

def self.generate
  pk = Util.zeros(Boxes::Curve25519XSalsa20Poly1305::PUBLICKEYBYTES)
  sk = Util.zeros(Boxes::Curve25519XSalsa20Poly1305::PRIVATEKEYBYTES)
  self.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



75
76
77
# File 'lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb', line 75

def primitive
  self.class.primitive
end

#public_keyPublicKey

the public key associated with this private key

Returns:



68
69
70
# File 'lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb', line 68

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.



61
62
63
# File 'lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb', line 61

def to_bytes
  @private_key
end