roputil.py 3.15 KB
import hashlib
import uuid
import requests

class RopUtils:

    @staticmethod
    def sign(param_values, ignore_param_names=None, secret=""):
        try:
            param_names = sorted(param_values.keys())
            
            if ignore_param_names:
                param_names = [name for name in param_names if name not in ignore_param_names]
            
            data_to_sign = [secret]
            data_to_sign.extend(f"{name}{param_values[name]}" for name in param_names)
            data_to_sign.append(secret)
            
            data_to_sign_str = ''.join(data_to_sign)
            
            sha1_digest = RopUtils.get_sha1_digest(data_to_sign_str.encode("utf-8"))
            
            return RopUtils.byte2hex(sha1_digest)
        except Exception as e:
            raise Exception(e)

    @staticmethod
    def utf8_encoding(value, source_charset_name):
        try:
            return value.encode(source_charset_name).decode("utf-8")
        except Exception as e:
            raise ValueError(e)

    @staticmethod
    def get_sha1_digest(data):
        try:
            sha1_digest = hashlib.sha1(data).digest()
            return sha1_digest
        except Exception as e:
            raise ValueError(e)

    @staticmethod
    def get_md5_digest(data):
        try:
            md5_digest = hashlib.md5(data).digest()
            return md5_digest
        except Exception as e:
            raise ValueError(e)

    @staticmethod
    def byte2hex(data):
        return ''.join(f"{byte:02X}" for byte in data)

    @staticmethod
    def get_uuid():
        return str(uuid.uuid4()).upper()
    
    @staticmethod
    def invokerEquityServer(orderNo):
        param_values = {"appKey": "48e5e13229b82c1b4e6e8c96151f0637", "format": "json", "locale": "zh_CN", "method": "scrm.api.equity.car.service.get.by.id", "v": "1.0.0"}
        param_values['id'] = '12'

        secret_key = "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b"
        ignore_param_names = []
        sign = RopUtils.sign(param_values, ignore_param_names, secret_key)
        param_values['sign'] = sign

        serverUrl = 'https://scrm.test.glsx.com.cn/rop-scrm-equity/router'
        try:
            #response = requests.get(serverUrl, params=param_values)
            print("Generated Signature:", serverUrl)
            print("Generated Signature:", param_values)
            response = requests.post(serverUrl, data=param_values)
            print(response)
            # 检查响应状态码
            if response.status_code == 200:
                return response.text
            else:
                return f"Error: {response.status_code}"
        except Exception as e:
            return f"Exception: {str(e)}"


        

# 示例用法
param_values = {"param1": "value1", "param2": "value2", "param3": "value3"}
ignore_param_names = ["param2"]
secret_key = "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b"

signature = RopUtils.sign(param_values, ignore_param_names, secret_key)
print("Generated Signature:", signature)

# 其他方法的使用方式也类似,根据需要调用相应的方法
orderNo = 12
response = RopUtils.invokerEquityServer(orderNo)
print("response text:", response)