Class: RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey
- Inherits:
-
Object
- Object
- RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey
- 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
-
.generate ⇒ RbNaCl::PrivateKey
Generates a new keypair.
Instance Method Summary collapse
-
#initialize(private_key) ⇒ Object
constructor
Initializes a new PrivateKey for key operations.
-
#primitive ⇒ Symbol
The crypto primitive this PrivateKey is to be used for.
-
#public_key ⇒ PublicKey
the public key associated with this private key.
-
#to_bytes ⇒ String
The raw bytes of the key.
Methods included from Sodium
sodium_constant, sodium_function, sodium_function_with_return_code, sodium_primitive, sodium_type
Methods included from Serializable
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
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
.generate ⇒ RbNaCl::PrivateKey
Generates a new keypair
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
#primitive ⇒ Symbol
The crypto primitive this PrivateKey is to be used for.
77 78 79 |
# File 'lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb', line 77 def primitive self.class.primitive end |
#public_key ⇒ PublicKey
the public key associated with this private key
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_bytes ⇒ String
The raw bytes of the key
63 64 65 |
# File 'lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb', line 63 def to_bytes @private_key end |