Class: XiWechatCorp::PKCS7Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/xi_wechat_corp/pkcs7_encoder.rb

Overview

This file is modified from offical python example

http://qydev.weixin.qq.com/python.zip

Constant Summary collapse

BLOCK_SIZE =
32

Instance Method Summary collapse

Instance Method Details

#decode(decrypted) ⇒ Object

Raises:



20
21
22
23
24
25
26
# File 'lib/xi_wechat_corp/pkcs7_encoder.rb', line 20

def decode(decrypted)
  return decrypted if decrypted.nil? || decrypted.empty?

  pad = decrypted[-1].ord
  raise PKCS7DecodeError if pad < 1 or pad > BLOCK_SIZE
  decrypted.byteslice(0...-pad)
end

#encode(text) ⇒ Object

Pad the text so the length is divisable by BLOCK_SIZE. Save the padding length as padding chracter itself.



11
12
13
14
15
16
17
18
# File 'lib/xi_wechat_corp/pkcs7_encoder.rb', line 11

def encode(text)
  return 32.chr * BLOCK_SIZE if text.nil? || text.empty?

  text_length = text.bytesize
  amount_to_pad = BLOCK_SIZE - (text_length % BLOCK_SIZE)
  pad = amount_to_pad.chr
  text + pad * amount_to_pad
end