Module: CryptBufferConcern::Padding

Included in:
CryptBuffer
Defined in:
lib/crypto-toolbox/crypt_buffer/concerns/padding.rb

Instance Method Summary collapse

Instance Method Details

#pad(n, replace: true) ⇒ Object

pad an existing buffer with the given amount of bytes If a padding already exists, replace: decides whether or not to replace it



44
45
46
47
48
49
50
51
# File 'lib/crypto-toolbox/crypt_buffer/concerns/padding.rb', line 44

def pad(n,replace: true)
  if padding? && replace
      strip_padding.pad(n)
  else
    pad = [n] * n
    return CryptBuffer(bytes + pad )
  end
end

#paddingObject

Return any existing padding



13
14
15
16
17
18
19
20
21
22
# File 'lib/crypto-toolbox/crypt_buffer/concerns/padding.rb', line 13

def padding
  last   = bytes.last
  subset = subset_padding

  if subset.all?{|e| e == last }
    self.class.new(subset)
  else
    self.class.new([])
  end
end

#padding?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/crypto-toolbox/crypt_buffer/concerns/padding.rb', line 37

def padding?
  !padding.empty?
end

#strip_paddingObject

Strip the existing padding if present



25
26
27
28
29
30
31
32
33
34
# File 'lib/crypto-toolbox/crypt_buffer/concerns/padding.rb', line 25

def strip_padding
  subset = bytes
  
  if padding?
    pad = padding
    len = pad.length
    subset = bytes[0,bytes.length - len]
  end
  self.class.new(subset)
end