ffi-hydrogen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +30 -0
  5. data/.travis.yml +10 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +72 -0
  9. data/Rakefile +46 -0
  10. data/bench/both.rb +86 -0
  11. data/bench/encode.rb +57 -0
  12. data/bench/encrypt.rb +80 -0
  13. data/bench/init.rb +5 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/ffi-hydrogen.gemspec +31 -0
  17. data/lib/ffi/hydrogen.rb +216 -0
  18. data/vendor/.clang-format +2 -0
  19. data/vendor/.gitignore +3 -0
  20. data/vendor/README.md +2 -0
  21. data/vendor/libhydrogen/.clang-format +95 -0
  22. data/vendor/libhydrogen/.gitignore +32 -0
  23. data/vendor/libhydrogen/.travis.yml +22 -0
  24. data/vendor/libhydrogen/LICENSE +18 -0
  25. data/vendor/libhydrogen/Makefile +61 -0
  26. data/vendor/libhydrogen/Makefile.arduino +51 -0
  27. data/vendor/libhydrogen/README.md +29 -0
  28. data/vendor/libhydrogen/hydrogen.c +18 -0
  29. data/vendor/libhydrogen/hydrogen.h +317 -0
  30. data/vendor/libhydrogen/impl/common.h +316 -0
  31. data/vendor/libhydrogen/impl/core.h +220 -0
  32. data/vendor/libhydrogen/impl/gimli-core/portable.h +39 -0
  33. data/vendor/libhydrogen/impl/gimli-core/sse2.h +97 -0
  34. data/vendor/libhydrogen/impl/gimli-core.h +25 -0
  35. data/vendor/libhydrogen/impl/hash.h +138 -0
  36. data/vendor/libhydrogen/impl/hydrogen_p.h +83 -0
  37. data/vendor/libhydrogen/impl/kdf.h +20 -0
  38. data/vendor/libhydrogen/impl/kx.h +441 -0
  39. data/vendor/libhydrogen/impl/pwhash.h +281 -0
  40. data/vendor/libhydrogen/impl/random.h +376 -0
  41. data/vendor/libhydrogen/impl/secretbox.h +236 -0
  42. data/vendor/libhydrogen/impl/sign.h +207 -0
  43. data/vendor/libhydrogen/impl/x25519.h +383 -0
  44. data/vendor/libhydrogen/library.properties +10 -0
  45. data/vendor/libhydrogen/logo.png +0 -0
  46. data/vendor/libhydrogen/tests/tests.c +431 -0
  47. data/vendor/main.c +140 -0
  48. data/vendor/stringencoders/.gitignore +25 -0
  49. data/vendor/stringencoders/.travis.yml +13 -0
  50. data/vendor/stringencoders/AUTHORS +1 -0
  51. data/vendor/stringencoders/COPYING +2 -0
  52. data/vendor/stringencoders/ChangeLog +170 -0
  53. data/vendor/stringencoders/Doxyfile +276 -0
  54. data/vendor/stringencoders/INSTALL +119 -0
  55. data/vendor/stringencoders/LICENSE +22 -0
  56. data/vendor/stringencoders/Makefile.am +3 -0
  57. data/vendor/stringencoders/NEWS +3 -0
  58. data/vendor/stringencoders/README +2 -0
  59. data/vendor/stringencoders/README.md +32 -0
  60. data/vendor/stringencoders/bootstrap.sh +3 -0
  61. data/vendor/stringencoders/configure-gcc-hardened.sh +16 -0
  62. data/vendor/stringencoders/configure.ac +44 -0
  63. data/vendor/stringencoders/doxy/footer.html +34 -0
  64. data/vendor/stringencoders/doxy/header.html +85 -0
  65. data/vendor/stringencoders/indent.sh +9 -0
  66. data/vendor/stringencoders/javascript/base64-speed.html +43 -0
  67. data/vendor/stringencoders/javascript/base64-test.html +209 -0
  68. data/vendor/stringencoders/javascript/base64.html +18 -0
  69. data/vendor/stringencoders/javascript/base64.js +176 -0
  70. data/vendor/stringencoders/javascript/qunit.css +119 -0
  71. data/vendor/stringencoders/javascript/qunit.js +1062 -0
  72. data/vendor/stringencoders/javascript/urlparse-test.html +367 -0
  73. data/vendor/stringencoders/javascript/urlparse.js +328 -0
  74. data/vendor/stringencoders/make-ci.sh +13 -0
  75. data/vendor/stringencoders/makerelease.sh +16 -0
  76. data/vendor/stringencoders/python/b85.py +176 -0
  77. data/vendor/stringencoders/src/Makefile.am +134 -0
  78. data/vendor/stringencoders/src/arraytoc.c +85 -0
  79. data/vendor/stringencoders/src/arraytoc.h +43 -0
  80. data/vendor/stringencoders/src/extern_c_begin.h +3 -0
  81. data/vendor/stringencoders/src/extern_c_end.h +3 -0
  82. data/vendor/stringencoders/src/html_named_entities_generator.py +203 -0
  83. data/vendor/stringencoders/src/modp_ascii.c +159 -0
  84. data/vendor/stringencoders/src/modp_ascii.h +162 -0
  85. data/vendor/stringencoders/src/modp_ascii_data.h +84 -0
  86. data/vendor/stringencoders/src/modp_ascii_gen.c +55 -0
  87. data/vendor/stringencoders/src/modp_b16.c +125 -0
  88. data/vendor/stringencoders/src/modp_b16.h +148 -0
  89. data/vendor/stringencoders/src/modp_b16_data.h +104 -0
  90. data/vendor/stringencoders/src/modp_b16_gen.c +65 -0
  91. data/vendor/stringencoders/src/modp_b2.c +69 -0
  92. data/vendor/stringencoders/src/modp_b2.h +130 -0
  93. data/vendor/stringencoders/src/modp_b2_data.h +44 -0
  94. data/vendor/stringencoders/src/modp_b2_gen.c +36 -0
  95. data/vendor/stringencoders/src/modp_b36.c +108 -0
  96. data/vendor/stringencoders/src/modp_b36.h +170 -0
  97. data/vendor/stringencoders/src/modp_b64.c +254 -0
  98. data/vendor/stringencoders/src/modp_b64.h +236 -0
  99. data/vendor/stringencoders/src/modp_b64_data.h +477 -0
  100. data/vendor/stringencoders/src/modp_b64_gen.c +168 -0
  101. data/vendor/stringencoders/src/modp_b64r.c +254 -0
  102. data/vendor/stringencoders/src/modp_b64r.h +242 -0
  103. data/vendor/stringencoders/src/modp_b64r_data.h +477 -0
  104. data/vendor/stringencoders/src/modp_b64w.c +254 -0
  105. data/vendor/stringencoders/src/modp_b64w.h +231 -0
  106. data/vendor/stringencoders/src/modp_b64w_data.h +477 -0
  107. data/vendor/stringencoders/src/modp_b85.c +109 -0
  108. data/vendor/stringencoders/src/modp_b85.h +171 -0
  109. data/vendor/stringencoders/src/modp_b85_data.h +36 -0
  110. data/vendor/stringencoders/src/modp_b85_gen.c +65 -0
  111. data/vendor/stringencoders/src/modp_bjavascript.c +65 -0
  112. data/vendor/stringencoders/src/modp_bjavascript.h +105 -0
  113. data/vendor/stringencoders/src/modp_bjavascript_data.h +84 -0
  114. data/vendor/stringencoders/src/modp_bjavascript_gen.c +58 -0
  115. data/vendor/stringencoders/src/modp_burl.c +228 -0
  116. data/vendor/stringencoders/src/modp_burl.h +259 -0
  117. data/vendor/stringencoders/src/modp_burl_data.h +136 -0
  118. data/vendor/stringencoders/src/modp_burl_gen.c +121 -0
  119. data/vendor/stringencoders/src/modp_html.c +128 -0
  120. data/vendor/stringencoders/src/modp_html.h +53 -0
  121. data/vendor/stringencoders/src/modp_html_named_entities.h +9910 -0
  122. data/vendor/stringencoders/src/modp_json.c +315 -0
  123. data/vendor/stringencoders/src/modp_json.h +103 -0
  124. data/vendor/stringencoders/src/modp_json_data.h +57 -0
  125. data/vendor/stringencoders/src/modp_json_gen.py +60 -0
  126. data/vendor/stringencoders/src/modp_mainpage.h +120 -0
  127. data/vendor/stringencoders/src/modp_numtoa.c +350 -0
  128. data/vendor/stringencoders/src/modp_numtoa.h +100 -0
  129. data/vendor/stringencoders/src/modp_qsiter.c +76 -0
  130. data/vendor/stringencoders/src/modp_qsiter.h +71 -0
  131. data/vendor/stringencoders/src/modp_stdint.h +43 -0
  132. data/vendor/stringencoders/src/modp_utf8.c +88 -0
  133. data/vendor/stringencoders/src/modp_utf8.h +38 -0
  134. data/vendor/stringencoders/src/modp_xml.c +311 -0
  135. data/vendor/stringencoders/src/modp_xml.h +166 -0
  136. data/vendor/stringencoders/src/stringencoders.pc +10 -0
  137. data/vendor/stringencoders/src/stringencoders.pc.in +10 -0
  138. data/vendor/stringencoders/test/Makefile.am +113 -0
  139. data/vendor/stringencoders/test/apr_base64.c +262 -0
  140. data/vendor/stringencoders/test/apr_base64.h +120 -0
  141. data/vendor/stringencoders/test/cxx_test.cc +482 -0
  142. data/vendor/stringencoders/test/minunit.h +82 -0
  143. data/vendor/stringencoders/test/modp_ascii_test.c +281 -0
  144. data/vendor/stringencoders/test/modp_b16_test.c +288 -0
  145. data/vendor/stringencoders/test/modp_b2_test.c +250 -0
  146. data/vendor/stringencoders/test/modp_b64_test.c +266 -0
  147. data/vendor/stringencoders/test/modp_b85_test.c +130 -0
  148. data/vendor/stringencoders/test/modp_bjavascript_test.c +137 -0
  149. data/vendor/stringencoders/test/modp_burl_test.c +423 -0
  150. data/vendor/stringencoders/test/modp_html_test.c +296 -0
  151. data/vendor/stringencoders/test/modp_json_test.c +336 -0
  152. data/vendor/stringencoders/test/modp_numtoa_test.c +545 -0
  153. data/vendor/stringencoders/test/modp_qsiter_test.c +280 -0
  154. data/vendor/stringencoders/test/modp_utf8_test.c +188 -0
  155. data/vendor/stringencoders/test/modp_xml_test.c +339 -0
  156. data/vendor/stringencoders/test/speedtest.c +241 -0
  157. data/vendor/stringencoders/test/speedtest_ascii.c +345 -0
  158. data/vendor/stringencoders/test/speedtest_msg.c +78 -0
  159. data/vendor/stringencoders/test/speedtest_numtoa.c +276 -0
  160. metadata +314 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fcf7222814e1b73a17b3ea735aea98e9b8ba25af66e140cf44930fe8c933d80d
