uri-ni 0.2.0 → 0.2.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/uri/ni/version.rb +1 -1
  3. data/lib/uri/ni.rb +41 -13
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46e2aa7887a91a2d14dd3f89a6a01e91ff00f6c966ba9256e256597d221d5b63
4
- data.tar.gz: e1b4bf407d8c8f5b0a1643ffd6ffd3e14a7389098a0a9c1253e8cbd733003116
3
+ metadata.gz: c7f97e35f7fc6987d8051e1f09e23cc2cbc29b6e188496b5d1abd5d030a7ec32
4
+ data.tar.gz: 3a66e3863206978a52e1281318808f043de2b7e4409b09d8bdff37a589bfb3fd
5
5
  SHA512:
6
- metadata.gz: 22026ee328bbd59155fa3c0ab1b85d7565f868484d7a4454c5c45aacb6ea31d8f0482b216d59b46a15a460089f896c775cb62f1cb540755e6cef96732a01977d
7
- data.tar.gz: 7c101253e23585c8045571bf4d00fd42deca06339cfe71bd9d93001d1d7138fe9eff99e4f899cb5344bf2adafc411ff8cb43e4413133eb1fe6dcce524730691b
6
+ metadata.gz: eac81311c6e7e895c908b0d45db5a7b028ed1e9b2e1b65e35d264c5840837abc589a9c11ac315272bf751f1f326b753f1b5e2f1add14313ae66258eefeee7c88
7
+ data.tar.gz: 8456780c695e1ad060546e46bf8cc1fcccbee00c1d535e6a3845f19f2928fd1a13efb720e264f23091fcf24b82bae797365877ca3747d6701502f96df61c65ca
@@ -3,7 +3,7 @@ require 'uri/generic'
3
3
 
4
4
  module URI
5
5
  class NI < Generic
6
- VERSION = "0.2.0"
6
+ VERSION = "0.2.1"
7
7
  end
8
8
 
9
9
  # might as well put this here
data/lib/uri/ni.rb CHANGED
@@ -42,9 +42,23 @@ class URI::NI < URI::Generic
42
42
  "sha-256": Digest::SHA256,
43
43
  "sha-384": Digest::SHA384,
44
44
  "sha-512": Digest::SHA512,
45
+ # XXX not sure if i want to do this
46
+ "sha-256-32": Digest::SHA256,
47
+ "sha-256-64": Digest::SHA256,
48
+ "sha-256-96": Digest::SHA256,
49
+ "sha-256-120": Digest::SHA256,
50
+ "sha-256-128": Digest::SHA256,
45
51
  }
46
52
 
47
- LENGTHS = DIGESTS.transform_values { |v| v.new.digest_length }
53
+ LENGTHS = DIGESTS.map do |k, v|
54
+ v = if m = /^sha-256-(\d+)$/.match(k)
55
+ m.captures.first.to_i / 8
56
+ else
57
+ v.new.digest_length
58
+ end
59
+
60
+ [k, v]
61
+ end.to_h
48
62
 
49
63
  # resolve first against digest length and then class
50
64
  DIGEST_REV = {
@@ -261,7 +275,8 @@ class URI::NI < URI::Generic
261
275
  # special case for when the data is a digest
262
276
  ctx = nil
263
277
  if data.is_a? Digest::Instance
264
- algorithm ||= algo_for data, algorithm
278
+ # note the self; this would have been a bug lol
279
+ self.algorithm = algorithm ||= algo_for data, algorithm
265
280
  ctx = data
266
281
  data = nil # unset data
267
282
  else
@@ -296,17 +311,26 @@ class URI::NI < URI::Generic
296
311
  block.call ctx, nil
297
312
  end
298
313
 
299
- self.set_path("/#{algorithm};" +
300
- ctx.base64digest.tr('+/', '-_').tr('=', ''))
314
+ set_digest ctx.digest
315
+
301
316
  self
302
317
  end
303
318
 
319
+ # Returns true if the digest length matches the algorithm.
320
+ #
321
+ # @return [false, true]
322
+ #
323
+ def valid?
324
+ LENGTHS[algorithm] and LENGTHS[algorithm] == digest.length
325
+ end
326
+
304
327
  # Display the available algorithms.
305
328
  #
306
329
  # @return [Array] containing the symbols representing the available
307
330
  # digest algorithms.
308
- def self.algorithms
309
- DIGESTS.keys.sort
331
+ def self.algorithms truncated: false
332
+ out = DIGESTS.keys.sort
333
+ truncated ? out : out.reject { |a| /^sha-256-/.match? a }
310
334
  end
311
335
 
312
336
  # Obtain the algorithm of the digest. May be nil.
@@ -384,7 +408,10 @@ class URI::NI < URI::Generic
384
408
  when Digest::Instance
385
409
  compute value
386
410
  when String
387
- value = transcode value, from: radix, to: 64
411
+ value = transcode value, from: radix
412
+ value = value[0, LENGTHS[a.to_sym]] # deal with truncation
413
+ value = transcode value, to: 64
414
+
388
415
  self.path = a ? "/#{a};#{value}" : "/;#{value}"
389
416
  when nil
390
417
  self.path = a ? "/#{a}" : ?/
@@ -530,16 +557,17 @@ class URI::NI < URI::Generic
530
557
  to_www https: false, authority: authority
531
558
  end
532
559
 
533
- # Returns the set of supported algorithms.
534
- # @return [Array] the algorithms
535
- def self.algorithms
536
- DIGESTS.keys
537
- end
538
560
 
539
561
  # Returns true if the algorithm is supported.
540
562
  # @param algorithm [Symbol,String] the algorithm identifier to test
541
563
  # @return [true, false] whether it is supported
542
564
  def self.valid_algo? algorithm
543
- DIGESTS.has_key? algorithm.to_s.downcase.to_sym
565
+ algorithm = algorithm.to_s.downcase.to_sym
566
+
567
+ # special case for truncated sha-256
568
+ return true if /^(sha-256)-(32|64|96|120|128)$/.match? algorithm
569
+
570
+ # etc
571
+ DIGESTS.has_key? algorithm
544
572
  end
545
573
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uri-ni
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Taylor