Module: Wework::Cipher
- Extended by:
- ActiveSupport::Concern
- Included in:
- Api::Agent, Api::Corp, Api::Provider, Api::Suite
- Defined in:
- lib/wework/cipher.rb
Constant Summary collapse
- BLOCK_SIZE =
32- CIPHER =
'AES-256-CBC'.freeze
Instance Method Summary collapse
- #decrypt(msg, encoding_aes_key) ⇒ Object
- #encoding_aes_key ⇒ Object
- #encrypt(plain, encoding_aes_key) ⇒ Object
- #generate_xml(msg, timestamp, nonce) ⇒ Object
- #msg_decrypt(message) ⇒ Object
- #msg_encrypt(message) ⇒ Object
-
#pack(content, app_id) ⇒ Object
app_id or corp_id.
- #signature(timestamp, nonce, encrypt) ⇒ Object
- #token ⇒ Object
- #unpack(msg) ⇒ Object
Instance Method Details
#decrypt(msg, encoding_aes_key) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/wework/cipher.rb', line 34 def decrypt(msg, encoding_aes_key) cipher = OpenSSL::Cipher.new(CIPHER) cipher.decrypt cipher.padding = 0 key_data = Base64.decode64(encoding_aes_key + '=') cipher.key = key_data cipher.iv = [key_data].pack('H*') plain = cipher.update(msg) + cipher.final decode_padding(plain) end |
#encoding_aes_key ⇒ Object
18 19 20 |
# File 'lib/wework/cipher.rb', line 18 def encoding_aes_key @encoding_aes_key ||= [:encoding_aes_key] end |
#encrypt(plain, encoding_aes_key) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/wework/cipher.rb', line 22 def encrypt(plain, encoding_aes_key) cipher = OpenSSL::Cipher.new(CIPHER) cipher.encrypt cipher.padding = 0 key_data = Base64.decode64(encoding_aes_key + '=') cipher.key = key_data cipher.iv = [key_data].pack('H*') cipher.update(plain) + cipher.final end |
#generate_xml(msg, timestamp, nonce) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/wework/cipher.rb', line 79 def generate_xml(msg, , nonce) encrypt = msg_encrypt(msg) { Encrypt: encrypt, MsgSignature: signature(, nonce, encrypt), TimeStamp: , Nonce: nonce }.to_xml(root: 'xml', children: 'item', skip_instruct: true, skip_types: true) end |
#msg_decrypt(message) ⇒ Object
65 66 67 |
# File 'lib/wework/cipher.rb', line 65 def msg_decrypt unpack(decrypt(Base64.decode64(), encoding_aes_key))[0] end |
#msg_encrypt(message) ⇒ Object
69 70 71 |
# File 'lib/wework/cipher.rb', line 69 def msg_encrypt Base64.strict_encode64(encrypt(pack(, corp_id), encoding_aes_key)) end |
#pack(content, app_id) ⇒ Object
app_id or corp_id
48 49 50 51 52 53 54 |
# File 'lib/wework/cipher.rb', line 48 def pack(content, app_id) random = SecureRandom.hex(8) text = content.force_encoding('ASCII-8BIT') msg_len = [text.length].pack('N') encode_padding("#{random}#{msg_len}#{text}#{app_id}") end |
#signature(timestamp, nonce, encrypt) ⇒ Object
73 74 75 76 77 |
# File 'lib/wework/cipher.rb', line 73 def signature(, nonce, encrypt) array = [token, , nonce] array << encrypt unless encrypt.nil? Digest::SHA1.hexdigest array.compact.collect(&:to_s).sort.join end |
#token ⇒ Object
14 15 16 |
# File 'lib/wework/cipher.rb', line 14 def token @token ||= [:token] end |
#unpack(msg) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/wework/cipher.rb', line 56 def unpack(msg) msg = decode_padding(msg) msg_len = msg[16, 4].reverse.unpack('V')[0] content = msg[20, msg_len] app_id = msg[(20 + msg_len)..-1] [content, app_id] end |