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_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
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
.generate ⇒ RbNaCl::PrivateKey
Generates a new keypair
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
#primitive ⇒ Symbol
The crypto primitive this PrivateKey is to be used for.
75 76 77 |
# File 'lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb', line 75 def primitive self.class.primitive end |
#public_key ⇒ PublicKey
the public key associated with this private key
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_bytes ⇒ String
The raw bytes of the key
61 62 63 |
# File 'lib/rbnacl/boxes/curve25519xsalsa20poly1305/private_key.rb', line 61 def to_bytes @private_key end |