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