module Crypto::AEAD::Chacha20Poly1305

Constants

ABYTES
KEYBYTES
NPUBBYTES
PRIMITIVE

Public Class Methods

crypto_aead_chacha20poly1305_primitive() click to toggle source
# File lib/crypto/aead/chacha20_poly1305.rb, line 18
def crypto_aead_chacha20poly1305_primitive
  PRIMITIVE
end
Also aliased as: primitive

Public Instance Methods

decrypt(ciphertext, additional_data, nonce, key, encoding = nil) click to toggle source
# File lib/crypto/aead/chacha20_poly1305.rb, line 56
def decrypt(ciphertext, additional_data, nonce, key, encoding = nil)
  if (message_len = (ciphertext_len = get_size(ciphertext)) - ABYTES) < 0
    fail Sodium::LengthError, "Ciphertext is too short", caller
  end

  check_length(nonce, NPUBBYTES, :Nonce)
  check_length(key, KEYBYTES, :SecretKey)

  message = zeros(message_len)
  key.readonly if key.is_a?(Sodium::SecretBuffer)
  if crypto_aead_chacha20poly1305_decrypt(message, nil, nil, ciphertext, ciphertext_len, additional_data, get_size(additional_data), nonce, key) == -1
    raise Sodium::CryptoError, "Message forged", caller
  end

  if encoding
    message.force_encoding(encoding)
  end

  message
ensure
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
end
encrypt(message, additional_data, nonce, key) click to toggle source
# File lib/crypto/aead/chacha20_poly1305.rb, line 42
def encrypt(message, additional_data, nonce, key)
  message_len = get_size(message)
  check_length(nonce, NPUBBYTES, :Nonce)
  check_length(key, KEYBYTES, :SecretKey)

  ciphertext = zeros(message_len + ABYTES)
  key.readonly if key.is_a?(Sodium::SecretBuffer)
  crypto_aead_chacha20poly1305_encrypt(ciphertext, nil, message, message_len, additional_data, get_size(additional_data), nil, nonce, key)

  ciphertext
ensure
  key.noaccess if key.is_a?(Sodium::SecretBuffer)
end
nonce() click to toggle source
# File lib/crypto/aead/chacha20_poly1305.rb, line 38
def nonce
  RandomBytes.buf(NPUBBYTES)
end