ruby-chacha20 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae76267b5d649d710b44082de368560526d391089b1fc5d2886c28918b03e074
4
- data.tar.gz: 6c872222bbacc21de3ddbf7cb96128da56017919b0814b75820dbb8d708f2243
3
+ metadata.gz: 061d9c24c329984525f22fd468f5bea38c1aee9e4c9e660da96514697db5e8ef
4
+ data.tar.gz: 25bf1a1ccddac6fae6f2b133b2dd570cd0cc070eea91e8338f12e029fe5b14a3
5
5
  SHA512:
6
- metadata.gz: d7349ea022faa1f730d22323d7235fdc7c6d565e72ac462a5b06731623b0b0375f963d0510baac2818ec3557231e32046cb1e8a954c07a6d721cde3e1e82c6d5
7
- data.tar.gz: d0b6984205b4ea31b237686ac722a060b3c02bdc5e8bcb8bda286107a84498e7141ed14b2c109fde841b6126796fd537ab0da1e99494d5b8f3b49406f2f8a5d2
6
+ metadata.gz: 77226764ab9a757bd6fc5ddfe9057ec0f9c8e0312530a0420da4c6dd9b0615d098ddeef77d0bd97bded09c29881e61874bc6c6c5af572505bf6abff9b2f886b1
7
+ data.tar.gz: 9e5afca4c6194838959a25d07b8b47db542f6afd95fde18c12c883f59982dd9ec0c958d6ad41f5a2eb7e90fcf7a765a614f9a8ebb67aefbaec62a123cd3acd3b
data/README.md CHANGED
@@ -8,7 +8,11 @@ only, it does not provide any kind of message authentication or integrity checki
8
8
 
9
9
  ## Installation
10
10
 
11
- For the time being, this is not on rubygems.org. Point your Gemfile to this repository.
11
+ Add this Gem to your Gemfile:
12
+
13
+ ```ruby
14
+ gem "ruby-chacha20"
15
+ ```
12
16
 
13
17
  ## Usage
14
18
 
data/Rakefile CHANGED
@@ -13,6 +13,7 @@ task default: :test
13
13
  task benchmark: :compile do
14
14
  $LOAD_PATH.unshift File.expand_path("lib", __dir__)
15
15
  require "benchmark"
16
+ require "memory_profiler"
16
17
  require "ruby-chacha20"
17
18
 
18
19
  def read_hex(inp)
@@ -27,4 +28,8 @@ task benchmark: :compile do
27
28
  Benchmark.bm do |bm|
28
29
  bm.report("ChaCha20#encrypt") { ChaCha20.new(key, nonce).encrypt("\x00".b * bytesize) }
29
30
  end
31
+
32
+ MemoryProfiler.report do
33
+ ChaCha20.new(key, nonce).encrypt("\x00".b * bytesize)
34
+ end.pretty_print
30
35
  end
@@ -71,18 +71,21 @@ static VALUE rb_chacha20_get_counter(VALUE self) {
71
71
  return rb_ull2inum(((unsigned LONG_LONG)(ctx->input[13]) << 32) | (unsigned LONG_LONG)(ctx->input[12]));
72
72
  }
73
73
 
74
- static VALUE rb_chacha20_encrypt_or_decrypt(VALUE self, VALUE input) {
75
- VALUE output;
74
+ static VALUE rb_chacha20_encrypt_or_decrypt(VALUE self, VALUE input, VALUE outbuf) {
76
75
  ECRYPT_ctx *ctx;
77
76
 
78
77
  Check_Type(input, RUBY_T_STRING);
78
+ Check_Type(outbuf, RUBY_T_STRING);
79
+
80
+ if (RSTRING_LEN(outbuf) != RSTRING_LEN(input)) {
81
+ rb_raise(rb_eArgError, "Output buffer must have the same size as the input");
82
+ }
79
83
 
80
84
  Data_Get_Struct(self, ECRYPT_ctx, ctx);
81
85
 
82
- output = rb_str_new(0, RSTRING_LEN(input));
83
- ECRYPT_encrypt_bytes(ctx, (const unsigned char*)RSTRING_PTR(input), (unsigned char*)RSTRING_PTR(output), (unsigned int)RSTRING_LEN(input));
86
+ ECRYPT_encrypt_bytes(ctx, (const unsigned char*)RSTRING_PTR(input), (unsigned char*)RSTRING_PTR(outbuf), (unsigned int)RSTRING_LEN(input));
84
87
 
85
- return output;
88
+ return outbuf;
86
89
  }
87
90
 
88
91
  void Init_chacha20_bindings() {
@@ -95,5 +98,5 @@ void Init_chacha20_bindings() {
95
98
  rb_define_private_method(cChaCha20, "get_nonce", rb_chacha20_get_nonce, 0);
96
99
  rb_define_private_method(cChaCha20, "set_counter", rb_chacha20_set_counter, 1);
97
100
  rb_define_private_method(cChaCha20, "get_counter", rb_chacha20_get_counter, 0);
98
- rb_define_private_method(cChaCha20, "encrypt_or_decrypt", rb_chacha20_encrypt_or_decrypt, 1);
101
+ rb_define_private_method(cChaCha20, "encrypt_or_decrypt", rb_chacha20_encrypt_or_decrypt, 2);
99
102
  }
@@ -50,14 +50,14 @@ class ChaCha20
50
50
 
51
51
  length = input.bytesize
52
52
 
53
- result = "\x00".b * @block_offset + input
54
- result = encrypt_or_decrypt(result)
55
- result = result.slice(@block_offset, length)
53
+ buffer = "\x00".b * @block_offset + input
54
+ buffer = encrypt_or_decrypt(buffer, buffer)
55
+ buffer.slice!(0, @block_offset)
56
56
 
57
57
  @block_offset = (@block_offset + length) % 64
58
58
  set_counter(get_counter - 1) unless @block_offset.zero?
59
59
 
60
- result
60
+ buffer
61
61
  end
62
62
 
63
63
  alias_method :decrypt, :encrypt
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-chacha20
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Gitter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-11 00:00:00.000000000 Z
11
+ date: 2025-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: memory_profiler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
55
69
  description:
56
70
  email:
57
71
  - contact@agitter.de