blake3-rb 0.1.0.pre10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Cargo.lock +302 -0
- data/Cargo.toml +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +139 -0
- data/ext/digest/blake3_ext/Cargo.toml +14 -0
- data/ext/digest/blake3_ext/extconf.rb +6 -0
- data/ext/digest/blake3_ext/src/bindings.rs +31 -0
- data/ext/digest/blake3_ext/src/lib.rs +77 -0
- data/lib/blake3-rb.rb +3 -0
- data/lib/digest/blake3.rb +9 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0fc2dd5f00543069207aa5842aab4eea78b26c6dcdc0ab6b406e8dc3f121bb40
|
4
|
+
data.tar.gz: 577fb0229048c29438f15ac589b94457ec7ae13ac96e271c1f1c9e0d11cdec68
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f5636840cf7dfeeb23bd3f9f6c0f578f1cf13621caa491a28124214b79486885735f02ea6878e463fa034336139eeb56d5a525558174e4eb826e08e005dc77ce
|
7
|
+
data.tar.gz: 4e1ef730c8e7da7a110150f30e9e73a16b703adf20b302ca358fb0cbe40299a0488bd927cfd537caa0617bda64add61d892a2c258a39da335e1b2256d7ab437a
|
data/Cargo.lock
ADDED
@@ -0,0 +1,302 @@
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
2
|
+
# It is not intended for manual editing.
|
3
|
+
version = 3
|
4
|
+
|
5
|
+
[[package]]
|
6
|
+
name = "aho-corasick"
|
7
|
+
version = "1.1.2"
|
8
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
9
|
+
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
10
|
+
dependencies = [
|
11
|
+
"memchr",
|
12
|
+
]
|
13
|
+
|
14
|
+
[[package]]
|
15
|
+
name = "arrayref"
|
16
|
+
version = "0.3.7"
|
17
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
18
|
+
checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545"
|
19
|
+
|
20
|
+
[[package]]
|
21
|
+
name = "arrayvec"
|
22
|
+
version = "0.7.4"
|
23
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
24
|
+
checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
|
25
|
+
|
26
|
+
[[package]]
|
27
|
+
name = "bindgen"
|
28
|
+
version = "0.66.1"
|
29
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
30
|
+
checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7"
|
31
|
+
dependencies = [
|
32
|
+
"bitflags",
|
33
|
+
"cexpr",
|
34
|
+
"clang-sys",
|
35
|
+
"lazy_static",
|
36
|
+
"lazycell",
|
37
|
+
"peeking_take_while",
|
38
|
+
"proc-macro2",
|
39
|
+
"quote",
|
40
|
+
"regex",
|
41
|
+
"rustc-hash",
|
42
|
+
"shlex",
|
43
|
+
"syn",
|
44
|
+
]
|
45
|
+
|
46
|
+
[[package]]
|
47
|
+
name = "bitflags"
|
48
|
+
version = "2.4.0"
|
49
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
50
|
+
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
|
51
|
+
|
52
|
+
[[package]]
|
53
|
+
name = "blake3"
|
54
|
+
version = "1.5.0"
|
55
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
56
|
+
checksum = "0231f06152bf547e9c2b5194f247cd97aacf6dcd8b15d8e5ec0663f64580da87"
|
57
|
+
dependencies = [
|
58
|
+
"arrayref",
|
59
|
+
"arrayvec",
|
60
|
+
"cc",
|
61
|
+
"cfg-if",
|
62
|
+
"constant_time_eq",
|
63
|
+
]
|
64
|
+
|
65
|
+
[[package]]
|
66
|
+
name = "blake3_ext"
|
67
|
+
version = "0.1.0"
|
68
|
+
dependencies = [
|
69
|
+
"blake3",
|
70
|
+
"rb-sys",
|
71
|
+
]
|
72
|
+
|
73
|
+
[[package]]
|
74
|
+
name = "cc"
|
75
|
+
version = "1.0.83"
|
76
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
77
|
+
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
|
78
|
+
dependencies = [
|
79
|
+
"libc",
|
80
|
+
]
|
81
|
+
|
82
|
+
[[package]]
|
83
|
+
name = "cexpr"
|
84
|
+
version = "0.6.0"
|
85
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
86
|
+
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
|
87
|
+
dependencies = [
|
88
|
+
"nom",
|
89
|
+
]
|
90
|
+
|
91
|
+
[[package]]
|
92
|
+
name = "cfg-if"
|
93
|
+
version = "1.0.0"
|
94
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
95
|
+
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
96
|
+
|
97
|
+
[[package]]
|
98
|
+
name = "clang-sys"
|
99
|
+
version = "1.6.1"
|
100
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
101
|
+
checksum = "c688fc74432808e3eb684cae8830a86be1d66a2bd58e1f248ed0960a590baf6f"
|
102
|
+
dependencies = [
|
103
|
+
"glob",
|
104
|
+
"libc",
|
105
|
+
"libloading",
|
106
|
+
]
|
107
|
+
|
108
|
+
[[package]]
|
109
|
+
name = "constant_time_eq"
|
110
|
+
version = "0.3.0"
|
111
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
112
|
+
checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2"
|
113
|
+
|
114
|
+
[[package]]
|
115
|
+
name = "glob"
|
116
|
+
version = "0.3.1"
|
117
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
118
|
+
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
|
119
|
+
|
120
|
+
[[package]]
|
121
|
+
name = "lazy_static"
|
122
|
+
version = "1.4.0"
|
123
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
124
|
+
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
125
|
+
|
126
|
+
[[package]]
|
127
|
+
name = "lazycell"
|
128
|
+
version = "1.3.0"
|
129
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
130
|
+
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
131
|
+
|
132
|
+
[[package]]
|
133
|
+
name = "libc"
|
134
|
+
version = "0.2.149"
|
135
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
136
|
+
checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"
|
137
|
+
|
138
|
+
[[package]]
|
139
|
+
name = "libloading"
|
140
|
+
version = "0.7.4"
|
141
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
142
|
+
checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f"
|
143
|
+
dependencies = [
|
144
|
+
"cfg-if",
|
145
|
+
"winapi",
|
146
|
+
]
|
147
|
+
|
148
|
+
[[package]]
|
149
|
+
name = "memchr"
|
150
|
+
version = "2.6.4"
|
151
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
152
|
+
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
153
|
+
|
154
|
+
[[package]]
|
155
|
+
name = "minimal-lexical"
|
156
|
+
version = "0.2.1"
|
157
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
158
|
+
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
159
|
+
|
160
|
+
[[package]]
|
161
|
+
name = "nom"
|
162
|
+
version = "7.1.3"
|
163
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
164
|
+
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
165
|
+
dependencies = [
|
166
|
+
"memchr",
|
167
|
+
"minimal-lexical",
|
168
|
+
]
|
169
|
+
|
170
|
+
[[package]]
|
171
|
+
name = "peeking_take_while"
|
172
|
+
version = "0.1.2"
|
173
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
174
|
+
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
|
175
|
+
|
176
|
+
[[package]]
|
177
|
+
name = "proc-macro2"
|
178
|
+
version = "1.0.69"
|
179
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
180
|
+
checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da"
|
181
|
+
dependencies = [
|
182
|
+
"unicode-ident",
|
183
|
+
]
|
184
|
+
|
185
|
+
[[package]]
|
186
|
+
name = "quote"
|
187
|
+
version = "1.0.33"
|
188
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
189
|
+
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
190
|
+
dependencies = [
|
191
|
+
"proc-macro2",
|
192
|
+
]
|
193
|
+
|
194
|
+
[[package]]
|
195
|
+
name = "rb-sys"
|
196
|
+
version = "0.9.82"
|
197
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
198
|
+
checksum = "a3e6bf79bf4c711917cacfaf46dfab4314dbfdd89a8ee3ec4b98336cd23f1ebf"
|
199
|
+
dependencies = [
|
200
|
+
"rb-sys-build",
|
201
|
+
]
|
202
|
+
|
203
|
+
[[package]]
|
204
|
+
name = "rb-sys-build"
|
205
|
+
version = "0.9.82"
|
206
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
207
|
+
checksum = "5482a1ed4cde58dddaf162b6aebcb5c25645822547832b8be101f2acd40bcdd6"
|
208
|
+
dependencies = [
|
209
|
+
"bindgen",
|
210
|
+
"lazy_static",
|
211
|
+
"proc-macro2",
|
212
|
+
"quote",
|
213
|
+
"regex",
|
214
|
+
"shell-words",
|
215
|
+
"syn",
|
216
|
+
]
|
217
|
+
|
218
|
+
[[package]]
|
219
|
+
name = "regex"
|
220
|
+
version = "1.10.0"
|
221
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
222
|
+
checksum = "d119d7c7ca818f8a53c300863d4f87566aac09943aef5b355bb83969dae75d87"
|
223
|
+
dependencies = [
|
224
|
+
"aho-corasick",
|
225
|
+
"memchr",
|
226
|
+
"regex-automata",
|
227
|
+
"regex-syntax",
|
228
|
+
]
|
229
|
+
|
230
|
+
[[package]]
|
231
|
+
name = "regex-automata"
|
232
|
+
version = "0.4.1"
|
233
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
234
|
+
checksum = "465c6fc0621e4abc4187a2bda0937bfd4f722c2730b29562e19689ea796c9a4b"
|
235
|
+
dependencies = [
|
236
|
+
"aho-corasick",
|
237
|
+
"memchr",
|
238
|
+
"regex-syntax",
|
239
|
+
]
|
240
|
+
|
241
|
+
[[package]]
|
242
|
+
name = "regex-syntax"
|
243
|
+
version = "0.8.0"
|
244
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
245
|
+
checksum = "c3cbb081b9784b07cceb8824c8583f86db4814d172ab043f3c23f7dc600bf83d"
|
246
|
+
|
247
|
+
[[package]]
|
248
|
+
name = "rustc-hash"
|
249
|
+
version = "1.1.0"
|
250
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
251
|
+
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
252
|
+
|
253
|
+
[[package]]
|
254
|
+
name = "shell-words"
|
255
|
+
version = "1.1.0"
|
256
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
257
|
+
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
|
258
|
+
|
259
|
+
[[package]]
|
260
|
+
name = "shlex"
|
261
|
+
version = "1.2.0"
|
262
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
263
|
+
checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380"
|
264
|
+
|
265
|
+
[[package]]
|
266
|
+
name = "syn"
|
267
|
+
version = "2.0.38"
|
268
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
269
|
+
checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b"
|
270
|
+
dependencies = [
|
271
|
+
"proc-macro2",
|
272
|
+
"quote",
|
273
|
+
"unicode-ident",
|
274
|
+
]
|
275
|
+
|
276
|
+
[[package]]
|
277
|
+
name = "unicode-ident"
|
278
|
+
version = "1.0.12"
|
279
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
280
|
+
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
281
|
+
|
282
|
+
[[package]]
|
283
|
+
name = "winapi"
|
284
|
+
version = "0.3.9"
|
285
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
286
|
+
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
287
|
+
dependencies = [
|
288
|
+
"winapi-i686-pc-windows-gnu",
|
289
|
+
"winapi-x86_64-pc-windows-gnu",
|
290
|
+
]
|
291
|
+
|
292
|
+
[[package]]
|
293
|
+
name = "winapi-i686-pc-windows-gnu"
|
294
|
+
version = "0.4.0"
|
295
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
296
|
+
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
297
|
+
|
298
|
+
[[package]]
|
299
|
+
name = "winapi-x86_64-pc-windows-gnu"
|
300
|
+
version = "0.4.0"
|
301
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
302
|
+
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
data/Cargo.toml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# This Cargo.toml is here to let externals tools (IDEs, etc.) know that this is
|
2
|
+
# a Rust project. Your extensions dependencies should be added to the Cargo.toml
|
3
|
+
# in the ext/ directory.
|
4
|
+
|
5
|
+
[workspace]
|
6
|
+
members = ["./ext/digest/blake3_ext"]
|
7
|
+
resolver = "2"
|
8
|
+
|
9
|
+
[profile.release]
|
10
|
+
lto = true
|
11
|
+
opt-level = 3
|
12
|
+
codegen-units = 1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Shopify, Inc.
|
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,139 @@
|
|
1
|
+
# `blake3-rb`
|
2
|
+
|
3
|
+
![Build Status](https://github.com/Shopify/blake3-ruby/workflows/CI/badge.svg)
|
4
|
+
|
5
|
+
Blake3 is a Ruby gem that provides a simple and efficient way to compute the Blake3 cryptographic hash function. This gem is designed to be easy to use and integrate into your Ruby projects using the Ruby [`digest` framework](https://github.com/ruby/digest).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "blake3-rb", github: "Shopify/blake3-rb"
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
$ bundle install
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Here's a simple usage example:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require "digest/blake3"
|
27
|
+
|
28
|
+
result = Digest::Blake3.hexdigest("your data here")
|
29
|
+
```
|
30
|
+
|
31
|
+
If you need to stream data:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require "digest/blake3"
|
35
|
+
|
36
|
+
hasher = Digest::Blake3.new
|
37
|
+
hasher.update("your data here")
|
38
|
+
result = hasher.hexdigest
|
39
|
+
```
|
40
|
+
|
41
|
+
Or use the `<<` operator:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
require "digest/blake3"
|
45
|
+
|
46
|
+
hasher = Digest::Blake3.new
|
47
|
+
hasher << "part1" << "part2"
|
48
|
+
result = hasher.hexdigest
|
49
|
+
```
|
50
|
+
|
51
|
+
### Base64 Digest
|
52
|
+
|
53
|
+
You can compute the Base64 digest of your data:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
require "digest/blake3"
|
57
|
+
|
58
|
+
result = Digest::Blake3.base64digest("your data here")
|
59
|
+
```
|
60
|
+
|
61
|
+
### Equality
|
62
|
+
|
63
|
+
You can compare two digests for equality:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
require "digest/blake3"
|
67
|
+
|
68
|
+
digest_one = Digest::Blake3.new
|
69
|
+
digest_two = Digest::Blake3.new
|
70
|
+
|
71
|
+
digest_one.update("your data here")
|
72
|
+
digest_two.update("your data here")
|
73
|
+
|
74
|
+
if digest_one == digest_two
|
75
|
+
puts "Digests are equal"
|
76
|
+
else
|
77
|
+
puts "Digests are not equal"
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
You can compute the hash of a file:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
require "digest/blake3"
|
85
|
+
|
86
|
+
result = Digest::Blake3.file("path/to/your/file")
|
87
|
+
```
|
88
|
+
|
89
|
+
## Benchmarks
|
90
|
+
|
91
|
+
Here are some benchmarks comparing this gem with other digests on `x86_64-linux`:
|
92
|
+
|
93
|
+
```bash
|
94
|
+
$ ruby bench/string.rb
|
95
|
+
...
|
96
|
+
Warming up --------------------------------------
|
97
|
+
Digest::SHA1 61.000 i/100ms
|
98
|
+
Digest::SHA256 21.000 i/100ms
|
99
|
+
Digest::MD5 58.000 i/100ms
|
100
|
+
Digest::Blake3 560.000 i/100ms
|
101
|
+
Calculating -------------------------------------
|
102
|
+
Digest::SHA1 612.174 (± 0.3%) i/s - 3.111k in 5.081922s
|
103
|
+
Digest::SHA256 215.281 (± 0.0%) i/s - 1.092k in 5.072453s
|
104
|
+
Digest::MD5 586.009 (± 0.3%) i/s - 2.958k in 5.047759s
|
105
|
+
Digest::Blake3 5.698k (± 0.6%) i/s - 28.560k in 5.012308s
|
106
|
+
|
107
|
+
Comparison:
|
108
|
+
Digest::Blake3: 5698.2 i/s
|
109
|
+
Digest::SHA1: 612.2 i/s - 9.31x slower
|
110
|
+
Digest::MD5: 586.0 i/s - 9.72x slower
|
111
|
+
Digest::SHA256: 215.3 i/s - 26.47x slower
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
## Testing
|
116
|
+
|
117
|
+
First, make sure your development environment is setup:
|
118
|
+
|
119
|
+
```bash
|
120
|
+
$ bin/setup
|
121
|
+
```
|
122
|
+
|
123
|
+
To run the tests, execute:
|
124
|
+
|
125
|
+
```bash
|
126
|
+
$ bundle exec rake test
|
127
|
+
```
|
128
|
+
|
129
|
+
## Contributing
|
130
|
+
|
131
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/blake3-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/Shopify/blake3-ruby/blob/main/CODE_OF_CONDUCT.md).
|
132
|
+
|
133
|
+
## License
|
134
|
+
|
135
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
136
|
+
|
137
|
+
## Code of Conduct
|
138
|
+
|
139
|
+
Everyone interacting in the Blake3 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Shopify/blake3-ruby/blob/main/CODE_OF_CONDUCT.md).
|
@@ -0,0 +1,14 @@
|
|
1
|
+
[package]
|
2
|
+
name = "blake3_ext"
|
3
|
+
version = "0.1.0"
|
4
|
+
edition = "2021"
|
5
|
+
authors = ["Ian Ker-Seymer <ian.kerseymer@shopify.com>"]
|
6
|
+
license = "MIT"
|
7
|
+
publish = false
|
8
|
+
|
9
|
+
[lib]
|
10
|
+
crate-type = ["cdylib"]
|
11
|
+
|
12
|
+
[dependencies]
|
13
|
+
rb-sys = { version = "0.9.82", features = ["stable-api-compiled-fallback"] }
|
14
|
+
blake3 = { version = "1.5.0" }
|
@@ -0,0 +1,31 @@
|
|
1
|
+
use std::ffi::{c_int, c_uchar, c_void};
|
2
|
+
|
3
|
+
use rb_sys::{rb_data_object_wrap, rb_obj_freeze, size_t, VALUE};
|
4
|
+
|
5
|
+
pub const RUBY_DIGEST_API_VERSION: c_int = 3;
|
6
|
+
|
7
|
+
pub type RbDigestHashInitFuncT = unsafe extern "C" fn(*mut c_void) -> c_int;
|
8
|
+
pub type RbDigestHashUpdateFuncT = unsafe extern "C" fn(*mut c_void, *mut c_uchar, size_t);
|
9
|
+
pub type RbDigestHashFinishFuncT = unsafe extern "C" fn(*mut c_void, *mut c_uchar) -> c_int;
|
10
|
+
|
11
|
+
#[derive(Debug)]
|
12
|
+
#[repr(C)]
|
13
|
+
pub struct RbDigestMetadataT {
|
14
|
+
pub api_version: c_int,
|
15
|
+
pub digest_len: size_t,
|
16
|
+
pub block_len: size_t,
|
17
|
+
pub ctx_size: size_t,
|
18
|
+
pub init_func: RbDigestHashInitFuncT,
|
19
|
+
pub update_func: RbDigestHashUpdateFuncT,
|
20
|
+
pub finish_func: RbDigestHashFinishFuncT,
|
21
|
+
}
|
22
|
+
|
23
|
+
pub unsafe fn rb_digest_make_metadata(meta: &'static RbDigestMetadataT) -> VALUE {
|
24
|
+
let data = rb_data_object_wrap(
|
25
|
+
0 as VALUE,
|
26
|
+
meta as *const RbDigestMetadataT as *mut c_void,
|
27
|
+
None,
|
28
|
+
None,
|
29
|
+
);
|
30
|
+
rb_obj_freeze(data)
|
31
|
+
}
|
@@ -0,0 +1,77 @@
|
|
1
|
+
mod bindings;
|
2
|
+
|
3
|
+
use blake3::Hasher;
|
4
|
+
use rb_sys::{
|
5
|
+
rb_cObject, rb_const_get, rb_define_class_under, rb_intern, rb_ivar_set, rb_require, size_t,
|
6
|
+
};
|
7
|
+
use std::ffi::{c_int, c_uchar, c_void};
|
8
|
+
use std::os::raw::c_char;
|
9
|
+
|
10
|
+
use bindings::{rb_digest_make_metadata, RbDigestMetadataT, RUBY_DIGEST_API_VERSION};
|
11
|
+
|
12
|
+
const BLOCK_LEN: usize = 64;
|
13
|
+
const DIGEST_LEN: usize = 32;
|
14
|
+
|
15
|
+
#[repr(C)]
|
16
|
+
#[derive(Debug)]
|
17
|
+
struct Blake3Ctx {
|
18
|
+
inner: Option<Hasher>,
|
19
|
+
}
|
20
|
+
|
21
|
+
static DIGEST_METADATA: RbDigestMetadataT = RbDigestMetadataT {
|
22
|
+
api_version: RUBY_DIGEST_API_VERSION,
|
23
|
+
digest_len: DIGEST_LEN as _,
|
24
|
+
block_len: BLOCK_LEN as _,
|
25
|
+
ctx_size: std::mem::size_of::<Blake3Ctx>() as _,
|
26
|
+
init_func: blake3_init,
|
27
|
+
update_func: blake3_update,
|
28
|
+
finish_func: blake3_finish,
|
29
|
+
};
|
30
|
+
|
31
|
+
// Initialize the context, which has already been allocated by Ruby.
|
32
|
+
extern "C" fn blake3_init(ctx: *mut c_void) -> c_int {
|
33
|
+
let ctx = ctx as *mut Blake3Ctx;
|
34
|
+
let ctx = unsafe { &mut *ctx };
|
35
|
+
ctx.inner = Some(Hasher::new());
|
36
|
+
1
|
37
|
+
}
|
38
|
+
|
39
|
+
// Update the context with the given data.
|
40
|
+
extern "C" fn blake3_update(ctx: *mut c_void, data: *mut c_uchar, len: size_t) {
|
41
|
+
let ctx = ctx as *mut Blake3Ctx;
|
42
|
+
let ctx = unsafe { &mut *ctx };
|
43
|
+
if let Some(inner) = ctx.inner.as_mut() {
|
44
|
+
let slice = unsafe { std::slice::from_raw_parts(data, len as _) };
|
45
|
+
inner.update(slice);
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
// Finalize the context and write the digest to the given pointer.
|
50
|
+
extern "C" fn blake3_finish(ctx: *mut c_void, digest: *mut c_uchar) -> c_int {
|
51
|
+
let ctx = ctx as *mut Blake3Ctx;
|
52
|
+
let ctx = unsafe { &mut *ctx };
|
53
|
+
if let Some(inner) = ctx.inner.as_mut() {
|
54
|
+
let slice = unsafe { std::slice::from_raw_parts_mut(digest, DIGEST_LEN) };
|
55
|
+
inner.finalize_xof().fill(slice);
|
56
|
+
1
|
57
|
+
} else {
|
58
|
+
0
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
/// # Safety
|
63
|
+
/// This function is called by Ruby, so it must be safe.
|
64
|
+
#[no_mangle]
|
65
|
+
pub unsafe extern "C" fn Init_blake3_ext() {
|
66
|
+
rb_require("digest\0".as_ptr() as *const c_char);
|
67
|
+
let digest_module = rb_const_get(rb_cObject, rb_intern("Digest\0".as_ptr() as *const c_char));
|
68
|
+
let digest_base = rb_const_get(digest_module, rb_intern("Base\0".as_ptr() as *const c_char));
|
69
|
+
let klass = rb_define_class_under(
|
70
|
+
digest_module,
|
71
|
+
"Blake3\0".as_ptr() as *const c_char,
|
72
|
+
digest_base,
|
73
|
+
);
|
74
|
+
let meta = rb_digest_make_metadata(&DIGEST_METADATA);
|
75
|
+
let metadata_id = rb_intern("metadata\0".as_ptr() as *const c_char);
|
76
|
+
rb_ivar_set(klass, metadata_id, meta);
|
77
|
+
}
|
data/lib/blake3-rb.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Tries to require the precompiled extension for the given Ruby version first
|
4
|
+
begin
|
5
|
+
RUBY_VERSION =~ /(\d+\.\d+)/
|
6
|
+
require "digest/blake3/#{Regexp.last_match(1)}/blake3_ext"
|
7
|
+
rescue LoadError
|
8
|
+
require_relative "blake3/blake3_ext"
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blake3-rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.pre10
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ian Ker-Seymer
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rb_sys
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.9'
|
27
|
+
description: Provides native bindings to the Blake3 hash function for Ruby.
|
28
|
+
email:
|
29
|
+
- ian.kerseymer@shopify.com
|
30
|
+
executables: []
|
31
|
+
extensions:
|
32
|
+
- ext/digest/blake3_ext/extconf.rb
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- Cargo.lock
|
36
|
+
- Cargo.toml
|
37
|
+
- LICENSE.txt
|
38
|
+
- README.md
|
39
|
+
- ext/digest/blake3_ext/Cargo.toml
|
40
|
+
- ext/digest/blake3_ext/extconf.rb
|
41
|
+
- ext/digest/blake3_ext/src/bindings.rs
|
42
|
+
- ext/digest/blake3_ext/src/lib.rs
|
43
|
+
- lib/blake3-rb.rb
|
44
|
+
- lib/digest/blake3.rb
|
45
|
+
homepage: https://github.com/Shopify/blake3-ruby
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata:
|
49
|
+
allowed_push_host: https://rubygems.org
|
50
|
+
homepage_uri: https://github.com/Shopify/blake3-ruby
|
51
|
+
source_code_uri: https://github.com/Shopify/blake3-ruby
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 3.0.0
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">"
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 1.3.1
|
66
|
+
requirements: []
|
67
|
+
rubygems_version: 3.4.10
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: Blake3 hash function bindings for Ruby.
|
71
|
+
test_files: []
|