Module: WeixinRailsMiddleware::PKCS7Encoder

Extended by:
PKCS7Encoder
Included in:
PKCS7Encoder
Defined in:
lib/weixin_rails_middleware/helpers/pkcs7_encoder.rb

Constant Summary collapse

BLOCK_SIZE =
32

Instance Method Summary collapse

Instance Method Details

#decode(text) ⇒ Object



9
10
11
12
13
14
# File 'lib/weixin_rails_middleware/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) ⇒ Object

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



18
19
20
21
22
23
24
25
# File 'lib/weixin_rails_middleware/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