module Crypto::SecretBox
Constants
- KEYBYTES
- MACBYTES
- NONCEBYTES
- PRIMITIVE
Public Instance Methods
detached(message, nonce, key)
click to toggle source
# File lib/crypto/secret_box.rb, line 110 def detached(message, nonce, key) message_len = get_size(message) check_length(nonce, NONCEBYTES, :Nonce) check_length(key, KEYBYTES, :SecretKey) ciphertext = zeros(message_len) mac = zeros(MACBYTES) key.readonly if key.is_a?(Sodium::SecretBuffer) crypto_secretbox_detached(ciphertext, mac, message, message_len, nonce, key) [ciphertext, mac] ensure key.noaccess if key.is_a?(Sodium::SecretBuffer) end
detached!(message, nonce, key)
click to toggle source
# File lib/crypto/secret_box.rb, line 146 def detached!(message, nonce, key) check_length(nonce, NONCEBYTES, :Nonce) check_length(key, KEYBYTES, :SecretKey) mac = zeros(MACBYTES) key.readonly if key.is_a?(Sodium::SecretBuffer) crypto_secretbox_detached(message, mac, message, get_size(message), nonce, key) [message, mac] ensure key.noaccess if key.is_a?(Sodium::SecretBuffer) end
nonce()
click to toggle source
# File lib/crypto/secret_box.rb, line 32 def nonce RandomBytes.buf(NONCEBYTES) end
open(ciphertext, nonce, key, encoding = nil)
click to toggle source
# File lib/crypto/secret_box.rb, line 50 def open(ciphertext, nonce, key, encoding = nil) ciphertext_len = get_size(ciphertext) check_length(nonce, NONCEBYTES, :Nonce) check_length(key, KEYBYTES, :SecretKey) decrypted = zeros(ciphertext_len - MACBYTES) key.readonly if key.is_a?(Sodium::SecretBuffer) if crypto_secretbox_open_easy(decrypted, ciphertext, ciphertext_len, nonce, key) == -1 raise Sodium::CryptoError, "Message forged", caller end if encoding decrypted.force_encoding(encoding) end decrypted ensure key.noaccess if key.is_a?(Sodium::SecretBuffer) end
open!(data, nonce, key, encoding = nil)
click to toggle source
# File lib/crypto/secret_box.rb, line 85 def open!(data, nonce, key, encoding = nil) ciphertext = String(data) if (message_len = (ciphertext_len = ciphertext.bytesize) - MACBYTES) < 0 fail Sodium::LengthError, "Ciphertext is too short", caller end check_length(nonce, NONCEBYTES, :Nonce) check_length(key, KEYBYTES, :SecretKey) key.readonly if key.is_a?(Sodium::SecretBuffer) if crypto_secretbox_open_easy(ciphertext, ciphertext, ciphertext_len, nonce, key) == -1 raise Sodium::CryptoError, "Message forged", caller end ciphertext.slice!(message_len..-1) if encoding ciphertext.force_encoding(encoding) end ciphertext ensure key.noaccess if key.is_a?(Sodium::SecretBuffer) end
open_detached(ciphertext, mac, nonce, key, encoding = nil)
click to toggle source
# File lib/crypto/secret_box.rb, line 125 def open_detached(ciphertext, mac, nonce, key, encoding = nil) ciphertext_len = get_size(ciphertext) check_length(mac, MACBYTES, :Mac) check_length(nonce, NONCEBYTES, :Nonce) check_length(key, KEYBYTES, :SecretKey) message = zeros(ciphertext_len) key.readonly if key.is_a?(Sodium::SecretBuffer) if crypto_secretbox_open_detached(message, ciphertext, mac, ciphertext_len, 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
open_detached!(ciphertext, mac, nonce, key, encoding = nil)
click to toggle source
# File lib/crypto/secret_box.rb, line 159 def open_detached!(ciphertext, mac, nonce, key, encoding = nil) check_length(mac, MACBYTES, :Mac) check_length(nonce, NONCEBYTES, :Nonce) check_length(key, KEYBYTES, :SecretKey) key.readonly if key.is_a?(Sodium::SecretBuffer) if crypto_secretbox_open_detached(ciphertext, ciphertext, mac, get_size(ciphertext), nonce, key) == -1 raise Sodium::CryptoError, "Message forged", caller end if encoding && ciphertext.respond_to?(:force_encoding) ciphertext.force_encoding(encoding) end ciphertext ensure key.noaccess if key.is_a?(Sodium::SecretBuffer) end
secretbox(message, nonce, key)
click to toggle source
# File lib/crypto/secret_box.rb, line 36 def secretbox(message, nonce, key) message_len = get_size(message) check_length(nonce, NONCEBYTES, :Nonce) check_length(key, KEYBYTES, :SecretKey) ciphertext = zeros(message_len + MACBYTES) key.readonly if key.is_a?(Sodium::SecretBuffer) crypto_secretbox_easy(ciphertext, message, message_len, nonce, key) ciphertext ensure key.noaccess if key.is_a?(Sodium::SecretBuffer) end
secretbox!(data, nonce, key)
click to toggle source
# File lib/crypto/secret_box.rb, line 70 def secretbox!(data, nonce, key) message = String(data) check_length(nonce, NONCEBYTES, :Nonce) check_length(key, KEYBYTES, :SecretKey) message_len = message.bytesize message << zeros(MACBYTES) key.readonly if key.is_a?(Sodium::SecretBuffer) crypto_secretbox_easy(message, message, message_len, nonce, key) message.force_encoding(Encoding::ASCII_8BIT) ensure key.noaccess if key.is_a?(Sodium::SecretBuffer) end