-
Notifications
You must be signed in to change notification settings - Fork 82
/
sodium.rb
71 lines (60 loc) · 1.78 KB
/
sodium.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# encoding: binary
# frozen_string_literal: true
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_type_primitive_constant(constant, name = constant)
fn = "crypto_#{sodium_type}_#{sodium_primitive}_#{constant.to_s.downcase}"
_generic_constant(fn, name)
end
def sodium_type_constant(constant, name = constant)
fn = "crypto_#{sodium_type}_#{constant.to_s.downcase}"
_generic_constant(fn, name)
end
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
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
# this alias exists to ensure we have a stable API
# remove for 6.0
alias sodium_constant sodium_type_primitive_constant
private
def _generic_constant(fn, name)
attach_function fn, [], :size_t
const_set(name, public_send(fn))
end
end
end