Module: Lockbox::Padding
- Included in:
- Lockbox
- Defined in:
- lib/lockbox/padding.rb
Constant Summary collapse
- PAD_FIRST_BYTE =
"\x80".b
- PAD_ZERO_BYTE =
"\x00".b
Instance Method Summary collapse
-
#pad(str, size: 16) ⇒ Object
ISO/IEC 7816-4 same as Libsodium libsodium.gitbook.io/doc/padding apply prior to encryption note: current implementation does not try to minimize side channels.
-
#unpad(str, size: 16) ⇒ Object
note: current implementation does not try to minimize side channels.
Instance Method Details
#pad(str, size: 16) ⇒ Object
ISO/IEC 7816-4 same as Libsodium libsodium.gitbook.io/doc/padding apply prior to encryption note: current implementation does not try to minimize side channels
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/lockbox/padding.rb', line 12 def pad(str, size: 16) raise ArgumentError, "Invalid size" if size < 1 str = str.dup.force_encoding(Encoding::BINARY) pad_length = size - 1 pad_length -= str.bytesize % size str << PAD_FIRST_BYTE pad_length.times do str << PAD_ZERO_BYTE end str end |
#unpad(str, size: 16) ⇒ Object
note: current implementation does not try to minimize side channels
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lockbox/padding.rb', line 30 def unpad(str, size: 16) raise ArgumentError, "Invalid size" if size < 1 if str.encoding != Encoding::BINARY str = str.dup.force_encoding(Encoding::BINARY) end i = 1 while i <= size case str[-i] when PAD_ZERO_BYTE i += 1 when PAD_FIRST_BYTE return str[0..-(i + 1)] else break end end raise Lockbox::PaddingError, "Invalid padding" end |