超级播放器签名示例

最后更新时间:2020-09-24 10:27:36

    Python 签名示例

    使用 pyjwt 库计算签名,请使用pip install pyjwt安装。

    #!/usr/bin/python
    #coding=utf-8
    
    import jwt
    
    AppId = 1255566655
    FileId = "4564972818519602447"
    CurrentTime = 1546340400
    PsignExpire = 1546344000
    UrlTimeExpire = "5c2b5640"
    Key = "24FEQmTzro4V5u3D5epW"
    
    Original = {
        "appId": AppId,
        "fileId": FileId,
        "currentTimeStamp": CurrentTime,
        "expireTimeStamp": PsignExpire,
        "urlAccessInfo": {
            "t": UrlTimeExpire
        }
    }
    
    Signature = jwt.encode(Original, Key, algorithm='HS256')
    
    print("Original: ", Original)
    print("Signature: ", Signature)

    Java 签名示例

    使用 java-jwt 库计算签名。

    import java.util.*;
    import com.auth0.jwt.algorithms.Algorithm;
    import com.auth0.jwt.exceptions.JWTCreationException;
    import com.auth0.jwt.JWT;
    
    class Main {
        public static void main(String[] args) {
            Integer AppId = 1255566655;
            String FileId = "4564972818519602447";
            Integer CurrentTime = 1589448067;
            Integer PsignExpire = 1589548067;
            String UrlTimeExpire = "5ebe9423‬";
            String Key = "24FEQmTzro4V5u3D5epW";
            HashMap<String, String> urlAccessInfo = new HashMap<String, String>();
            urlAccessInfo.put("t", UrlTimeExpire);
    
            try {
                Algorithm algorithm = Algorithm.HMAC256(Key);
                String token = JWT.create().withClaim("appId", AppId).withClaim("fileId", FileId)
                        .withClaim("currentTimeStamp", CurrentTime).withClaim("expireTimeStamp", PsignExpire)
                        .withClaim("urlAccessInfo", urlAccessInfo).sign(algorithm);
                System.out.println("token:" + token);
            } catch (JWTCreationException exception) {
                // Invalid Signing configuration / Couldn't convert Claims.
            }
        }
    }

    Go 签名示例

    使用 jwt-go 库计算签名,请使用命令go get github.com/dgrijalva/jwt-go进行安装。

    
    package main
    
    import (
            "fmt"
            "time"
            "strconv"
            "github.com/dgrijalva/jwt-go"
    )
    
    func main() {
            appId := 1255566655 // 用户 appid
            fileId := "4564972818519602447" // 目标 FileId
            currentTime := time.Now().Unix()
            psignExpire := currentTime + 3600 // 可任意设置过期时间,示例1h
            urlTimeExpire := strconv.FormatInt(psignExpire, 16) // 可任意设置过期时间,16进制字符串形式,示例1h
            key := []byte("24FEQmTzro4V5u3D5epW")
    
            // Create a new token object, specifying signing method and the claims
            // you would like it to contain.
            token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
                    "appId":            appId,
                    "fileId":           fileId,
                    "currentTimeStamp": currentTime,
                    "expireTimeStamp":  psignExpire,
                    "urlAccessInfo": map[string]string{
                            "t": urlTimeExpire,
                    },
            })
    
            // Sign and get the complete encoded token as a string using the secret
            tokenString, err := token.SignedString(key)
    
            fmt.Println(tokenString, err)
    }

    C# 签名示例

    使用 jose-jwt 计算签名,请使用 NuGet 命令Install-Package jose-jwt进行安装。

    using System;
    using System.Text;
    using System.Collections.Generic;
    using Jose;
    
    public class Program
    {
            public static void Main()
            {
                    var appId = 1255566655; // 用户 appid
                    var fileId = "4564972818519602447"; // 目标 FileId
                    var currentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
                    var psignExpire = currentTime + 3600; // 可任意设置过期时间,示例1h
                    var urlTimeExpire = psignExpire.ToString("X4"); // 可任意设置过期时间,16进制形式,示例1h
                    var key = "24FEQmTzro4V5u3D5epW";
                    var keyBytes = Encoding.ASCII.GetBytes(key);
                    var payload = new Dictionary<string, object>()
                    {
                            {"appId", appId}, 
                            {"fileId", fileId}, 
                            {"currentTimeStamp", currentTime}, 
                            {"expireTimeStamp", psignExpire}, 
                            {"urlAccessInfo", new Dictionary<string, object>()
                                    {
                                            {"t", urlTimeExpire}
                                    }
                            }
                    };
                    string token = Jose.JWT.Encode(payload, keyBytes, JwsAlgorithm.HS256);
                    Console.WriteLine(token);
            }
    }

    PHP 签名示例

    使用 php-jwt 计算签名,请使用命令composer require firebase/php-jwt进行安装。

    <?php
    require 'vendor/autoload.php';
    use \Firebase\JWT\JWT;
    
    $appId = 1255566655; // 用户 appid
    $fileId = "4564972818519602447"; // 目标 FileId
    $currentTime = time();
    $psignExpire = $currentTime + 3600; // 可任意设置过期时间,示例1h
    $urlTimeExpire = dechex($psignExpire); // 可任意设置过期时间,16进制字符串形式,示例1h
    $key = "24FEQmTzro4V5u3D5epW";
    
    $payload = array(
        "appId" => $appId,
        "fileId" => $fileId,
        "currentTimeStamp" => $currentTime,
        "expireTimeStamp" => $psignExpire,
        "urlAccessInfo" => array(
            "t" => $urlTimeExpire
        )
    );
    
    $jwt = JWT::encode($payload, $key, 'HS256');
    print_r($jwt);
    ?>

    Node.js 签名示例

    使用 jsonwebtoken 计算签名,请使用命令npm install jsonwebtoken进行安装。

    var jwt = require('jsonwebtoken');
    
    var appId = 1255566655 // 用户 appid
    var fileId = "4564972818519602447" // 目标 FileId
    var currentTime = Math.floor(Date.now()/1000)
    var psignExpire = currentTime + 3600 // 可任意设置过期时间,示例1h
    var urlTimeExpire = psignExpire.toString(16) // 可任意设置过期时间,16进制字符串形式,示例1h
    var key = '24FEQmTzro4V5u3D5epW'
    
    var payload = {
            appId: appId,
            fileId: fileId,
            currentTimeStamp: currentTime,
            expireTimeStamp: psignExpire,
            urlAccessInfo: {
                    t: urlTimeExpire
            }
    }
    var token = jwt.sign(payload, key);
    console.log(token);