Class: XiWechatCorp::AesCrypt
- Inherits:
-
Object
- Object
- XiWechatCorp::AesCrypt
- Defined in:
- lib/xi_wechat_corp/aes_crypt.rb
Constant Summary collapse
- PKCS7 =
PKCS7Encoder.new
Instance Method Summary collapse
- #cipher_decrypt(text) ⇒ Object
- #cipher_encrypt(text) ⇒ Object
- #decode64(text) ⇒ Object
- #decrypt(text) ⇒ Object
- #encode64(text) ⇒ Object
- #encrypt(text) ⇒ Object
-
#initialize(aes_key, corp_id) ⇒ AesCrypt
constructor
A new instance of AesCrypt.
- #pack(text) ⇒ Object
- #pad(text) ⇒ Object
- #unpack(text) ⇒ Object
- #unpad(text) ⇒ Object
Constructor Details
#initialize(aes_key, corp_id) ⇒ AesCrypt
Returns a new instance of AesCrypt.
10 11 12 13 14 |
# File 'lib/xi_wechat_corp/aes_crypt.rb', line 10 def initialize(aes_key, corp_id) @aes_key = decode64(aes_key + '=') raise ArgumentError if @aes_key.size != 32 @corp_id = corp_id end |
Instance Method Details
#cipher_decrypt(text) ⇒ Object
61 62 63 |
# File 'lib/xi_wechat_corp/aes_crypt.rb', line 61 def cipher_decrypt(text) cipher(:decrypt, text) end |
#cipher_encrypt(text) ⇒ Object
57 58 59 |
# File 'lib/xi_wechat_corp/aes_crypt.rb', line 57 def cipher_encrypt(text) cipher(:encrypt, text) end |
#decode64(text) ⇒ Object
53 54 55 |
# File 'lib/xi_wechat_corp/aes_crypt.rb', line 53 def decode64(text) Base64.decode64(text) end |
#decrypt(text) ⇒ Object
20 21 22 |
# File 'lib/xi_wechat_corp/aes_crypt.rb', line 20 def decrypt(text) unpack unpad cipher_decrypt decode64 text end |
#encode64(text) ⇒ Object
49 50 51 |
# File 'lib/xi_wechat_corp/aes_crypt.rb', line 49 def encode64(text) Base64.strict_encode64(text) end |
#encrypt(text) ⇒ Object
16 17 18 |
# File 'lib/xi_wechat_corp/aes_crypt.rb', line 16 def encrypt(text) encode64 cipher_encrypt pad pack text end |
#pack(text) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/xi_wechat_corp/aes_crypt.rb', line 24 def pack(text) [ SecureRandom.random_bytes(16), [text.bytesize].pack('N'), text.force_encoding('ASCII-8BIT'), @corp_id ].join('') end |
#pad(text) ⇒ Object
41 42 43 |
# File 'lib/xi_wechat_corp/aes_crypt.rb', line 41 def pad(text) PKCS7.encode(text) end |
#unpack(text) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/xi_wechat_corp/aes_crypt.rb', line 33 def unpack(text) size, corp_id = text.unpack('@16Na*') output = corp_id.slice!(0, size) raise InvalidCorpIDError.new(corp_id) if corp_id != @corp_id output.force_encoding('UTF-8') end |
#unpad(text) ⇒ Object
45 46 47 |
# File 'lib/xi_wechat_corp/aes_crypt.rb', line 45 def unpad(text) PKCS7.decode(text) end |