uri-ni 0.2.0 → 0.2.2

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 +51 -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: 0210a945c627b9ec4a14f96b5beac95ca2084e5a873643ed963d37f939f81796
4
+ data.tar.gz: d3c7a53b7c0dfb92e64e2989114863838dbf2b895a9fb0a4375f86213fd61d7d
5
5
  SHA512:
6
- metadata.gz: 22026ee328bbd59155fa3c0ab1b85d7565f868484d7a4454c5c45aacb6ea31d8f0482b216d59b46a15a460089f896c775cb62f1cb540755e6cef96732a01977d
7
- data.tar.gz: 7c101253e23585c8045571bf4d00fd42deca06339cfe71bd9d93001d1d7138fe9eff99e4f899cb5344bf2adafc411ff8cb43e4413133eb1fe6dcce524730691b
6
+ metadata.gz: e02348cfcdb25c10a8ff83c462bfff0c3720b1488bedc33a3e8735eb1a98f3887afd50ca5d6f6674f30fd2ae92e3f5bf49822dbcc2ee6c1ee51385e55612ea36
7
+ data.tar.gz: 4f6a9d14efab6c9b6fa46c9df390d5222ef8a8a6afe4d06aeba6709284d59f20d35aa12d5da4f8f3dc52cb2a159dd17f97872165278324b0407b20f8e4280bd7
@@ -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.2"
7
7
  end
8
8
 
9
9
  # might as well put this here
data/lib/uri/ni.rb CHANGED
@@ -42,9 +42,25 @@ 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.freeze
62
+
63
+ TRUNCATED = [32, 64, 96, 120, 128].map { |i| "sha-256-#{i}".to_sym }.freeze
48
64
 
49
65
  # resolve first against digest length and then class
50
66
  DIGEST_REV = {
@@ -261,7 +277,8 @@ class URI::NI < URI::Generic
261
277
  # special case for when the data is a digest
262
278
  ctx = nil
263
279
  if data.is_a? Digest::Instance
264
- algorithm ||= algo_for data, algorithm
280
+ # note the self; this would have been a bug lol
281
+ self.algorithm = algorithm ||= algo_for data, algorithm
265
282
  ctx = data
266
283
  data = nil # unset data
267
284
  else
@@ -296,17 +313,34 @@ class URI::NI < URI::Generic
296
313
  block.call ctx, nil
297
314
  end
298
315
 
299
- self.set_path("/#{algorithm};" +
300
- ctx.base64digest.tr('+/', '-_').tr('=', ''))
316
+ set_digest ctx.digest
317
+
301
318
  self
302
319
  end
303
320
 
321
+ # Returns true if the digest length matches the algorithm.
322
+ #
323
+ # @return [false, true]
324
+ #
325
+ def valid?
326
+ LENGTHS[algorithm] and LENGTHS[algorithm] == digest.length
327
+ end
328
+
329
+ # Returns true if the algorithm is a truncated one.
330
+ #
331
+ # @return [false, true]
332
+ #
333
+ def truncated?
334
+ TRUNCATED.include? algorithm
335
+ end
336
+
304
337
  # Display the available algorithms.
305
338
  #
306
339
  # @return [Array] containing the symbols representing the available
307
340
  # digest algorithms.
308
- def self.algorithms
309
- DIGESTS.keys.sort
341
+ def self.algorithms truncated: false
342
+ out = DIGESTS.keys.sort
343
+ truncated ? out : out.reject { |a| /^sha-256-/.match? a }
310
344
  end
311
345
 
312
346
  # Obtain the algorithm of the digest. May be nil.
@@ -384,7 +418,10 @@ class URI::NI < URI::Generic
384
418
  when Digest::Instance
385
419
  compute value
386
420
  when String
387
- value = transcode value, from: radix, to: 64
421
+ value = transcode value, from: radix
422
+ value = value[0, LENGTHS[a.to_sym]] # deal with truncation
423
+ value = transcode value, to: 64
424
+
388
425
  self.path = a ? "/#{a};#{value}" : "/;#{value}"
389
426
  when nil
390
427
  self.path = a ? "/#{a}" : ?/
@@ -530,16 +567,17 @@ class URI::NI < URI::Generic
530
567
  to_www https: false, authority: authority
531
568
  end
532
569
 
533
- # Returns the set of supported algorithms.
534
- # @return [Array] the algorithms
535
- def self.algorithms
536
- DIGESTS.keys
537
- end
538
570
 
539
571
  # Returns true if the algorithm is supported.
540
572
  # @param algorithm [Symbol,String] the algorithm identifier to test
541
573
  # @return [true, false] whether it is supported
542
574
  def self.valid_algo? algorithm
543
- DIGESTS.has_key? algorithm.to_s.downcase.to_sym
575
+ algorithm = algorithm.to_s.downcase.to_sym
576
+
577
+ # special case for truncated sha-256
578
+ return true if /^(sha-256)-(32|64|96|120|128)$/.match? algorithm
579
+
580
+ # etc
581
+ DIGESTS.has_key? algorithm
544
582
  end
545
583
  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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Taylor