PageRenderTime 25ms CodeModel.GetById 19ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/jruby/2.1/gems/rbnacl-3.0.1/lib/rbnacl/sodium.rb

https://github.com/delowong/logstash
Ruby | 47 lines | 38 code | 7 blank | 2 comment | 4 complexity | 5b879a499bc572d61366a1ce75c6f08f MD5 | raw file
 1# encoding: binary
 2require 'ffi'
 3
 4module RbNaCl
 5  # Provides helpers for defining the libsodium bindings
 6  module Sodium
 7    def self.extended(klass)
 8      klass.extend FFI::Library
 9      if defined?(RBNACL_LIBSODIUM_GEM_LIB_PATH)
10        klass.ffi_lib RBNACL_LIBSODIUM_GEM_LIB_PATH
11      else
12        klass.ffi_lib 'sodium'
13      end
14    end
15
16
17    def sodium_type(type = nil)
18      return @type if type.nil?
19      @type = type
20    end
21
22    def sodium_primitive(primitive = nil)
23      return @primitive if primitive.nil?
24      @primitive = primitive
25    end
26
27    def primitive
28      sodium_primitive
29    end
30
31    def sodium_constant(constant, name=constant)
32      fn = "crypto_#{sodium_type}_#{sodium_primitive}_#{constant.to_s.downcase}"
33      attach_function fn, [], :ulong_long
34      self.const_set(name, self.public_send(fn))
35    end
36
37    def sodium_function(name, function, arguments)
38      self.module_eval <<-eos, __FILE__, __LINE__ + 1
39      attach_function #{function.inspect}, #{arguments.inspect}, :int
40      def self.#{name}(*args)
41        ret = #{function}(*args)
42        ret == 0
43      end
44      eos
45    end
46  end
47end