Class: PQCrypto::Seal::Padding::BucketsPolicy

Inherits:
Policy
  • Object
show all
Defined in:
lib/pq_crypto/seal/padding.rb

Instance Attribute Summary collapse

Attributes inherited from Policy

#id

Instance Method Summary collapse

Methods inherited from Policy

#verify!

Constructor Details

#initialize(values) ⇒ BucketsPolicy

Returns a new instance of BucketsPolicy.



95
96
97
98
99
100
101
# File 'lib/pq_crypto/seal/padding.rb', line 95

def initialize(values)
  @buckets = Array(values).map { |value| Integer(value) }.uniq.sort.freeze
  raise InvalidConfigurationError, "padding buckets must not be empty" if @buckets.empty?
  raise InvalidConfigurationError, "padding buckets must be non-negative" if @buckets.first.negative?

  super(Format::PADDING_BUCKETS)
end

Instance Attribute Details

#bucketsObject (readonly)

Returns the value of attribute buckets.



93
94
95
# File 'lib/pq_crypto/seal/padding.rb', line 93

def buckets
  @buckets
end

Instance Method Details

#target(base_length) ⇒ Object



103
104
105
106
107
108
# File 'lib/pq_crypto/seal/padding.rb', line 103

def target(base_length)
  bucket = buckets.bsearch { |value| value >= Integer(base_length) }
  raise InvalidConfigurationError, "no padding bucket can hold envelope" unless bucket

  bucket
end