digest-blake2b 0.0.4 → 0.0.6

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.
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class Blake2bGCTest < Minitest::Test
4
+ def test_a_million_iteration
5
+ 1_000_000.times do |i|
6
+ Digest::Blake2b.new(32, Digest::Blake2b::Key.none).digest('abc', :to_hex)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/assertions'
3
+ require 'digest/blake2b'
metadata CHANGED
@@ -1,80 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: digest-blake2b
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Kotov
8
8
  - Franck Verrot
9
9
  - Mauricio Gomes
10
- autorequire:
11
10
  bindir: exe
12
11
  cert_chain: []
13
- date: 2021-11-19 00:00:00.000000000 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: bundler
17
- requirement: !ruby/object:Gem::Requirement
18
- requirements:
19
- - - "~>"
20
- - !ruby/object:Gem::Version
21
- version: '2.2'
22
- type: :development
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- requirements:
26
- - - "~>"
27
- - !ruby/object:Gem::Version
28
- version: '2.2'
29
- - !ruby/object:Gem::Dependency
30
- name: minitest
31
- requirement: !ruby/object:Gem::Requirement
32
- requirements:
33
- - - "~>"
34
- - !ruby/object:Gem::Version
35
- version: '5.11'
36
- type: :development
37
- prerelease: false
38
- version_requirements: !ruby/object:Gem::Requirement
39
- requirements:
40
- - - "~>"
41
- - !ruby/object:Gem::Version
42
- version: '5.11'
43
- - !ruby/object:Gem::Dependency
44
- name: rake
45
- requirement: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: '13.0'
50
- type: :development
51
- prerelease: false
52
- version_requirements: !ruby/object:Gem::Requirement
53
- requirements:
54
- - - "~>"
55
- - !ruby/object:Gem::Version
56
- version: '13.0'
57
- - !ruby/object:Gem::Dependency
58
- name: rake-compiler
59
- requirement: !ruby/object:Gem::Requirement
60
- requirements:
61
- - - "~>"
62
- - !ruby/object:Gem::Version
63
- version: '0.9'
64
- type: :development
65
- prerelease: false
66
- version_requirements: !ruby/object:Gem::Requirement
67
- requirements:
68
- - - "~>"
69
- - !ruby/object:Gem::Version
70
- version: '0.9'
12
+ date: 1980-01-02 00:00:00.000000000 Z
13
+ dependencies: []
71
14
  description: BLAKE2b is a cryptographic hash function faster than MD5, SHA-1, SHA-2,
72
15
  and SHA-3 for 64-bit systems.
73
16
  email:
74
- - mauricio@edge14.com
17
+ - kotovalexarian@gmail.com
75
18
  executables: []
76
19
  extensions:
77
- - ext/digest/blake2b/sse/extconf.rb
20
+ - ext/digest/blake2b/ext/extconf.rb
78
21
  extra_rdoc_files: []
79
22
  files:
80
23
  - ".circleci/config.yml"
@@ -85,33 +28,35 @@ files:
85
28
  - Rakefile
86
29
  - checksums/.keep
87
30
  - digest-blake2b.gemspec
88
- - ext/digest/blake2b/ref/blake2-impl.h
89
- - ext/digest/blake2b/ref/blake2.h
90
- - ext/digest/blake2b/ref/blake2b-ref.c
91
- - ext/digest/blake2b/ref/extconf.rb
92
- - ext/digest/blake2b/ref/rbext.c
93
- - ext/digest/blake2b/sse/blake2-config.h
94
- - ext/digest/blake2b/sse/blake2-impl.h
95
- - ext/digest/blake2b/sse/blake2.h
96
- - ext/digest/blake2b/sse/blake2b-load-sse2.h
97
- - ext/digest/blake2b/sse/blake2b-load-sse41.h
98
- - ext/digest/blake2b/sse/blake2b-ref.c
99
- - ext/digest/blake2b/sse/blake2b-round.h
100
- - ext/digest/blake2b/sse/extconf.rb
101
- - ext/digest/blake2b/sse/rbext.c
31
+ - ext/digest/blake2b/ext/blake2-impl.h
32
+ - ext/digest/blake2b/ext/blake2.h
33
+ - ext/digest/blake2b/ext/blake2b-ref.c
34
+ - ext/digest/blake2b/ext/extconf.rb
35
+ - ext/digest/blake2b/ext/rbext.c
36
+ - ext/digest/blake2b/ext/ref/blake2b-ref.c
37
+ - ext/digest/blake2b/ext/sse/blake2-config.h
38
+ - ext/digest/blake2b/ext/sse/blake2b-load-sse2.h
39
+ - ext/digest/blake2b/ext/sse/blake2b-load-sse41.h
40
+ - ext/digest/blake2b/ext/sse/blake2b-ref.c
41
+ - ext/digest/blake2b/ext/sse/blake2b-round.h
102
42
  - lib/digest/blake2b.rb
