签名nonce增加字符和特殊符号

This commit is contained in:
chejiulong 2023-03-07 16:50:12 +08:00
parent 62ad13a6b3
commit 44810361a0
3 changed files with 17 additions and 7 deletions

Binary file not shown.

10
main.go
View File

@ -190,17 +190,17 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request) {
} }
func verify_signature(signature string, nonce int64, timestamp int64, data interface{}) bool { func verify_signature(signature string, nonce string, timestamp int64, data interface{}) bool {
key := "qZ6v1&H#Wjx+yRm2D@*sJF$tnfL83Ia" key := "qZ6v1&H#Wjx+yRm2D@*sJF$tnfL83Ia"
fmt.Printf("Received signature: %s\n", signature) fmt.Printf("Received signature: %s\n", signature)
fmt.Printf("Received timestamp: %d\n", timestamp) fmt.Printf("Received timestamp: %d\n", timestamp)
fmt.Printf("Received nonce: %d\n", nonce) fmt.Printf("Received nonce: %s\n", nonce)
fmt.Printf("Received data: %v\n", data) fmt.Printf("Received data: %v\n", data)
received_signature := signature received_signature := signature
received_timestamp, _ := strconv.ParseInt(fmt.Sprintf("%v", timestamp), 10, 64) received_timestamp, _ := strconv.ParseInt(fmt.Sprintf("%v", timestamp), 10, 64)
received_nonce, _ := strconv.Atoi(fmt.Sprintf("%v", nonce)) received_nonce := nonce //strconv.Atoi(fmt.Sprintf("%v", nonce))
if time.Now().Unix()-received_timestamp > 7200 { if time.Now().Unix()-received_timestamp > 7200 {
fmt.Println("Timestamp expired") fmt.Println("Timestamp expired")
@ -209,7 +209,7 @@ func verify_signature(signature string, nonce int64, timestamp int64, data inter
received_data_bytes, _ := json.Marshal(data) received_data_bytes, _ := json.Marshal(data)
received_data := string(received_data_bytes) received_data := string(received_data_bytes)
expected_data := fmt.Sprintf("%d|%d|%s", received_timestamp, received_nonce, received_data) expected_data := fmt.Sprintf("%d|%s|%s", received_timestamp, received_nonce, received_data)
fmt.Printf("Expected data: %s\n", expected_data) fmt.Printf("Expected data: %s\n", expected_data)
mac := hmac.New(sha256.New, []byte(key)) mac := hmac.New(sha256.New, []byte(key))
@ -1203,5 +1203,5 @@ type TaskData struct {
type Signature struct { type Signature struct {
Signature string `json:"signature"` Signature string `json:"signature"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
Nonce int64 `json:"nonce"` Nonce string `json:"nonce"`
} }

View File

@ -2,9 +2,9 @@
function sign_message($data) { function sign_message($data) {
$timestamp = time(); $timestamp = time();
$nonce = mt_rand(100000, 999999); // 生成一个六位随机数 $nonce = generate_random_string(64); // 生成一个六位随机数,包含大小写字母和数字
$message = "$timestamp|$nonce|" . json_encode($data); $message = "$timestamp|$nonce|" . json_encode($data);
$key = 'qZ6v1&H#Wjx+yRm2D@*sJF$tnfL83Ia'; // 签名密钥,请自行设置 $key = 'qZ6v1&H#Wjx+yRm2D@*sJF$tnfL83Ia';
$signature = hash_hmac('sha256', $message, $key); $signature = hash_hmac('sha256', $message, $key);
$signed_message = array( $signed_message = array(
'TaskData' => $data, 'TaskData' => $data,
@ -17,6 +17,16 @@ function sign_message($data) {
return json_encode($signed_message); return json_encode($signed_message);
} }
function generate_random_string($length) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&*()_-+=';
$random_string = '';
for ($i = 0; $i < $length; $i++) {
$index = mt_rand(0, strlen($characters) - 1);
$random_string .= $characters[$index];
}
return $random_string;
}
$data = array( $data = array(
"command" => "lastCall", "command" => "lastCall",
"excluded_filename" => "lastCall.txt", "excluded_filename" => "lastCall.txt",