module YolSso::PKCS7Encoder

Constants

BLOCK_SIZE

Public Instance Methods

decode(text) click to toggle source
# File lib/yol_sso/helpers/pkcs7_encoder.rb, line 9
def decode(text)
  pad = text[-1].ord
  pad = 0 if (pad < 1 || pad > BLOCK_SIZE)
  size = text.size - pad
  text[0...size]
end
encode(text) click to toggle source

对需要加密的明文进行填充补位 返回补齐明文字符串

# File lib/yol_sso/helpers/pkcs7_encoder.rb, line 18
def encode(text)
  # 计算需要填充的位数
  amount_to_pad = BLOCK_SIZE - (text.length % BLOCK_SIZE)
  amount_to_pad = BLOCK_SIZE if amount_to_pad == 0
  # 获得补位所用的字符
  pad_chr = amount_to_pad.chr
  "#{text}#{pad_chr * amount_to_pad}"
end