uri-ni 0.1.4 → 0.2.0
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 +4 -4
- data/lib/uri/ni/version.rb +6 -2
- data/lib/uri/ni.rb +61 -6
- metadata +3 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 46e2aa7887a91a2d14dd3f89a6a01e91ff00f6c966ba9256e256597d221d5b63
|
|
4
|
+
data.tar.gz: e1b4bf407d8c8f5b0a1643ffd6ffd3e14a7389098a0a9c1253e8cbd733003116
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 22026ee328bbd59155fa3c0ab1b85d7565f868484d7a4454c5c45aacb6ea31d8f0482b216d59b46a15a460089f896c775cb62f1cb540755e6cef96732a01977d
|
|
7
|
+
data.tar.gz: 7c101253e23585c8045571bf4d00fd42deca06339cfe71bd9d93001d1d7138fe9eff99e4f899cb5344bf2adafc411ff8cb43e4413133eb1fe6dcce524730691b
|
data/lib/uri/ni/version.rb
CHANGED
|
@@ -3,9 +3,13 @@ require 'uri/generic'
|
|
|
3
3
|
|
|
4
4
|
module URI
|
|
5
5
|
class NI < Generic
|
|
6
|
-
VERSION = "0.
|
|
6
|
+
VERSION = "0.2.0"
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
# might as well put this here
|
|
10
|
-
|
|
10
|
+
if self.respond_to? :register_scheme
|
|
11
|
+
register_scheme 'NI', NI
|
|
12
|
+
else
|
|
13
|
+
@@schemes['NI'] = NI
|
|
14
|
+
end
|
|
11
15
|
end
|
data/lib/uri/ni.rb
CHANGED
|
@@ -44,6 +44,8 @@ class URI::NI < URI::Generic
|
|
|
44
44
|
"sha-512": Digest::SHA512,
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
LENGTHS = DIGESTS.transform_values { |v| v.new.digest_length }
|
|
48
|
+
|
|
47
49
|
# resolve first against digest length and then class
|
|
48
50
|
DIGEST_REV = {
|
|
49
51
|
64 => { Digest::SHA512 => :"sha-512", Digest::SHA2 => :"sha-512" },
|
|
@@ -95,11 +97,14 @@ class URI::NI < URI::Generic
|
|
|
95
97
|
16 => [/^[0-9A-Fa-f]*$/, 'Data %s is not in hexadecimal'],
|
|
96
98
|
}
|
|
97
99
|
|
|
98
|
-
|
|
100
|
+
ASSERT_REPR = -> data, radix do
|
|
99
101
|
re, error = ASSERT[radix]
|
|
100
|
-
raise ArgumentError, error % data unless re.match data
|
|
102
|
+
raise ArgumentError, error % data unless re.match? data
|
|
101
103
|
end
|
|
102
104
|
|
|
105
|
+
define_method :assert_repr, &ASSERT_REPR
|
|
106
|
+
define_singleton_method :assert_repr, &ASSERT_REPR
|
|
107
|
+
|
|
103
108
|
# from whatever to binary
|
|
104
109
|
DECODE = {
|
|
105
110
|
256 => -> x { x },
|
|
@@ -133,12 +138,15 @@ class URI::NI < URI::Generic
|
|
|
133
138
|
16 => -> x { x.upcase },
|
|
134
139
|
}
|
|
135
140
|
|
|
136
|
-
|
|
141
|
+
TRANSCODE = -> data, from: 256, to: 256, alt: false do
|
|
137
142
|
assert_repr data, from
|
|
138
143
|
data = ENCODE[to].call(DECODE[from].call data) unless from == to
|
|
139
144
|
alt ? ALT[to].call(data) : CANON[to].call(data)
|
|
140
145
|
end
|
|
141
146
|
|
|
147
|
+
define_method :transcode, &TRANSCODE
|
|
148
|
+
define_singleton_method :transcode, &TRANSCODE
|
|
149
|
+
|
|
142
150
|
protected
|
|
143
151
|
|
|
144
152
|
# holy crap you can override these?
|
|
@@ -160,6 +168,53 @@ class URI::NI < URI::Generic
|
|
|
160
168
|
|
|
161
169
|
public
|
|
162
170
|
|
|
171
|
+
# Transform a digest for a known algorithm into a {URI::NI}. Takes a
|
|
172
|
+
# binary, Base64, Base32, or hexadecimal string.
|
|
173
|
+
#
|
|
174
|
+
# @param algorithm [Symbol,#to_sym] a supported algorithm
|
|
175
|
+
# @param digest [String, #to_s, Digest::Instance] the digest (or context)
|
|
176
|
+
#
|
|
177
|
+
# @raise [ArgumentError] if the algorithm is unsupported
|
|
178
|
+
# @raise [ArgumentError] if the input string is not the right length
|
|
179
|
+
# for the algorithm
|
|
180
|
+
# @raise [ArgumentError] if the input string is incorrectly encoded
|
|
181
|
+
#
|
|
182
|
+
# @return [URI::NI] the transformed identifier
|
|
183
|
+
#
|
|
184
|
+
def self.ingest algorithm = nil, digest
|
|
185
|
+
return compute(digest) if digest.is_a? Digest::Instance
|
|
186
|
+
return digest if digest.is_a? URI::NI # noop
|
|
187
|
+
|
|
188
|
+
# make sure we're indeed dealing with a string
|
|
189
|
+
digest = digest.to_s
|
|
190
|
+
|
|
191
|
+
# just parse if it's already a ni: URI string
|
|
192
|
+
return URI(digest) if /^ni:/i.match? digest
|
|
193
|
+
|
|
194
|
+
# get the expected length of the digest for the algorithm
|
|
195
|
+
len = LENGTHS[algorithm.to_s.downcase.to_sym] or raise ArgumentError,
|
|
196
|
+
"unsupported algorithm #{algorithm}"
|
|
197
|
+
|
|
198
|
+
# the length of the string is used to determine the expected encoding
|
|
199
|
+
radix = case digest.length
|
|
200
|
+
when len then 256
|
|
201
|
+
when -> x { x >= (len * 8 / 6.0).ceil && x < (len * 8 / 5.0).ceil }
|
|
202
|
+
# note that a length longer than base64 but shorter than base32
|
|
203
|
+
# can be assumed to be base64 with padding
|
|
204
|
+
64
|
|
205
|
+
when (len * 8 / 5.0).ceil then 32
|
|
206
|
+
when len * 2 then 16
|
|
207
|
+
else
|
|
208
|
+
raise ArgumentError, "invalid string length: #{digest.length}"
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# then we lint it to see if it's correct
|
|
212
|
+
digest = transcode digest, from: radix, to: 64
|
|
213
|
+
|
|
214
|
+
# then we transcode
|
|
215
|
+
URI("ni:///#{algorithm};#{digest}")
|
|
216
|
+
end
|
|
217
|
+
|
|
163
218
|
# Compute an RFC6920 URI from a data source.
|
|
164
219
|
#
|
|
165
220
|
# @param data [#to_s, IO, Digest, nil]
|
|
@@ -171,8 +226,8 @@ class URI::NI < URI::Generic
|
|
|
171
226
|
# @yieldparam ctx [Digest::Instance] The digest instance to the block
|
|
172
227
|
# @yieldparam buf [String, nil] The current read buffer (if +data+ is set)
|
|
173
228
|
# @yieldreturn [nil] The result of the block is ignored
|
|
174
|
-
#
|
|
175
|
-
# @return [URI::NI]
|
|
229
|
+
#
|
|
230
|
+
# @return [URI::NI]
|
|
176
231
|
def self.compute data = nil, algorithm: :"sha-256", blocksize: 65536,
|
|
177
232
|
authority: nil, query: nil, &block
|
|
178
233
|
|
|
@@ -192,7 +247,7 @@ class URI::NI < URI::Generic
|
|
|
192
247
|
ctx = DIGESTS[algorithm]
|
|
193
248
|
ctx.new
|
|
194
249
|
end
|
|
195
|
-
|
|
250
|
+
|
|
196
251
|
# (Re)-compute a digest using existing information from an instance.
|
|
197
252
|
# @see .compute
|
|
198
253
|
def compute data = nil, algorithm: nil, blocksize: 65536,
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: uri-ni
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dorian Taylor
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: bundler
|
|
@@ -65,7 +64,6 @@ licenses:
|
|
|
65
64
|
- Apache-2.0
|
|
66
65
|
metadata:
|
|
67
66
|
homepage_uri: https://github.com/doriantaylor/rb-uri-ni
|
|
68
|
-
post_install_message:
|
|
69
67
|
rdoc_options: []
|
|
70
68
|
require_paths:
|
|
71
69
|
- lib
|
|
@@ -80,8 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
80
78
|
- !ruby/object:Gem::Version
|
|
81
79
|
version: '0'
|
|
82
80
|
requirements: []
|
|
83
|
-
rubygems_version: 3.
|
|
84
|
-
signing_key:
|
|
81
|
+
rubygems_version: 3.6.7
|
|
85
82
|
specification_version: 4
|
|
86
83
|
summary: URI handler for RFC6920 ni:/// URIs
|
|
87
84
|
test_files: []
|