Sha256: 97d36b71143a1c647e72c9b2b6c3c111878241fdb2000ee374cdead77cfb0e5e

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

# encoding: binary
require 'ffi'

module RbNaCl
  # Provides helpers for defining the libsodium bindings
  module Sodium
    def self.extended(klass)
      klass.extend FFI::Library
      if defined?(RBNACL_LIBSODIUM_GEM_LIB_PATH)
        klass.ffi_lib RBNACL_LIBSODIUM_GEM_LIB_PATH
      else
        klass.ffi_lib 'sodium'
      end
    end


    def sodium_type(type = nil)
      return @type if type.nil?
      @type = type
    end

    def sodium_primitive(primitive = nil)
      return @primitive if primitive.nil?
      @primitive = primitive
    end

    def primitive
      sodium_primitive
    end

    def sodium_constant(constant, name=constant)
      fn = "crypto_#{sodium_type}_#{sodium_primitive}_#{constant.to_s.downcase}"
      attach_function fn, [], :ulong_long
      self.const_set(name, self.public_send(fn))
    end

    def sodium_function(name, function, arguments)
      self.module_eval <<-eos, __FILE__, __LINE__ + 1
      attach_function #{function.inspect}, #{arguments.inspect}, :int
      def self.#{name}(*args)
        ret = #{function}(*args)
        ret == 0
      end
      eos
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rbnacl-3.1.2 lib/rbnacl/sodium.rb
rbnacl-3.1.1 lib/rbnacl/sodium.rb
rbnacl-3.1.0 lib/rbnacl/sodium.rb
rbnacl-3.0.1 lib/rbnacl/sodium.rb