module RbNaCl::Sodium

Provides helpers for defining the libsodium bindings

Public Class Methods

extended(klass) click to toggle source
# File lib/rbnacl/sodium.rb, line 9
def self.extended(klass)
  klass.extend FFI::Library
  klass.ffi_lib ["sodium", "libsodium.so.18", "libsodium.so.23"]
end

Public Instance Methods

primitive() click to toggle source
# File lib/rbnacl/sodium.rb, line 28
def primitive
  sodium_primitive
end
sodium_constant(constant, name: constant, fallback: nil) click to toggle source
# File lib/rbnacl/sodium.rb, line 32
def sodium_constant(constant, name: constant, fallback: nil)
  fn_name_components = ["crypto", sodium_type, sodium_primitive, constant.to_s.downcase]
  fn_name = fn_name_components.compact.join("_")

  begin
    attach_function fn_name, [], :size_t
  rescue FFI::NotFoundError
    raise if fallback.nil?

    define_singleton_method fn_name, -> { fallback }
  end

  const_set(name, public_send(fn_name))
end
sodium_function(name, function, arguments) click to toggle source
# File lib/rbnacl/sodium.rb, line 47
    def sodium_function(name, function, arguments)
      module_eval <<-RUBY, __FILE__, __LINE__ + 1
      attach_function #{function.inspect}, #{arguments.inspect}, :int
      def self.#{name}(*args)
        ret = #{function}(*args)
        ret == 0
      end
      RUBY
    end
sodium_function_with_return_code(name, function, arguments) click to toggle source
# File lib/rbnacl/sodium.rb, line 57
    def sodium_function_with_return_code(name, function, arguments)
      module_eval <<-RUBY, __FILE__, __LINE__ + 1
      attach_function #{function.inspect}, #{arguments.inspect}, :int
      def self.#{name}(*args)
        #{function}(*args)
      end
      RUBY
    end
sodium_primitive(primitive = nil) click to toggle source
# File lib/rbnacl/sodium.rb, line 20
def sodium_primitive(primitive = nil)
  if primitive.nil?
    @primitive if defined?(@primitive)
  else
    @primitive = primitive
  end
end
sodium_type(type = nil) click to toggle source
# File lib/rbnacl/sodium.rb, line 14
def sodium_type(type = nil)
  return @type if type.nil?

  @type = type
end