Class: CZTop::Z85::Padded
- Inherits:
-
CZTop::Z85
- Object
- CZTop::Z85
- CZTop::Z85::Padded
- Defined in:
- lib/cztop/z85/padded.rb
Overview
Warning: Z85 doesn’t have a standardized padding procedure. So other implementations won’t automatically recognize and chop off the padding. Only use this if you really need padding, like when you can’t guarantee the input for #encode is always a multiple of 4 bytes.
Z85 with simple padding. This allows you to #encode input of any length.
Padding Scheme
If the data to be encoded is empty (0 bytes), it is encoded to the empty string, just like in Z85.
Otherwise, a small padding sequence of 1 to 4 (identical) bytes is appended to the binary data. Its last byte denotes the number of padding bytes. This padding is done even if the length of the binary input is a multiple of 4 bytes. This is similar to PKCS#7 padding.
+----------------------------------------+------------+
| binary data | padding |
| any number of bytes | 1-4 bytes |
+----------------------------------------+------------+
The resulting blob is encoded using #encode.
When decoding, #decode does the inverse. After decoding, it checks the last byte to determine the number of bytes of padding used, and chops those off.
Instance Attribute Summary
Attributes included from HasFFIDelegate
Class Method Summary collapse
-
.decode(input) ⇒ String
Same as #decode, but without the need to create an instance first.
-
.encode(input) ⇒ String
Same as #encode, but without the need to create an instance first.
Instance Method Summary collapse
-
#decode(input) ⇒ String
Decodes from Z85 with padding.
-
#encode(input) ⇒ String
Encododes to Z85, with padding if needed.
Methods inherited from CZTop::Z85
Methods included from HasFFIDelegate::ClassMethods
#ffi_delegate, #from_ffi_delegate
Methods included from HasFFIDelegate
#attach_ffi_delegate, #from_ffi_delegate, raise_zmq_err, #to_ptr
Constructor Details
This class inherits a constructor from CZTop::Z85
Class Method Details
.decode(input) ⇒ String
Same as #decode, but without the need to create an instance first.
56 57 58 |
# File 'lib/cztop/z85/padded.rb', line 56 def decode(input) default.decode(input) end |
.encode(input) ⇒ String
Same as #encode, but without the need to create an instance first.
44 45 46 |
# File 'lib/cztop/z85/padded.rb', line 44 def encode(input) default.encode(input) end |
Instance Method Details
#decode(input) ⇒ String
Decodes from Z85 with padding.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/cztop/z85/padded.rb', line 96 def decode(input) return super if input.empty? decoded = super # last byte contains number of padding bytes padding_bytes = decoded.byteslice(-1).ord decoded.byteslice(0...-padding_bytes) end |
#encode(input) ⇒ String
Encododes to Z85, with padding if needed.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/cztop/z85/padded.rb', line 75 def encode(input) return super if input.empty? padding_bytes = 4 - (input.bytesize % 4) # if 0, make it 4. we MUST append padding. padding_bytes = 4 if padding_bytes.zero? # generate and append padding padding = [padding_bytes].pack('C') * padding_bytes super("#{input}#{padding}") end |