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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/sodium.rb +24 -0
  3. data/lib/sodium/version.rb +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c81230a67614e1616d64c30e469947b9131a72ac
4
- data.tar.gz: 228b1201f0b00119fba63b04ccdd7a2f88fcc77e
3
+ metadata.gz: 87db0771703338d8f1f2af7e6e0b4fa97bf16340
4
+ data.tar.gz: 5362403aea2592d3b09b56f2051ea93424635d57
5
5
  SHA512:
6
- metadata.gz: d36f6b18d630b803b43cd094647ac5c332f599ab668c5df340ac02237742cc91770b644b2030db00e3611d975b10afd3ec10e9752ecac92dc781e9e560d8c5e6
7
- data.tar.gz: 0b54875b9bc202e53714a8be00078e847843c4e255902e82aacf3f464d55cd6256a36559de82a1bf1d088b51a073277a6175d1788f23f314381004b3c985d30d
6
+ metadata.gz: 8c87f61dc009dd14d49f18d78a9f7f50ead03d972b0d08e87bc7f81d2c6b2e4cc66234e6402cf57f231607ada0ba0f01c0b4b0bb6a63cd213df4e21afdf16678
7
+ data.tar.gz: 33d8c50b5575fc148f215f26f1cd108fd4dbebfef6a7c4a613bd465e944652b1d0d746baefeaa7dadc3b7f54790bef7e84ef7c159455cc0358be7c8d3a824db7
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Sodium
2
- VERSION = Gem::Version.new('0.2.3')
2
+ VERSION = Gem::Version.new('0.2.4')
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ffi-libsodium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hendrik Beskow