class Sodium::SecretBox

Overview

https://libsodium.gitbook.io/doc/secret-key_cryptography

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

Instance Method Summary

Instance methods inherited from class Sodium::Key

to_slice : Bytes to_slice

Instance methods inherited from module Sodium::Wipe

close close, finalize finalize

Constructor Detail

def self.new(key : SecureBuffer) #

Use an existing SecureBuffer.


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

Copy bytes to a new SecureBuffer

Optionally erases bytes after copying if erase is set.


[View source]
def self.new #

Generate a new random key held in a SecureBuffer.


[View source]

Instance Method Detail

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

Returns decrypted message.

Optionally supply a destination buffer.


[View source]
def decrypt_string(src, dst : Bytes? = nil, *, nonce : Nonce) : String #

Returns decrypted message as a String.

Optionally supply a destination buffer.


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

Encrypts data and returns {ciphertext, nonce}

Optionally supply a destination buffer.


[View source]
def key : SecureBuffer #

Encryption key


[View source]
def to_slice : Bytes #

Returns key


[View source]