Class: RbNaCl::Boxes::Sealed
- Inherits:
-
Object
- Object
- RbNaCl::Boxes::Sealed
- Extended by:
- Sodium
- Defined in:
- lib/rbnacl/boxes/sealed.rb
Overview
Sealed boxes are designed to anonymously send messages to a recipient given its public key.
Only the recipient can decrypt these messages, using its private key. While the recipient can verify the integrity of the message, it cannot verify the identity of the sender.
A message is encrypted using an ephemeral key pair, whose secret part is destroyed right after the encryption process.
Without knowing the secret key used for a given message, the sender cannot decrypt its own message later. And without additional data, a message cannot be correlated with the identity of its sender.
Class Method Summary collapse
-
.from_private_key(private_key) ⇒ RbNaCl::SealedBox
Create a new Sealed Box for decrypting.
-
.from_public_key(public_key) ⇒ RbNaCl::SealedBox
Create a new Sealed Box for encrypting.
Instance Method Summary collapse
-
#box(message) ⇒ String
(also: #encrypt)
Encrypts a message.
-
#initialize(public_key, private_key = nil) ⇒ RbNaCl::SealedBox
constructor
WARNING: you should strongly prefer the from_private_key/from_public_key class methods.
-
#open(ciphertext) ⇒ String
(also: #decrypt)
Decrypts a ciphertext.
-
#primitive ⇒ Symbol
The crypto primitive for the box class.
Methods included from Sodium
sodium_constant, sodium_function, sodium_function_with_return_code, sodium_primitive, sodium_type
Constructor Details
#initialize(public_key, private_key = nil) ⇒ RbNaCl::SealedBox
WARNING: you should strongly prefer the from_private_key/from_public_key class methods.
Create a new Sealed Box
Sets up the Box for deriving the shared key and encrypting and decrypting messages.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rbnacl/boxes/sealed.rb', line 47 def initialize(public_key, private_key = nil) unless private_key.nil? @private_key = private_key.is_a?(PrivateKey) ? private_key : PrivateKey.new(private_key) raise IncorrectPrimitiveError unless @private_key.primitive == primitive public_key = @private_key.public_key if public_key.nil? end @public_key = public_key.is_a?(PublicKey) ? public_key : PublicKey.new(public_key) raise IncorrectPrimitiveError unless @public_key.primitive == primitive end |
Class Method Details
.from_private_key(private_key) ⇒ RbNaCl::SealedBox
Create a new Sealed Box for decrypting
Sets up the Box for decryption of new messages.
68 69 70 |
# File 'lib/rbnacl/boxes/sealed.rb', line 68 def self.from_private_key(private_key) new(nil, private_key) end |
.from_public_key(public_key) ⇒ RbNaCl::SealedBox
Create a new Sealed Box for encrypting
Sets up the Box for encryption of new messages.
81 82 83 |
# File 'lib/rbnacl/boxes/sealed.rb', line 81 def self.from_public_key(public_key) new(public_key, nil) end |
Instance Method Details
#box(message) ⇒ String Also known as: encrypt
Encrypts a message
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rbnacl/boxes/sealed.rb', line 92 def box() # No padding needed. msg = # variable name to match other RbNaCl code. # ensure enough space in result ct = Util.zeros(msg.bytesize + SEALBYTES) success = self.class.box_seal(ct, msg, msg.bytesize, @public_key.to_s) raise CryptoError, "Encryption failed" unless success ct end |
#open(ciphertext) ⇒ String Also known as: decrypt
Decrypts a ciphertext
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rbnacl/boxes/sealed.rb', line 113 def open(ciphertext) raise CryptoError, "Decryption failed. No private key." unless @private_key ct = ciphertext raise CryptoError, "Decryption failed. Ciphertext failed verification." if ct.bytesize < SEALBYTES = Util.zeros(ct.bytesize - SEALBYTES) success = self.class.box_seal_open(, ct, ct.bytesize, @public_key.to_s, @private_key.to_s) raise CryptoError, "Decryption failed. Ciphertext failed verification." unless success end |
#primitive ⇒ Symbol
The crypto primitive for the box class
131 132 133 |
# File 'lib/rbnacl/boxes/sealed.rb', line 131 def primitive self.class.primitive end |