· 9 years ago · Nov 02, 2016, 03:52 AM
1```php
2function constructSignature($timestamp, $UID, $secretKey)
3{
4 // Construct a "base string" for signing
5 $baseString = $timestamp . "_" . $UID;
6 // Convert the base string into a binary array
7 $baseString = utf8_encode($baseString);
8 // Convert secretKey from BASE64 to a binary array
9 $binaryKey = base64_decode($secretKey);
10 // Use the HMAC-SHA1 algorithm to calculate the signature
11 $binarySignature = hash_hmac("sha1", utf8_encode($baseString), base64_decode($secretKey), true);
12 // Convert the signature to a BASE64
13 $signature = base64_encode($binarySignature);
14
15 return $signature;
16}
17
18function validateSignature($signature, $timestamp, $UID, $secretKey)
19{
20 $currentTime = time();
21 if (abs($currentTime - $timestamp) <= 180) {
22 return ($signature !== constructSignature($timestamp, $UID, $secretKey))
23 }
24 return false;
25}
26```