任何人都可以描述我如何使用开发人员密钥、应用程序 ID 和用户名在 php 中生成令牌。Videyo 只提供 html js,他们没有任何对 php 的支持。请提示我。令牌长度为 200 以上。
问问题
875 次
3 回答
5
这是生成令牌的示例 php 代码。不要忘记将 $DEV_KEY 和 $APP_ID 替换为您的开发者密钥和应用程序 ID,您可以在https://developer.vidyo.io/dashboard上找到它们。对于用户名,不要添加任何特殊字符,尤其是“@”符号。该代码使用“@”将 appId 附加到用户名,因此如果在用户名中使用它会弄乱应用程序 ID 的解码。对于更短或更长的令牌持续时间,请更改 $expiresInSecs 值。
<!DOCTYPE html>
<html>
<head>
<title>Token Generation php script</title>
</head>
<body>
<p><?php echo "Token Generation Sample <br />";
$DEV_KEY = "XXXXX" ; // Copy your dev key from vidyo.io dashboard
$APP_ID = "XXXXX.vidyo.io" ; // Copy your app Id from vidyo.io dashboard
$username = "Batman" ; // Username, hard coded for debug purposes
$expiresInSecs = 1000 ; // Generated token will expire after these many seconds
// time() by default subtracts datetime(1970, 1, 1) from the datetime
// on which we call it, therefore the number of seconds is smaller
// by (pseudocode!) seconds("1970-01-01").
// In Erlang, on the other hand, we get the actual number of seconds,
// hence we adjust for this difference here.
// IMPORTANT! A 64bit architecture is assumed! Otherwise, the timestamp
// might be stored as a 32bit integer, therefore limiting the "capacity"
// to 2038 (see https://en.wikipedia.org/wiki/Year_2038_problem).
$EPOCH_SECONDS = 62167219200 ;
$expires = $EPOCH_SECONDS + $expiresInSecs + time();
echo "<br />" ;
echo "Developer key" . "\t" ."\t" ."\t" . ":" . $DEV_KEY . "<br />" ;
echo "App ID : " . $APP_ID . "<br />" ;
echo "Username : " . $username . "<br />" ;
echo "Expires : " . date("F j, Y, g:i a", $expiresInSecs + time()) . "<br />" ;
$jid = $username . "@" . $APP_ID ;
//echo "JID: " . $jid . "<br />" ;
// Must place \0 within double quotes, not single quotes.
$body = "provision" . "\0" . $jid . "\0" . $expires . "\0" . "" ;
echo "BODY: " . $body . "<br />" ;
// Important to convert to UTF8. I found this is what made the difference.
$utf8_body = utf8_encode( $body ) ;
echo "UTF8 BODY: " . $utf8_body . "<br />" ;
// Ensure the SHA384 Algo is being used.
$mac_hash = hash_hmac( "sha384", $utf8_body, $DEV_KEY ) ;
echo "HMAC (384): " . $mac_hash . "<br />" ;
// Again, ensure \0 are encapsulated with double quotes. Single quotes does not append the null character to the string
$serialized = $utf8_body . "\0" . $mac_hash ;
echo "SERIALIZED: " . $serialized . "<br />" ;
// Base64 Encode the serialized string
$b64_encoded = base64_encode( $serialized ) ;
echo "<br /> B64 ENCODED TOKEN :<br /><br />" . $b64_encoded . "<br />" ;
?></p>
</body>
</html>
于 2017-11-14T18:41:05.563 回答
1
您可以使用以下代码生成令牌:
class secureToken{
private static $secretKey = 'Your Desired key(Hash)';
private static $secretIv = 'www.domain.com';
private static $encryptMethod = "AES-256-CBC";
public static function tokenencrypt($data) {
$key = hash('sha256', self::$secretKey);
$iv = substr(hash('sha256', self::$secretIv), 0, 16);
$result = openssl_encrypt($data, self::$encryptMethod, $key, 0, $iv);
return $result= base64_encode($result);
}
public static function tokendecrypt($data) {
$key = hash('sha256', self::$secretKey);
$iv = substr(hash('sha256', self::$secretIv), 0, 16);
$result = openssl_decrypt(base64_decode($data), self::$encryptMethod, $key, 0, $iv);
return $result;
}
}
使用方法:
$tokenencrypt = secureToken::tokenencrypt(Your APP ID and Userid);
$tokendecrypt = secureToken::tokendecrypt($tokenencrypt);
于 2017-11-14T05:48:36.523 回答
0
您可以使用 twilo php 生成具有给定参数的令牌。
于 2017-11-14T05:49:20.337 回答