我正在尝试调用由定时哈希验证系统加密的 Web api。
我可以使用 python 调用该 API
import hmac
import hashlib
import time
import requests
import json
def generate_timed_hash(secret_key, data):
timestamp = str(int(time.time())) # Current Unix timestamp
message = data + timestamp
hash_value = hmac.new(secret_key.encode(), message.encode(), hashlib.sha256).hexdigest()
return hash_value, timestamp
def testApi():
data = json.dumps({'userId': 'c088ab7f-dd04-4836-93cd-7ab2843db971'})
secret_key = 'mysecret'
hash_value, timestamp = generate_timed_hash(secret_key, data)
headers = {
'X-Timestamp': timestamp,
'X-Hash': hash_value,
}
response = requests.post('https://some-host/api/secured-plan-detail/', data={'data': data}, headers=headers)
print(response.json())
testApi()
llSHA256String
用于生成 SHA256 字符串。但仍然无法找出翻译 python 代码的方法。
这是我尝试进行http调用。
default
{
state_entry()
{
llHTTPRequest(
"https://some-host/api/secured-plan-detail/",
[
HTTP_METHOD, "POST",
HTTP_MIMETYPE, "application/json"
//HTTP_CUSTOM_HEADER, "X-Timestamp:", timestamp
//HTTP_CUSTOM_HEADER, "X-Hash:", hash_value
],
llList2Json(JSON_OBJECT, ["userId", "c088ab7f-dd04-4836-93cd-7ab2843db971"])
);
}
http_response(key request_id, integer status, list metadata, string body) {
llOwnerSay((string)status);
llOwnerSay("response: " + body);
}
}
如何在 Lindane 脚本语言中进行这样的调用?
这对我有用。
default
{
state_entry()
{
string time = llGetUnixTime();
string data = llList2Json(JSON_OBJECT, ["userId", "c088ab7f-dd04-4836-93cd-7ab2843db971"]);
string PASS = "mysecret";
string message = PASS + data + (string)time;
string hash = llSHA256String(message);
string url = "https://some-host/api/secured-plan-detail";
llHTTPRequest(
url,
[
HTTP_METHOD, "POST",
HTTP_MIMETYPE, "application/json",
HTTP_CUSTOM_HEADER, "X-Timestamp", (string)time,
HTTP_CUSTOM_HEADER, "X-Hash", hash
],
llList2Json(JSON_OBJECT, ["data", data])
);
}
http_response(key request_id, integer status, list metadata, string body) {
llOwnerSay((string)status);
llOwnerSay("response: " + body);
}
}