class Sodium::SecretBox

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.cr

Constant 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

Class Method Summary

Instance Method Summary

Instance methods inherited from module Sodium::Wipe

close close, finalize finalize

Constructor Detail

def self.new(key : Crypto::Secret) #

Use an existing Crypto::Secret


[View source]
def self.new(bytes : Bytes, erase = false) #

Copy bytes to a new SecureBuffer

Optionally erases bytes after copying if erase is set.

DEPRECATED Use .copy_from or .move_from


[View source]
def self.new #

Generate a new random key held in a SecureBuffer

DEPRECATED Use .random


[View source]

Class Method Detail

def self.copy_from(key : Bytes) #

Copy key to a new SecureBuffer


[View source]
def self.move_from(key : Bytes) #

Copy key to a new SecureBuffer

Erases key after copying


[View source]
def self.random #

Generate a new random key held in a SecureBuffer


[View source]

Instance Method Detail

def decrypt(src, dst : Bytes | Nil = nil, *, nonce : Nonce) : Bytes #

Returns decrypted message.

Optionally supply a destination buffer.


[View source]
def decrypt_string(src, *, nonce : Nonce) : String #

Returns decrypted message as a String.


[View source]
def encrypt(src, dst : Bytes | Nil = nil, *, nonce : Nonce | Nil = nil) #

Encrypts data and returns {ciphertext, nonce}

Optionally supply a destination buffer.


[View source]
def key : Crypto::Secret #

Encryption key


[View source]
def to_slice : Bytes #

DEPRECATED Use key.readonly or key.readwrite


[View source]