class RbNaCl::Boxes::Sealed
a message cannot be correlated with the identity of its sender.
cannot decrypt its own message later. And without additional data,
Without knowing the secret key used for a given message, the sender
is destroyed right after the encryption process.
A message is encrypted using an ephemeral key pair, whose secret part
verify the identity of the sender.
While the recipient can verify the integrity of the message, it cannot
Only the recipient can decrypt these messages, using its private key.
given its public key.
Sealed boxes are designed to anonymously send messages to a recipient
def self.from_private_key(private_key)
-
(RbNaCl::SealedBox)
- The new Box, ready to use
Raises:
-
(RbNaCl::LengthError)
- on invalid keys
Parameters:
-
private_key
(String, RbNaCl::PrivateKey
) -- The private key to decrypt with
def self.from_private_key(private_key) new(nil, private_key) end
def self.from_public_key(public_key)
-
(RbNaCl::SealedBox)
- The new Box, ready to use
Raises:
-
(RbNaCl::LengthError)
- on invalid keys
Parameters:
-
public_key
(String, RbNaCl::PublicKey
) -- The public key to encrypt to
def self.from_public_key(public_key) new(public_key, nil) end
def box(message)
-
(String)
- The ciphertext (BINARY encoded)
Raises:
-
(RbNaCl::CryptoError)
- If the encrytion fails.
Parameters:
-
message
(String
) -- The message to be encrypted.
def box(message) # No padding needed. msg = message # 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
def initialize(public_key, private_key = nil)
-
(RbNaCl::SealedBox)
- The new Box, ready to use
Raises:
-
(RbNaCl::LengthError)
- on invalid keys
Parameters:
-
private_key
(String, RbNaCl::PrivateKey
) -- The private key to decrypt with -
public_key
(String, RbNaCl::PublicKey
) -- The public key to encrypt to
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
def open(ciphertext)
-
(String)
- The decrypted message (BINARY encoded)
Raises:
-
(RbNaCl::CryptoError)
- If the ciphertext cannot be authenticated. -
(RbNaCl::CryptoError)
- If no private key is available.
Parameters:
-
ciphertext
(String
) -- The message to be decrypted.
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 message = Util.zeros(ct.bytesize - SEALBYTES) success = self.class.box_seal_open(message, ct, ct.bytesize, @public_key.to_s, @private_key.to_s) raise CryptoError, "Decryption failed. Ciphertext failed verification." unless success message end
def primitive
-
(Symbol)
- The primitive used
def primitive self.class.primitive end