Class: Zweifische::ISOIEC78164Padding

Inherits:
Object
  • Object
show all
Defined in:
lib/zweifische/iso_iec_7816_4_padding.rb

Class Method Summary collapse

Class Method Details

.pad(num) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
# File 'lib/zweifische/iso_iec_7816_4_padding.rb', line 3

def self.pad(num)
  raise ArgumentError.new("max pad is 16 bytes") if num > 15 || num < 0
  return "" if num == 0
  0x80.chr + (0x00.chr * (num - 1))
end

.unpad(text) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/zweifische/iso_iec_7816_4_padding.rb', line 9

def self.unpad(text)
  wpad = text.split("").reverse[0..15]
  count = wpad.each_with_index do |ch, idx|
    return text if ch != 0.chr && ch != 0x80.chr
    break idx if ch == 0x80.chr
  end
  text[0..(-1 * (count + 2))]
end