ffi-libsodium 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sodium.rb +24 -0
- data/lib/sodium/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87db0771703338d8f1f2af7e6e0b4fa97bf16340
|
4
|
+
data.tar.gz: 5362403aea2592d3b09b56f2051ea93424635d57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c87f61dc009dd14d49f18d78a9f7f50ead03d972b0d08e87bc7f81d2c6b2e4cc66234e6402cf57f231607ada0ba0f01c0b4b0bb6a63cd213df4e21afdf16678
|
7
|
+
data.tar.gz: 33d8c50b5575fc148f215f26f1cd108fd4dbebfef6a7c4a613bd465e944652b1d0d746baefeaa7dadc3b7f54790bef7e84ef7c159455cc0358be7c8d3a824db7
|
data/lib/sodium.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
require 'ffi'
|
2
2
|
require_relative 'sodium/errors'
|
3
|
+
require_relative 'sodium/utils'
|
4
|
+
require_relative 'sodium/buffer'
|
3
5
|
|
4
6
|
module Sodium
|
5
7
|
extend FFI::Library
|
8
|
+
extend Utils
|
6
9
|
ffi_lib :libsodium
|
7
10
|
|
8
11
|
attach_function :init, :sodium_init, [], :int
|
@@ -15,6 +18,9 @@ module Sodium
|
|
15
18
|
attach_function :sodium_malloc, [:size_t], :pointer
|
16
19
|
attach_function :sodium_allocarray, [:size_t, :size_t], :pointer
|
17
20
|
|
21
|
+
attach_function :sodium_bin2hex, [:buffer_out, :size_t, :buffer_in, :size_t], :string
|
22
|
+
attach_function :sodium_hex2bin, [:buffer_out, :size_t, :buffer_in, :size_t, :string, :pointer, :pointer], :int
|
23
|
+
|
18
24
|
module_function
|
19
25
|
|
20
26
|
def mlock(addr, len)
|
@@ -32,4 +38,22 @@ module Sodium
|
|
32
38
|
def allocarray(count, size)
|
33
39
|
sodium_allocarray(count, size) || raise(NoMemoryError, "Failed to allocate memory size=#{count * size} bytes", caller)
|
34
40
|
end
|
41
|
+
|
42
|
+
def bin2hex(bin)
|
43
|
+
bin_len = get_size(bin)
|
44
|
+
hex = FFI::MemoryPointer.new(:char, bin_len * 2 + 1)
|
45
|
+
sodium_bin2hex(hex, hex.size, bin, bin_len)
|
46
|
+
end
|
47
|
+
|
48
|
+
def hex2bin(hex, ignore = nil)
|
49
|
+
if ignore
|
50
|
+
bin_maxlen = hex.tr(ignore, '').bytesize / 2
|
51
|
+
else
|
52
|
+
bin_maxlen = hex.bytesize / 2
|
53
|
+
end
|
54
|
+
|
55
|
+
bin = Sodium::Buffer.new(:uchar, bin_maxlen)
|
56
|
+
sodium_hex2bin(bin, bin_maxlen, hex, hex.bytesize, ignore, nil, nil)
|
57
|
+
bin
|
58
|
+
end
|
35
59
|
end
|
data/lib/sodium/version.rb
CHANGED