Class: PCO::URL::Encryption::Chunk
- Inherits:
-
Object
- Object
- PCO::URL::Encryption::Chunk
- Defined in:
- lib/pco/url/encryption.rb
Instance Method Summary collapse
-
#decode ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#encode ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#initialize(bytes) ⇒ Chunk
constructor
A new instance of Chunk.
Constructor Details
#initialize(bytes) ⇒ Chunk
Returns a new instance of Chunk.
38 39 40 |
# File 'lib/pco/url/encryption.rb', line 38 def initialize(bytes) @bytes = bytes end |
Instance Method Details
#decode ⇒ Object
rubocop:disable Metrics/AbcSize
42 43 44 45 46 47 48 49 |
# File 'lib/pco/url/encryption.rb', line 42 def decode # rubocop:disable Metrics/AbcSize bytes = @bytes.take_while { |c| c != 61 } # strip padding bytes = bytes.find_all { |b| !TABLE.index(b.chr).nil? } # remove invalid characters n = (bytes.length * 5.0 / 8.0).floor p = bytes.length < 8 ? 5 - (n * 8) % 5 : 0 c = bytes.inject(0) { |m, o| (m << 5) + TABLE.index(o.chr) } >> p (0..(n - 1)).to_a.reverse.collect { |i| ((c >> i * 8) & 0xff).chr } end |
#encode ⇒ Object
rubocop:disable Metrics/AbcSize
51 52 53 54 55 56 57 58 59 |
# File 'lib/pco/url/encryption.rb', line 51 def encode # rubocop:disable Metrics/AbcSize n = (@bytes.length * 8.0 / 5.0).ceil p = n < 8 ? 5 - (@bytes.length * 8) % 5 : 0 c = @bytes.inject(0) { |m, o| (m << 8) + o } << p [ (0..(n - 1)).to_a.reverse.collect { |i| TABLE[(c >> i * 5) & 0x1f].chr }, ("=" * (8 - n)) ] end |