103
- - lib/digest/blake2b/impl.rb
104
43
  - lib/digest/blake2b/key.rb
105
44
  - lib/digest/blake2b/version.rb
106
45
  - performance/performance_suite.rb
46
+ - test/blake2/key_test.rb
47
+ - test/blake2b_api_test.rb
48
+ - test/blake2b_test.rb
49
+ - test/blake2b_vector_test.rb
50
+ - test/performance/blake2b_gc_test.rb
51
+ - test/test_helper.rb
107
52
  homepage: https://github.com/kotovalexarian/digest-blake2b
108
53
  licenses:
109
54
  - GPL-3.0
110
55
  metadata:
56
+ rubygems_mfa_required: 'true'
111
57
  homepage_uri: https://github.com/kotovalexarian/digest-blake2b
112
58
  source_code_uri: https://github.com/kotovalexarian/digest-blake2b
113
59
  bug_tracker_uri: https://github.com/kotovalexarian/digest-blake2b/issues
114
- post_install_message:
115
60
  rdoc_options: []
116
61
  require_paths:
117
62
  - lib
@@ -119,18 +64,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
64
  requirements:
120
65
  - - ">="
121
66
  - !ruby/object:Gem::Version
122
- version: '2.1'
67
+ version: '3.1'
123
68
  - - "<"
124
69
  - !ruby/object:Gem::Version
125
- version: '4'
70
+ version: '4.1'
126
71
  required_rubygems_version: !ruby/object:Gem::Requirement
127
72
  requirements:
128
73
  - - ">="
129
74
  - !ruby/object:Gem::Version
130
75
  version: '0'
131
76
  requirements: []
132
- rubygems_version: 3.2.22
133
- signing_key:
77
+ rubygems_version: 4.0.3
134
78
  specification_version: 4
135
79
  summary: The BLAKE2b cryptographic hash function.
