Skip to content

Commit

Permalink
完善python3支持
Browse files Browse the repository at this point in the history
  • Loading branch information
gusibi committed May 1, 2017
1 parent ffa7990 commit f5b6c53
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
5 changes: 3 additions & 2 deletions weixin/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
_always_safe = (b'abcdefghijklmnopqrstuvwxyz'
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-+')

safe_char = _always_safe

error_dict = {
'AppID 参数错误': {
Expand Down Expand Up @@ -236,8 +237,8 @@ def genarate_js_signature(params):

def genarate_signature(params):
sorted_params = sorted([v for k, v in params.items()])
params_str = ''.join(sorted_params)
return sha1(params_str).hexdigest()
params_str = smart_str(''.join(sorted_params))
return sha1(str(params_str).encode('utf-8')).hexdigest()


def get_encoding(html=None, headers=None):
Expand Down
22 changes: 12 additions & 10 deletions weixin/lib/WXBizMsgCrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from imp import reload

import base64
import string
import random
import hashlib
import time
Expand All @@ -18,9 +17,12 @@
import xml.etree.cElementTree as ET
import sys
import socket

reload(sys)

from .import *
from .ierror import *

from weixin.helper import smart_str, safe_char


class FormatException(Exception):
Expand All @@ -47,7 +49,7 @@ def getSHA1(self, token, timestamp, nonce, encrypt):
sortlist = [token, timestamp, nonce, encrypt]
sortlist.sort()
sha = hashlib.sha1()
sha.update("".join(sortlist))
sha.update(str("".join(sortlist)).encode('utf-8'))
return WXBizMsgCrypt_OK, sha.hexdigest()
except Exception:
return WXBizMsgCrypt_ComputeSignature_Error, None
Expand Down Expand Up @@ -142,8 +144,8 @@ def encrypt(self, text, appid):
@return: 加密得到的字符串
"""
# 16位随机字符串添加到明文开头
text = self.get_random_str() + struct.pack(
"I", socket.htonl(len(text))) + text + appid
pack_str = smart_str(struct.pack("I", socket.htonl(len(text))))
text = self.get_random_str() + pack_str + text + appid
# 使用自定义的填充方式对明文进行补位填充
pkcs7 = PKCS7Encoder()
text = pkcs7.encode(text)
Expand All @@ -168,15 +170,15 @@ def decrypt(self, text, appid):
except Exception:
return WXBizMsgCrypt_DecryptAES_Error, None
try:
pad = ord(plain_text[-1])
pad = plain_text[-1]
# 去掉补位字符串
# pkcs7 = PKCS7Encoder()
# plain_text = pkcs7.encode(plain_text)
# 去除16位随机字符串
content = plain_text[16:-pad]
xml_len = socket.ntohl(struct.unpack("I", content[:4])[0])
xml_content = content[4:xml_len+4]
from_appid = content[xml_len+4:]
from_appid = smart_str(content[xml_len+4:])
except Exception:
return WXBizMsgCrypt_IllegalBuffer, None
if from_appid != appid:
Expand All @@ -187,9 +189,9 @@ def get_random_str(self):
""" 随机生成16位字符串
@return: 16位字符串
"""
rule = string.letters + string.digits
str = random.sample(rule, 16)
return "".join(str)
str_list = random.sample(safe_char[:-4], 16)
sl = [chr(s) for s in str_list]
return "".join(sl)


class WXBizMsgCrypt(object):
Expand Down

0 comments on commit f5b6c53

Please sign in to comment.