class Sodium::SecretBox
- Sodium::SecretBox
- Sodium::Key
- Reference
- Object
Overview
https://libsodium.gitbook.io/doc/secret-key_cryptography
WARNING Only use this class for compatibility with older applications already using SecretBox.
Use Sodium::Cipher::Aead::XChaCha20Poly1305Ietf
for new applications.
box = Sodium::SecretBox.new
message = "foobar"
encrypted, nonce = box.encrypt message
# On the other side.
box = Sodium::SecretBox.new key
message = key.decrypt encrypted, nonce: nonce
Defined in:
sodium/secret_box.crConstant Summary
-
KEY_SIZE =
LibSodium.crypto_secretbox_keybytes.to_i
-
MAC_SIZE =
LibSodium.crypto_secretbox_macbytes.to_i
-
NONCE_SIZE =
LibSodium.crypto_secretbox_noncebytes.to_i
Constructors
-
.new(key : Crypto::Secret)
Use an existing
Crypto::Secret
-
.new(bytes : Bytes, erase = false)
Copy bytes to a new
SecureBuffer
DEPRECATED Use .copy_from or .move_from
-
.new
Generate a new random key held in a
SecureBuffer
DEPRECATED Use .random
Class Method Summary
-
.copy_from(key : Bytes)
Copy key to a new
SecureBuffer
-
.move_from(key : Bytes)
Copy key to a new
SecureBuffer
-
.random
Generate a new random key held in a
SecureBuffer
Instance Method Summary
-
#decrypt(src, dst : Bytes | Nil = nil, *, nonce : Nonce) : Bytes
Returns decrypted message.
-
#decrypt_string(src, *, nonce : Nonce) : String
Returns decrypted message as a
String
. -
#encrypt(src, dst : Bytes | Nil = nil, *, nonce : Nonce | Nil = nil)
Encrypts data and returns {ciphertext, nonce}
-
#key : Crypto::Secret
Encryption key
-
#to_slice : Bytes
DEPRECATED Use
key.readonly
orkey.readwrite
Constructor Detail
Copy bytes to a new SecureBuffer
Optionally erases bytes after copying if erase is set.
DEPRECATED Use .copy_from or .move_from
Class Method Detail
Instance Method Detail
Returns decrypted message.
Optionally supply a destination buffer.
Returns decrypted message as a String
.
Encrypts data and returns {ciphertext, nonce}
Optionally supply a destination buffer.