Request header authentication, encryption request parameters, return value for decrypting

(1) When the test interface, the test case written interfaces, and analog when requested, will request authentication header. Give you the key value you can

 

import hashlib 
import time
import base64


def get_sha1 (str_data)
sha1_str hashlib.sha1 = (str (str_data)). hexdigest ()
print sha1_str
return sha1_str


def get_md5 (IMSI)
imsi_md5 hashlib.md5 = ()
imsi_md5.update (str ( IMSI))
imsi_md5_str imsi_md5.hexdigest = ()
print imsi_md5_str
return imsi_md5_str



the authenticate DEF (owner_id = "", api_id = "", api_key = ""):
the time.sleep (. 1)
header = {}
TIME_STAMP STR = (int (the time.time ()))
sign_str = STR (api_id) + STR (api_key) + TIME_STAMP
Sign = hashlib.sha1 (sign_str.encode ( "UTF8")). hexdigest ()
token_str = ",." the Join ([STR (owner_id), STR (api_id), TIME_STAMP, Sign])
token = base64.b64encode (token_str.encode ( "ASCII")). decode ( "UTF-. 8")
header [ "the Authorization"] = "Bearer {0}." the format (token)
return header



described above is a value sha and encryption for obtaining the value of the following function is md5 request header authentication token acquired in







(2) encryption request parameter:

first, to write the encryption method and decryption method:


from Crypto.Cipher import AES
from Crypto import Random
import urllib


class AESCipher:
def __init__(self, key):
self.key = key.decode("base64")
print key

def encrypt(self, raw):
"""
Returns hex encoded encrypted value!
"""
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) # 对要加密的内容按16位进行补位

iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_ECB, iv)
raw = pad(raw)
return str(cipher.encrypt(raw)).encode("base64").strip()

def decrypt(self, enc):
"""
Param hex encoded to the decrypt the Requires
"" "
ENC = urllib.unquote (ENC) .decode ( 'UTF-. 8') # special characters (+ = ..) converting at
ENC = enc.decode (" Base64 ")
IV = ENC [: 16]
cipher = AES.new (self.key, AES.MODE_ECB, IV)
dec_str = cipher.decrypt (ENC) .strip ( '\ X10')
return dec_str



encryption and decryption functions which packaging is completed then the actual call on the line: certainly the encrypted key value
key = ""
asc = aes_cipher.AESCipher(key=key)
enc_str = asc.encrypt (json.dumps (data) ) when the requested data = base64.b64decode (enc_str) 
to be about base64


This encrypted request parameters according to the key value of


the result is the same decryption


= ret.encode for_bs ( 'Base64') 
aes = aes_cipher.AESCipher (decry_key)
dec = aes.decrypt (for_bs)


Well, the share is completed, try it!







Guess you like

Origin www.cnblogs.com/testling/p/11822300.html