module Crypto::Box
Constants
- BEFORENMBYTES
- MACBYTES
- NONCEBYTES
- PRIMITIVE
- PUBLICKEYBYTES
- SECRETKEYBYTES
- SEEDBYTES
Public Instance Methods
beforenm(public_key, secret_key)
click to toggle source
# File lib/crypto/box.rb, line 91 def beforenm(public_key, secret_key) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) shared_secret = Sodium::SecretBuffer.new(BEFORENMBYTES) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) crypto_box_beforenm(shared_secret, public_key, secret_key) shared_secret.noaccess shared_secret ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end
box(message, nonce, public_key, secret_key)
click to toggle source
# File lib/crypto/box.rb, line 105 def box(message, nonce, public_key, secret_key) message_len = get_size(message) check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) ciphertext = zeros(message_len + MACBYTES) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) crypto_box_easy(ciphertext, message, message_len, nonce, public_key, secret_key) ciphertext ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end
box!(data, nonce, public_key, secret_key)
click to toggle source
# File lib/crypto/box.rb, line 141 def box!(data, nonce, public_key, secret_key) message = String(data) check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) message_len = message.bytesize message << zeros(MACBYTES) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) crypto_box_easy(message, message, message_len, nonce, public_key, secret_key) message ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end
detached(message, nonce, public_key, secret_key)
click to toggle source
# File lib/crypto/box.rb, line 183 def detached(message, nonce, public_key, secret_key) message_len = get_size(message) check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) ciphertext = zeros(message_len) mac = zeros(MACBYTES) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) crypto_box_detached(ciphertext, mac, message, message_len, nonce, public_key, secret_key) [ciphertext, mac] ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end
detached!(message, nonce, public_key, secret_key)
click to toggle source
# File lib/crypto/box.rb, line 220 def detached!(message, nonce, public_key, secret_key) check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) mac = zeros(MACBYTES) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) crypto_box_detached(message, mac, message, get_size(message), nonce, public_key, secret_key) [message, mac] ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end
keypair()
click to toggle source
# File lib/crypto/box.rb, line 47 def keypair public_key = zeros(PUBLICKEYBYTES) secret_key = zeros(SECRETKEYBYTES) crypto_box_keypair(public_key, secret_key) [public_key, secret_key] end
memory_locked_keypair()
click to toggle source
# File lib/crypto/box.rb, line 68 def memory_locked_keypair public_key = zeros(PUBLICKEYBYTES) secret_key = Sodium::SecretBuffer.new(SECRETKEYBYTES) crypto_box_keypair(public_key, secret_key) secret_key.noaccess [public_key, secret_key] end
memory_locked_seed_keypair(seed)
click to toggle source
# File lib/crypto/box.rb, line 77 def memory_locked_seed_keypair(seed) check_length(seed, SEEDBYTES, :Seed) public_key = zeros(PUBLICKEYBYTES) secret_key = Sodium::SecretBuffer.new(SECRETKEYBYTES) seed.readonly if seed.is_a?(Sodium::SecretBuffer) crypto_box_seed_keypair(public_key, secret_key, seed) secret_key.noaccess [public_key, secret_key] ensure seed.noaccess if seed.is_a?(Sodium::SecretBuffer) end
nonce()
click to toggle source
# File lib/crypto/box.rb, line 43 def nonce RandomBytes.buf(NONCEBYTES) end
open(ciphertext, nonce, public_key, secret_key, encoding = nil)
click to toggle source
# File lib/crypto/box.rb, line 120 def open(ciphertext, nonce, public_key, secret_key, encoding = nil) ciphertext_len = get_size(ciphertext) check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) message = zeros(ciphertext_len - MACBYTES) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) if crypto_box_open_easy(message, ciphertext, ciphertext_len, nonce, public_key, secret_key) == -1 raise Sodium::CryptoError, "Message forged", caller end if encoding message.force_encoding(encoding) end message ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end
open!(data, nonce, public_key, secret_key, encoding = nil)
click to toggle source
# File lib/crypto/box.rb, line 157 def open!(data, nonce, public_key, secret_key, encoding = nil) ciphertext = String(data) ciphertext_len = ciphertext.bytesize if (message_len = ciphertext_len - MACBYTES) < 0 fail Sodium::LengthError, "Ciphertext is too short", caller end check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) if crypto_box_open_easy(ciphertext, ciphertext, ciphertext_len, nonce, public_key, secret_key) == -1 raise Sodium::CryptoError, "Message forged", caller end ciphertext.slice!(message_len..-1) if encoding ciphertext.force_encoding(encoding) end ciphertext ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end
open_detached(ciphertext, mac, nonce, public_key, secret_key, encoding = nil)
click to toggle source
# File lib/crypto/box.rb, line 198 def open_detached(ciphertext, mac, nonce, public_key, secret_key, encoding = nil) ciphertext_len = get_size(ciphertext) check_length(mac, MACBYTES, :Mac) check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) message = zeros(ciphertext_len) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) if crypto_box_open_detached(message, ciphertext, mac, ciphertext_len, nonce, public_key, secret_key) == -1 raise Sodium::CryptoError, "Message forged", caller end if encoding message.force_encoding(encoding) end message ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end
open_detached!(ciphertext, mac, nonce, public_key, secret_key, encoding = nil)
click to toggle source
# File lib/crypto/box.rb, line 233 def open_detached!(ciphertext, mac, nonce, public_key, secret_key, encoding = nil) check_length(mac, MACBYTES, :Mac) check_length(nonce, NONCEBYTES, :Nonce) check_length(public_key, PUBLICKEYBYTES, :PublicKey) check_length(secret_key, SECRETKEYBYTES, :SecretKey) secret_key.readonly if secret_key.is_a?(Sodium::SecretBuffer) if crypto_box_open_detached(ciphertext, ciphertext, mac, get_size(ciphertext), nonce, public_key, secret_key) == -1 raise Sodium::CryptoError, "Message forged", caller end if encoding && ciphertext.respond_to?(:force_encoding) ciphertext.force_encoding(encoding) end ciphertext ensure secret_key.noaccess if secret_key.is_a?(Sodium::SecretBuffer) end
seed_keypair(seed)
click to toggle source
# File lib/crypto/box.rb, line 55 def seed_keypair(seed) check_length(seed, SEEDBYTES, :Seed) public_key = zeros(PUBLICKEYBYTES) secret_key = zeros(SECRETKEYBYTES) seed.readonly if seed.is_a?(Sodium::SecretBuffer) crypto_box_seed_keypair(public_key, secret_key, seed) [public_key, secret_key] ensure seed.noaccess if seed.is_a?(Sodium::SecretBuffer) end