136
- test_files: []
80
+ test_files:
81
+ - test/blake2/key_test.rb
82
+ - test/blake2b_api_test.rb
83
+ - test/blake2b_test.rb
84
+ - test/blake2b_vector_test.rb
85
+ - test/performance/blake2b_gc_test.rb
86
+ - test/test_helper.rb
@@ -1,160 +0,0 @@
1
- /*
2
- BLAKE2 reference source code package - reference C implementations
3
-
4
- Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
5
- terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
6
- your option. The terms of these licenses can be found at:
7
-
8
- - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
9
- - OpenSSL license : https://www.openssl.org/source/license.html
10
- - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
11
-
12
- More information about the BLAKE2 hash function can be found at
13
- https://blake2.net.
14
- */
15
- #ifndef BLAKE2_IMPL_H
16
- #define BLAKE2_IMPL_H
17
-
18
- #include <stdint.h>
19
- #include <string.h>
20
-
21
- #if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
22
- #if defined(_MSC_VER)
23
- #define BLAKE2_INLINE __inline
24
- #elif defined(__GNUC__)
25
- #define BLAKE2_INLINE __inline__
26
- #else
27
- #define BLAKE2_INLINE
28
- #endif
29
- #else
30
- #define BLAKE2_INLINE inline
31
- #endif
32
-
33
- static BLAKE2_INLINE uint32_t load32( const void *src )
34
- {
35
- #if defined(NATIVE_LITTLE_ENDIAN)
36
- uint32_t w;
37
- memcpy(&w, src, sizeof w);
38
- return w;
39
- #else
40
- const uint8_t *p = ( const uint8_t * )src;
41
- return (( uint32_t )( p[0] ) << 0) |
42
- (( uint32_t )( p[1] ) << 8) |
43
- (( uint32_t )( p[2] ) << 16) |
44
- (( uint32_t )( p[3] ) << 24) ;
45
- #endif
46
- }
47
-
48
- static BLAKE2_INLINE uint64_t load64( const void *src )
49
- {
50
- #if defined(NATIVE_LITTLE_ENDIAN)
51
- uint64_t w;
52
- memcpy(&w, src, sizeof w);
53
- return w;
54
- #else
55
- const uint8_t *p = ( const uint8_t * )src;
56
- return (( uint64_t )( p[0] ) << 0) |
57
- (( uint64_t )( p[1] ) << 8) |
58
- (( uint64_t )( p[2] ) << 16) |
59
- (( uint64_t )( p[3] ) << 24) |
60
- (( uint64_t )( p[4] ) << 32) |
61
- (( uint64_t )( p[5] ) << 40) |
62
- (( uint64_t )( p[6] ) << 48) |
63
- (( uint64_t )( p[7] ) << 56) ;
64
- #endif
65
- }
66
-
67
- static BLAKE2_INLINE uint16_t load16( const void *src )
68
- {
69
- #if defined(NATIVE_LITTLE_ENDIAN)
70
- uint16_t w;
71
- memcpy(&w, src, sizeof w);
72
- return w;
73
- #else
74
- const uint8_t *p = ( const uint8_t * )src;
75
- return (( uint16_t )( p[0] ) << 0) |
76
- (( uint16_t )( p[1] ) << 8) ;
77
- #endif
78
- }
79
-
80
- static BLAKE2_INLINE void store16( void *dst, uint16_t w )
81
- {
82
- #if defined(NATIVE_LITTLE_ENDIAN)
83
- memcpy(dst, &w, sizeof w);
84
- #else
85
- uint8_t *p = ( uint8_t * )dst;
86
- *p++ = ( uint8_t )w; w >>= 8;
87
- *p++ = ( uint8_t )w;
88
- #endif
89
- }
90
-
91
- static BLAKE2_INLINE void store32( void *dst, uint32_t w )
92
- {
93
- #if defined(NATIVE_LITTLE_ENDIAN)
94
- memcpy(dst, &w, sizeof w);
95
- #else
96
- uint8_t *p = ( uint8_t * )dst;
97
- p[0] = (uint8_t)(w >> 0);
98
- p[1] = (uint8_t)(w >> 8);
99
- p[2] = (uint8_t)(w >> 16);
100
- p[3] = (uint8_t)(w >> 24);
101
- #endif
102
- }
103
-
104
- static BLAKE2_INLINE void store64( void *dst, uint64_t w )
105
- {
106
- #if defined(NATIVE_LITTLE_ENDIAN)
107
- memcpy(dst, &w, sizeof w);
108
- #else
109
- uint8_t *p = ( uint8_t * )dst;
110
- p[0] = (uint8_t)(w >> 0);
111
- p[1] = (uint8_t)(w >> 8);
112
- p[2] = (uint8_t)(w >> 16);
113
- p[3] = (uint8_t)(w >> 24);
114
- p[4] = (uint8_t)(w >> 32);
115
- p[5] = (uint8_t)(w >> 40);
116
- p[6] = (uint8_t)(w >> 48);
117
- p[7] = (uint8_t)(w >> 56);
118
- #endif
119
- }
120
-
121
- static BLAKE2_INLINE uint64_t load48( const void *src )
122
- {
123
- const uint8_t *p = ( const uint8_t * )src;
124
- return (( uint64_t )( p[0] ) << 0) |
125
- (( uint64_t )( p[1] ) << 8) |
126
- (( uint64_t )( p[2] ) << 16) |
127
- (( uint64_t )( p[3] ) << 24) |
128
- (( uint64_t )( p[4] ) << 32) |
129
- (( uint64_t )( p[5] ) << 40) ;
130
- }
131
-
132
- static BLAKE2_INLINE void store48( void *dst, uint64_t w )
133
- {
134
- uint8_t *p = ( uint8_t * )dst;
135
- p[0] = (uint8_t)(w >> 0);
136
- p[1] = (uint8_t)(w >> 8);
137
- p[2] = (uint8_t)(w >> 16);
138
- p[3] = (uint8_t)(w >> 24);
139
- p[4] = (uint8_t)(w >> 32);
140
- p[5] = (uint8_t)(w >> 40);
141
- }
142
-
143
- static BLAKE2_INLINE uint32_t rotr32( const uint32_t w, const unsigned c )
144
- {
145
- return ( w >> c ) | ( w << ( 32 - c ) );
146
- }
147
-
148
- static BLAKE2_INLINE uint64_t rotr64( const uint64_t w, const unsigned c )
149
- {
150
- return ( w >> c ) | ( w << ( 64 - c ) );
151
- }
152
-
153
- /* prevents compiler optimizing out memset() */
154
- static BLAKE2_INLINE void secure_zero_memory(void *v, size_t n)
155
- {
156
- static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
157
- memset_v(v, 0, n);
158
- }
159
-
160
- #endif
@@ -1,195 +0,0 @@
1
- /*
2
- BLAKE2 reference source code package - reference C implementations
3
-
4
- Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
5
- terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
6
- your option. The terms of these licenses can be found at:
7
-
8
- - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
9
- - OpenSSL license : https://www.openssl.org/source/license.html
10
- - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
11
-
12
- More information about the BLAKE2 hash function can be found at
13
- https://blake2.net.
14
- */
15
- #ifndef BLAKE2_H
16
- #define BLAKE2_H
17
-
18
- #include <stddef.h>
19
- #include <stdint.h>
20
-
21
- #if defined(_MSC_VER)
22
- #define BLAKE2_PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop))
23
- #else
24
- #define BLAKE2_PACKED(x) x __attribute__((packed))
25
- #endif
26
-
27
- #if defined(__cplusplus)
28
- extern "C" {
29
- #endif
30
-
31
- enum blake2s_constant
32
- {
33
- BLAKE2S_BLOCKBYTES = 64,
34
- BLAKE2S_OUTBYTES = 32,
35
- BLAKE2S_KEYBYTES = 32,
36
- BLAKE2S_SALTBYTES = 8,
37
- BLAKE2S_PERSONALBYTES = 8
38
- };
39
-
40
- enum blake2b_constant
41
- {
42
- BLAKE2B_BLOCKBYTES = 128,
43
- BLAKE2B_OUTBYTES = 64,
44
- BLAKE2B_KEYBYTES = 64,
45
- BLAKE2B_SALTBYTES = 16,
46
- BLAKE2B_PERSONALBYTES = 16
47
- };
48
-
49
- typedef struct blake2s_state__
50
- {
51
- uint32_t h[8];
52
- uint32_t t[2];
53
- uint32_t f[2];
54
- uint8_t buf[BLAKE2S_BLOCKBYTES];
55
- size_t buflen;
56
- size_t outlen;
57
- uint8_t last_node;
58
- } blake2s_state;
59
-
60
- typedef struct blake2b_state__
61
- {
62
- uint64_t h[8];
63
- uint64_t t[2];
64
- uint64_t f[2];
65
- uint8_t buf[BLAKE2B_BLOCKBYTES];
66
- size_t buflen;
67
- size_t outlen;
68
- uint8_t last_node;
69
- } blake2b_state;
70
-
71
- typedef struct blake2sp_state__
72
- {
73
- blake2s_state S[8][1];
74
- blake2s_state R[1];
75
- uint8_t buf[8 * BLAKE2S_BLOCKBYTES];
76
- size_t buflen;
77
- size_t outlen;
78
- } blake2sp_state;
79
-
80
- typedef struct blake2bp_state__
81
- {
82
- blake2b_state S[4][1];
83
- blake2b_state R[1];
84
- uint8_t buf[4 * BLAKE2B_BLOCKBYTES];
85
- size_t buflen;
86
- size_t outlen;
87
- } blake2bp_state;
88
-
89
-
90
- BLAKE2_PACKED(struct blake2s_param__
91
- {
92
- uint8_t digest_length; /* 1 */
93
- uint8_t key_length; /* 2 */
94
- uint8_t fanout; /* 3 */
95
- uint8_t depth; /* 4 */
96
- uint32_t leaf_length; /* 8 */
97
- uint32_t node_offset; /* 12 */
98
- uint16_t xof_length; /* 14 */
99
- uint8_t node_depth; /* 15 */
100
- uint8_t inner_length; /* 16 */
101
- /* uint8_t reserved[0]; */
102
- uint8_t salt[BLAKE2S_SALTBYTES]; /* 24 */
103
- uint8_t personal[BLAKE2S_PERSONALBYTES]; /* 32 */
104
- });
105
-
106
- typedef struct blake2s_param__ blake2s_param;
107
-
108
- BLAKE2_PACKED(struct blake2b_param__
109
- {
110
- uint8_t digest_length; /* 1 */
111
- uint8_t key_length; /* 2 */
112
- uint8_t fanout; /* 3 */
113
- uint8_t depth; /* 4 */
114
- uint32_t leaf_length; /* 8 */
115
- uint32_t node_offset; /* 12 */
116
- uint32_t xof_length; /* 16 */
117
- uint8_t node_depth; /* 17 */
118
- uint8_t inner_length; /* 18 */
119
- uint8_t reserved[14]; /* 32 */
120
- uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
121
- uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
122
- });
123
-
124
- typedef struct blake2b_param__ blake2b_param;
125
-
126
- typedef struct blake2xs_state__
127
- {
128
- blake2s_state S[1];
129
- blake2s_param P[1];
130
- } blake2xs_state;
131
-
132
- typedef struct blake2xb_state__
133
- {
134
- blake2b_state S[1];
135
- blake2b_param P[1];
136
- } blake2xb_state;
137
-
138
- /* Padded structs result in a compile-time error */
139
- enum {
140
- BLAKE2_DUMMY_1 = 1/(sizeof(blake2s_param) == BLAKE2S_OUTBYTES),
141
- BLAKE2_DUMMY_2 = 1/(sizeof(blake2b_param) == BLAKE2B_OUTBYTES)
142
- };
143
-
144
- /* Streaming API */
145
- int blake2s_init( blake2s_state *S, size_t outlen );
146
- int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
147
- int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
148
- int blake2s_update( blake2s_state *S, const void *in, size_t inlen );
149
- int blake2s_final( blake2s_state *S, void *out, size_t outlen );
150
-
151
- int blake2b_init( blake2b_state *S, size_t outlen );
152
- int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
153
- int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
154
- int blake2b_update( blake2b_state *S, const void *in, size_t inlen );
155
- int blake2b_final( blake2b_state *S, void *out, size_t outlen );
156
-
157
- int blake2sp_init( blake2sp_state *S, size_t outlen );
158
- int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen );
159
- int blake2sp_update( blake2sp_state *S, const void *in, size_t inlen );
160
- int blake2sp_final( blake2sp_state *S, void *out, size_t outlen );
161
-
162
- int blake2bp_init( blake2bp_state *S, size_t outlen );
163
- int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen );
164
- int blake2bp_update( blake2bp_state *S, const void *in, size_t inlen );
165
- int blake2bp_final( blake2bp_state *S, void *out, size_t outlen );
166
-
167
- /* Variable output length API */
168
- int blake2xs_init( blake2xs_state *S, const size_t outlen );
169
- int blake2xs_init_key( blake2xs_state *S, const size_t outlen, const void *key, size_t keylen );
170
- int blake2xs_update( blake2xs_state *S, const void *in, size_t inlen );
171
- int blake2xs_final(blake2xs_state *S, void *out, size_t outlen);
172
-
173
- int blake2xb_init( blake2xb_state *S, const size_t outlen );
174
- int blake2xb_init_key( blake2xb_state *S, const size_t outlen, const void *key, size_t keylen );
175
- int blake2xb_update( blake2xb_state *S, const void *in, size_t inlen );
176
- int blake2xb_final(blake2xb_state *S, void *out, size_t outlen);
177
-
178
- /* Simple API */
179
- int blake2s( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
180
- int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
181
-
182
- int blake2sp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
183
- int blake2bp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
184
-
185
- int blake2xs( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
186
- int blake2xb( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
187
-
188
- /* This is simply an alias for blake2b */
189
- int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
190
-
191
- #if defined(__cplusplus)
192
- }
193
- #endif
194
-
195
- #endif
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'mkmf'
4
-
5
- $CFLAGS += ' -std=c99 -pedantic -Wall -Wextra'
6
-
7
- create_makefile 'digest/blake2b/ref'
@@ -1,111 +0,0 @@
1
- #include <ruby/ruby.h>
2
- #include <ruby/encoding.h>
3
- #include "blake2.h"
4
-
5
- typedef struct {
6
- size_t key_length;
7
- uint8_t *key_bytes;
8
-
9
- size_t output_length;
10
- uint8_t *output;
11
-
12
- VALUE to_hex;
13
- VALUE to_bytes;
14
- } Blake2;
15
-
16
- VALUE mDigest;
17
- VALUE mDigest_cBlake2;
18
-
19
- static void blake2_free(Blake2 *blake2) {
20
- free(blake2->key_bytes);
21
- free(blake2->output);
22
-
23
- rb_gc_mark(blake2->to_hex);
24
- rb_gc_mark(blake2->to_bytes);
25
-
26
- ruby_xfree(blake2);
27
- }
28
-
29
- static VALUE blake2_alloc(VALUE klass) {
30
- Blake2 *blake2_obj = (Blake2 *)ruby_xmalloc(sizeof(Blake2));
31
-
32
- return Data_Wrap_Struct(klass, NULL, blake2_free, blake2_obj);
33
- }
34
-
35
- VALUE m_blake2_initialize(VALUE self, VALUE _len, VALUE _key) {
36
- Blake2 *blake2;
37
- Data_Get_Struct(self, Blake2, blake2);
38
- unsigned int i;
39
-
40
- ID bytes_method = rb_intern("bytes");
41
- blake2->to_hex = ID2SYM(rb_intern("to_hex"));
42
- blake2->to_bytes = ID2SYM(rb_intern("to_bytes"));
43
-
44
- VALUE key_bytes_ary = rb_funcall(_key, bytes_method, 0);
45
- blake2->key_length = RARRAY_LEN(key_bytes_ary);
46
- blake2->key_bytes = (uint8_t*)malloc(blake2->key_length * sizeof(uint8_t));
47
-
48
- for(i = 0; i < blake2->key_length; i++) {
49
- VALUE byte = rb_ary_entry(key_bytes_ary, i);
50
- blake2->key_bytes[i] = NUM2INT(byte);
51
- }
52
-
53
- blake2->output_length = NUM2INT(_len);
54
- blake2->output = (uint8_t*)malloc(blake2->output_length * sizeof(uint8_t));
55
-
56
- return Qnil;
57
- }
58
-
59
-
60
- VALUE m_blake2_digest(VALUE self, VALUE _input, VALUE _representation) {
61
- Blake2 *blake2;
62
-
63
- char *input = RSTRING_PTR(_input);
64
- uint64_t input_length = RSTRING_LEN(_input);
65
- unsigned int i;
66
-
67
- Data_Get_Struct(self, Blake2, blake2);
68
-
69
- blake2b(blake2->output, blake2->output_length, input, input_length,
70
- blake2->key_bytes, blake2->key_length);
71
-
72
- VALUE result;
73
-
74
- if(_representation == blake2->to_bytes) {
75
- result = rb_ary_new2(blake2->output_length);
76
-
77
- for(i = 0; i < blake2->output_length; i++) {
78
- rb_ary_push(result, INT2NUM(blake2->output[i]));
79
- }
80
- } else if(_representation == blake2->to_hex) {
81
- unsigned long ary_len = blake2->output_length * (unsigned)sizeof(char) * 2;
82
- char *c_str = (char*)malloc(ary_len + 1);
83
-
84
- for(i = 0; i < blake2->output_length; i++) {
85
- sprintf(c_str + (i * 2), "%02x", blake2->output[i]);
86
- }
87
- c_str[ary_len] = 0;
88
-
89
- result = rb_str_new(c_str, ary_len);
90
-
91
- if((unsigned long)RSTRING_LEN(result) != ary_len) {
92
- rb_raise(rb_eRuntimeError, "m_blake2_digest: sizes don't match. Ary: %lu != %lu", RSTRING_LEN(result), ary_len);
93
- }
94
-
95
- free(c_str);
96
- } else {
97
- rb_raise(rb_eArgError, "unknown representation :%"PRIsVALUE"", _representation);
98
- }
99
-
100
- return result;
101
- }
102
-
103
- void Init_ref() {
104
- mDigest = rb_define_module("Digest");
105
-
106
- mDigest_cBlake2 = rb_define_class_under(mDigest, "Blake2b", rb_cObject);
107
- rb_define_alloc_func(mDigest_cBlake2, blake2_alloc);
108
-
109
- rb_define_private_method(mDigest_cBlake2, "initialize", RUBY_METHOD_FUNC(m_blake2_initialize), 2);
110
- rb_define_method(mDigest_cBlake2, "digest", RUBY_METHOD_FUNC(m_blake2_digest), 2);
111
- }
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'mkmf'
4
-
5
- $CFLAGS += ' -std=c99 -pedantic -Wall -Wextra -Wno-long-long'
6
-
7
- create_makefile 'digest/blake2b/sse'
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Digest
4
- class Blake2b
5
- if RUBY_PLATFORM =~ /^x86_64/
6
- IMPL = 'sse'
7
- else
8
- IMPL = 'ref'
9
- end
10
- end
11
- end
File without changes
File without changes
File without changes