module Lockbox::Padding
Constants
- PAD_FIRST_BYTE
- PAD_ZERO_BYTE
Public Instance Methods
pad(str, **options)
click to toggle source
# File lib/lockbox/padding.rb, line 6 def pad(str, **options) pad!(str.dup, **options) end
pad!(str, size: 16)
click to toggle source
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
# File lib/lockbox/padding.rb, line 20 def pad!(str, size: 16) raise ArgumentError, "Invalid size" if size < 1 str.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, **options)
click to toggle source
# File lib/lockbox/padding.rb, line 10 def unpad(str, **options) unpad!(str.dup, **options) end
unpad!(str, size: 16)
click to toggle source
note: current implementation does not try to minimize side channels
# File lib/lockbox/padding.rb, line 38 def unpad!(str, size: 16) raise ArgumentError, "Invalid size" if size < 1 str.force_encoding(Encoding::BINARY) i = 1 while i <= size case str[-i] when PAD_ZERO_BYTE i += 1 when PAD_FIRST_BYTE str.slice!(-i..-1) return str else break end end raise Lockbox::PaddingError, "Invalid padding" end