4
+ data.tar.gz: 7beeae085e1a733aeefbfed6107bfc7c8649ebd37037dace6945ca264bd4185b
5
+ SHA512:
6
+ metadata.gz: d9ebfc742fb3a4e8065328f7e5262188f805d2a42d658d17346be91a389aebbcc3835ae67a96ff933faea709add7b82b5ea262e9d887b3969835dd085d6f7966
7
+ data.tar.gz: 4b37a7cd2c2b60c5a422bd5e72cee11f8cdeb2badefb2c0c724de35c525205cd78073a6d68062f38175fb7f71eef8d011647f33293120a4354298271813a0aeb
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .ruby-version
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,30 @@
1
+ # Styles are inherited from Mad Rubocop
2
+
3
+ inherit_gem:
4
+ mad_rubocop: .rubocop.yml
5
+
6
+ # Styles that are modified from the defaults
7
+ AllCops:
8
+ TargetRubyVersion: 2.3
9
+
10
+ Style/BlockDelimiters:
11
+ Description: >-
12
+ Avoid using {...} for multi-line blocks (multiline chaining is
13
+ always ugly).
14
+ Prefer {...} over do...end for single-line blocks.
15
+ StyleGuide: 'https://git.moneydesktop.com/dev/ruby-style-guide#single-line-blocks'
16
+ Exclude:
17
+ - 'spec/**/*'
18
+
19
+ Style/HashSyntax:
20
+ Description: >-
21
+ Prefer Ruby 1.8 hash syntax { :a => 1, :b => 2 }
22
+ over 1.9 syntax { a: 1, b: 2 }.
23
+ StyleGuide: 'https://git.moneydesktop.com/dev/ruby-style-guide#hash-literals'
24
+ EnforcedStyle: hash_rockets
25
+ Exclude:
26
+ - 'Gemfile'
27
+
28
+ Layout/SpaceAroundOperators:
29
+ Exclude:
30
+ - 'mx-aggregation_status.gemspec'
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+
5
+ rvm:
6
+ - 2.5.3
7
+ - 2.3.1
8
+
9
+ before_script:
10
+ - cc --version
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ffi-hydrogen.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Marcos Minond
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ [![Build Status](https://travis-ci.org/minond/ffi-hydrogen.svg?branch=master)](https://travis-ci.org/minond/ffi-hydrogen)
2
+
3
+ # FFI::Hydrogen
4
+
5
+ Combine [libhydrogen](https://github.com/jedisct1/libhydrogen) with a good
6
+ implementation of string encoding in C, Nick Galbreath's
7
+ [base64](https://github.com/client9/stringencoders) implementation in this
8
+ case, and the end result is hopefully a fast method of encrypting data in a way
9
+ that is URL safe.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem "ffi-hydrogen"
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install ffi-hydrogen
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ key = ::FFI::Hydrogen.hydro_secretbox_keygen
31
+ context = "examples"
32
+
33
+ message = "0123456789"
34
+ encrypted = ::FFI::Hydrogen.hydro_secretbox_encrypt(message, context, key)
35
+ encoded = ::FFI::Hydrogen.modp_b64_encode(encrypted)
36
+ decoded = ::FFI::Hydrogen.modp_b64_decode(encoded)
37
+ decrypted = ::FFI::Hydrogen.hydro_secretbox_decrypt(decoded, context, key)
38
+
39
+ puts "message: #{message}"
40
+ puts "encrypted: #{encrypted}"
41
+ puts "encoded: #{encoded}"
42
+ puts "decoded: #{decoded}"
43
+ puts "decrypted: #{decrypted}"
44
+ ```
45
+
46
+ ### `::FFI::Hydrogen::Secretbox`
47
+
48
+ ```ruby
49
+ ctx = "examples"
50
+ key = ::FFI::Hydrogen.hydro_secretbox_keygen
51
+ box = ::FFI::Hydrogen::Secretbox.new(ctx, key)
52
+
53
+ message = "0123456789"
54
+ boxed = box.encrypt_encode(message)
55
+ unboxed = box.decode_decrypt(boxed)
56
+
57
+ puts "message: #{message}"
58
+ puts "boxed: #{boxed}"
59
+ puts "unboxed: #{unboxed}"
60
+ ```
61
+
62
+ ## Development
63
+
64
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
65
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
66
+ prompt that will allow you to experiment.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To
69
+ release a new version, update the version number in `ffi-hydrogen.gemspec`, and
70
+ then run `bundle exec rake release`, which will create a git tag for the
71
+ version, push git commits and tags, and push the `.gem` file to
72
+ [rubygems.org](https://rubygems.org).
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require "bundler/gem_tasks"
2
+ require "ffi"
3
+ require "rspec/core/rake_task"
4
+ require "rubocop/rake_task"
5
+
6
+ def sys(cmd)
7
+ puts "#{cmd}"
8
+ ret = system(cmd)
9
+ raise "ERROR: '#{cmd}' failed" unless ret
10
+ ret
11
+ end
12
+
13
+ RSpec::Core::RakeTask.new(:spec)
14
+
15
+ desc "Run RuboCop"
16
+ task :rubocop do
17
+ RuboCop::RakeTask.new
18
+ end
19
+
20
+ desc "Compile shared library"
21
+ task :compile do
22
+ wflags = "-Wall -Wextra -Wmissing-prototypes -Wdiv-by-zero"\
23
+ " -Wbad-function-cast -Wcast-align -Wcast-qual -Wfloat-equal"\
24
+ " -Wmissing-declarations -Wnested-externs -Wno-unknown-pragmas"\
25
+ " -Wpointer-arith -Wredundant-decls -Wstrict-prototypes -Wswitch-enum"\
26
+ " -Wno-type-limits"
27
+
28
+ cflags = "-O3 -march=native -fPIC -fno-exceptions #{wflags}"
29
+
30
+ # main.o: main.c
31
+ # $(CC) -c $(CFLAGS) $< -o $@
32
+ sys("cc -c #{cflags} vendor/main.c -o vendor/main.o")
33
+
34
+ # main.dylib: main.o
35
+ # $(CC) $< -shared -o $@
36
+ sys("cc vendor/main.o -shared -o vendor/main.#{::FFI::Platform::LIBSUFFIX}")
37
+ end
38
+
39
+ desc "Run all benchmarks"
40
+ task :bench do
41
+ ruby "bench/both.rb"
42
+ ruby "bench/encode.rb"
43
+ ruby "bench/encrypt.rb"
44
+ end
45
+
46
+ task :default => [:compile, :rubocop, :spec]
data/bench/both.rb ADDED
@@ -0,0 +1,86 @@
1
+ require "base64"
2
+ require "benchmark/ips"
3
+ require "rbnacl"
4
+ require "urlcrypt"
5
+
6
+ require "ffi/hydrogen"
7
+
8
+ require_relative "./init.rb"
9
+
10
+ def suite(str)
11
+ puts "============================ Test string length: #{str.size} ============================"
12
+
13
+ label = "#{str.size}_char"
14
+
15
+ hydrogen_key = ::FFI::Hydrogen.hydro_secretbox_keygen
16
+ hydrogen_context = "benched1"
17
+ hydrogen_box = ::FFI::Hydrogen::Secretbox.new(hydrogen_context, hydrogen_key)
18
+ hydrogen_encrypted_encoded = hydrogen_box.encrypt_encode(str)
19
+
20
+ rbnacl_key = ::RbNaCl::Random.random_bytes(::RbNaCl::SecretBox.key_bytes)
21
+ rbnacl_box = ::RbNaCl::SimpleBox.from_secret_key(rbnacl_key)
22
+ rbnacl_encrypted_encoded = ::Base64.encode64(rbnacl_box.encrypt(str))
23
+
24
+ urlcrypt_key = "h34j4k3l25gh342o5jk46ghjk5nbjkl7nhljk34532j4k5b43hjk5g432jk6v4gjhk6f5j6gv5hj2k3g54hjk321g4hjk25g4hjk35gf4yu6f5jg5b43jk25h4jk325d"
25
+ ::URLcrypt.key = [urlcrypt_key].pack("H*")
26
+ urlcrypt_encrypted = ::URLcrypt.encrypt(str)
27
+
28
+ Benchmark.ips do |b|
29
+ b.time = 2
30
+ b.warmup = 1
31
+
32
+ b.report("boxed_encrypt_encode_#{label}") do |n|
33
+ i = 0
34
+ while i < n
35
+ hydrogen_box.encrypt_encode(str)
36
+ i += 1
37
+ end
38
+ end
39
+
40
+ b.report("boxed_decode_decrypt_#{label}") do |n|
41
+ i = 0
42
+ while i < n
43
+ hydrogen_box.decode_decrypt(hydrogen_encrypted_encoded)
44
+ i += 1
45
+ end
46
+ end
47
+
48
+ b.report("urlcrypt_encrypt_#{label}") do |n|
49
+ i = 0
50
+ while i < n
51
+ ::URLcrypt.encrypt(str)
52
+ i += 1
53
+ end
54
+ end
55
+
56
+ b.report("urlcrypt_decrypt_#{label}") do |n|
57
+ i = 0
58
+ while i < n
59
+ ::URLcrypt.decrypt(urlcrypt_encrypted)
60
+ i += 1
61
+ end
62
+ end
63
+
64
+ b.report("boxed_rbnacl_simplebox_encrypt_plus_base64_#{label}") do |n|
65
+ i = 0
66
+ while i < n
67
+ ::Base64.encode64(rbnacl_box.encrypt(str))
68
+ i += 1
69
+ end
70
+ end
71
+
72
+ b.report("boxed_rbnacl_simplebox_decrypt_plus_base64_#{label}") do |n|
73
+ i = 0
74
+ while i < n
75
+ rbnacl_box.decrypt(::Base64.decode64(rbnacl_encrypted_encoded))
76
+ i += 1
77
+ end
78
+ end
79
+
80
+ b.compare!
81
+ end
82
+ end
83
+
84
+ TEST_STRINGS.each do |str|
85
+ suite(str)
86
+ end
data/bench/encode.rb ADDED
@@ -0,0 +1,57 @@
1
+ require "base64"
2
+ require "benchmark/ips"
3
+
4
+ require "ffi/hydrogen"
5
+
6
+ require_relative "./init.rb"
7
+
8
+ def suite(str)
9
+ puts "============================ Test string length: #{str.size} ============================"
10
+
11
+ label = "#{str.size}_char"
12
+
13
+ encoded = ::FFI::Hydrogen.modp_b64_encode(str)
14
+
15
+ Benchmark.ips do |b|
16
+ b.time = 2
17
+ b.warmup = 1
18
+
19
+ b.report("modp_b64_encode_#{label}") do |n|
20
+ i = 0
21
+ while i < n
22
+ ::FFI::Hydrogen.modp_b64_encode(str)
23
+ i += 1
24
+ end
25
+ end
26
+
27
+ b.report("modp_b64_decode_#{label}") do |n|
28
+ i = 0
29
+ while i < n
30
+ ::FFI::Hydrogen.modp_b64_decode(encoded)
31
+ i += 1
32
+ end
33
+ end
34
+
35
+ b.report("base64_encode64_#{label}") do |n|
36
+ i = 0
37
+ while i < n
38
+ ::Base64.encode64(str)
39
+ i += 1
40
+ end
41
+ end
42
+
43
+ b.report("base64_decode64_#{label}") do |n|
44
+ i = 0
45
+ while i < n
46
+ ::Base64.decode64(str)
47
+ i += 1
48
+ end
49
+ end
50
+
51
+ b.compare!
52
+ end
53
+ end
54
+
55
+ TEST_STRINGS.each do |str|
56
+ suite(str)
57
+ end
data/bench/encrypt.rb ADDED
@@ -0,0 +1,80 @@
1
+ require "benchmark/ips"
2
+ require "rbnacl"
3
+
4
+ require "ffi/hydrogen"
5
+
6
+ require_relative "./init.rb"
7
+
8
+ def suite(str)
9
+ puts "============================ Test string length: #{str.size} ============================"
10
+
11
+ label = "#{str.size}_char"
12
+
13
+ hydrogen_key = ::FFI::Hydrogen.hydro_secretbox_keygen
14
+ hydrogen_context = "benched1"
15
+ hydrogen_box = ::FFI::Hydrogen::Secretbox.new(hydrogen_context, hydrogen_key)
16
+ hydrogen_encrypted = hydrogen_box.encrypt(str)
17
+
18
+ rbnacl_key = ::RbNaCl::Random.random_bytes(::RbNaCl::SecretBox.key_bytes)
19
+ rbnacl_box = ::RbNaCl::SimpleBox.from_secret_key(rbnacl_key)
20
+ rbnacl_encrypted = rbnacl_box.encrypt(str)
21
+
22
+ Benchmark.ips do |b|
23
+ b.time = 2
24
+ b.warmup = 1
25
+
26
+ b.report("hydro_secretbox_encrypt_#{label}") do |n|
27
+ i = 0
28
+ while i < n
29
+ ::FFI::Hydrogen.hydro_secretbox_encrypt(str, hydrogen_context, hydrogen_key)
30
+ i += 1
31
+ end
32
+ end
33
+
34
+ b.report("hydro_secretbox_decrypt_#{label}") do |n|
35
+ i = 0
36
+ while i < n
37
+ ::FFI::Hydrogen.hydro_secretbox_decrypt(hydrogen_encrypted, hydrogen_context, hydrogen_key)
38
+ i += 1
39
+ end
40
+ end
41
+
42
+ b.report("boxed_hydro_secretbox_encrypt_#{label}") do |n|
43
+ i = 0
44
+ while i < n
45
+ hydrogen_box.encrypt(str)
46
+ i += 1
47
+ end
48
+ end
49
+
50
+ b.report("boxed_hydro_secretbox_decrypt_#{label}") do |n|
51
+ i = 0
52
+ while i < n
53
+ hydrogen_box.decrypt(hydrogen_encrypted)
54
+ i += 1
55
+ end
56
+ end
57
+
58
+ b.report("boxed_rbnacl_simplebox_encrypt_#{label}") do |n|
59
+ i = 0
60
+ while i < n
61
+ rbnacl_box.encrypt(str)
62
+ i += 1
63
+ end
64
+ end
65
+
66
+ b.report("boxed_rbnacl_simplebox_decrypt_#{label}") do |n|
67
+ i = 0
68
+ while i < n
69
+ rbnacl_box.decrypt(rbnacl_encrypted)
70
+ i += 1
71
+ end
72
+ end
73
+
74
+ b.compare!
75
+ end
76
+ end
77
+
78
+ TEST_STRINGS.each do |str|
79
+ suite(str)
80
+ end
data/bench/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ TEST_STRINGS = [
2
+ "0123456789",
3
+ "<abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv>",
4
+ "<abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#^&*()_+>",
5
+ ]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ffi/hydrogen"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "ffi-hydrogen"
6
+ spec.version = "0.1.0"
7
+ spec.authors = ["Marcos Minond"]
8
+ spec.email = ["marcos.minond@mx.com"]
9
+
10
+ spec.summary = "libhydrogen + encoding = cryptographically sound, URL safe string = libhydrogen + encoding"
11
+ spec.homepage = "https://github.com/minond/ffi-hydrogen"
12
+
13
+ # Specify which files should be added to the gem when it is released.
14
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
15
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
16
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "ffi"
23
+
24
+ spec.add_development_dependency "benchmark-ips", "~> 2.7.2"
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "mad_rubocop"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rbnacl", "~> 6.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "urlcrypt"
31
+ end