Module: PQCrypto::Seal::Padding
- Defined in:
- lib/pq_crypto/seal/padding.rb
Class Method Summary collapse
- .fixed_or_buckets(base_length, policy) ⇒ Object
- .padme_target(length) ⇒ Object
- .target(base_length, policy) ⇒ Object
Class Method Details
.fixed_or_buckets(base_length, policy) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pq_crypto/seal/padding.rb', line 32 def fixed_or_buckets(base_length, policy) if policy.key?(:to) target = Integer(policy.fetch(:to)) raise InvalidConfigurationError, "padding target is smaller than envelope" if target < base_length target elsif policy.key?(:buckets) bucket = Array(policy.fetch(:buckets)).map { |n| Integer(n) }.sort.find { |n| n >= base_length } raise InvalidConfigurationError, "no padding bucket can hold envelope" unless bucket bucket else raise InvalidConfigurationError, "padding hash must contain :to or :buckets" end end |
.padme_target(length) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/pq_crypto/seal/padding.rb', line 8 def padme_target(length) n = Integer(length) raise ArgumentError, "length must be non-negative" if n.negative? return n if n <= 1 exponent = n.bit_length - 1 significant = exponent.bit_length low_bits = exponent - significant return n if low_bits <= 0 mask = (1 << low_bits) - 1 (n + mask) & ~mask end |
.target(base_length, policy) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/pq_crypto/seal/padding.rb', line 22 def target(base_length, policy) case policy when nil, :none then base_length when :padme then padme_target(base_length) when Hash then fixed_or_buckets(base_length, policy) else raise InvalidConfigurationError, "unsupported padding policy: #{policy.inspect}" end end |