


package com.android.tencentvpn.util;import io.jsonwebtoken.Jwts;import io.jsonwebtoken.SignatureAlgorithm;import java.security.Key;import java.util.Date;import javax.crypto.spec.SecretKeySpec;public class SignGenerate {private static final long EXPIRE_TIME = 72 * 60 * 60 * 1000; // Expiration time, in milliseconds/*** @param deviceName The unique identifier for a business-controlled device* @param SecretKey The application secret key returned by TencentCloud api* @return The signature string required by the multi-network sdk, which is ultimately invoked via setSign.**/public static String generateSign(String deviceName, String SecretKey) {try {Date now = new Date();Date expireDate = new Date(now.getTime() + EXPIRE_TIME);Key key =new SecretKeySpec(SecretKey.getBytes(), SignatureAlgorithm.HS256.getJcaName());return Jwts.builder().claim("deviceName", deviceName).expiration(expireDate).signWith(key).compact();} catch (Exception e) {e.printStackTrace();}return "";}}
// Add pod 'JWT' to the podfile to integrate the JWT library.// .h file:@interface SignGenerator : NSObject/*** @deviceName The unique identifier for a business-controlled device* @param SecretKey The application secret key returned by TencentCloud api* @return The signature string required by the multi-network sdk, which is ultimately invoked via setSign.*/+ (NSString *)generateSignWithDeviceName:(NSString *)deviceName secretKey:(NSString *)secretKey;@end// .m file#import "SignGenerator.h"#import <JWT/JWT.h>@implementation SignGenerator+ (NSString *)generateSignWithDeviceName:(NSString *)deviceName secretKey:(NSString *)secretKey {// 1. Set the expiration time (72 hours later)NSDate *currentDate = [NSDate date];NSDate *expireDate = [currentDate dateByAddingTimeInterval:72 * 60 * 60];// 2. Construct the Claims (payload)NSDictionary *claims = @{@"deviceName": deviceName,@"exp": @((long)[expireDate timeIntervalSince1970]) // Expiration};NSData *keyData = [secretKey dataUsingEncoding:NSUTF8StringEncoding];// 3. Sign using the HS256 algorithm and the secret keyJWTBuilder *builder = [JWTBuilder encodePayload:claims].secretData(keyData).algorithmName(@"HS256"); // Algorithm nameNSString *jwt = builder.encode;// 4. Error handlingif (builder.jwtError) {NSLog(@"JWT generation failed: %@", builder.jwtError);return @"";}return jwt;}@end
package mainimport ("fmt""github.com/dgrijalva/jwt-go/v4""time")func main() {sign, expireTime, err := GenerateSign("tencent-test")if err != nil {panic(err)}println(sign, expireTime)}// SignClaims JwtCustomClaimstype SignClaims struct {jwt.StandardClaimsDeviceName string `json:"deviceName"`}var (ExpireTime = 72 // Expiration time, in hoursSecretKey = []byte("shCrVLKq/Hp6UdtVpw8lBqu6ZDcLwOABf2Az67e/pjI=") // APP secret key)// GenerateSign generates a signaturefunc GenerateSign(deviceName string) (string, int64, error) {expireTime := time.Now().Add(time.Duration(ExpireTime) * time.Hour)claims := &SignClaims{StandardClaims: jwt.StandardClaims{NotBefore: jwt.Now(),ExpiresAt: jwt.At(expireTime),},DeviceName: deviceName,}println(fmt.Sprintf("%#v", claims))token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)sign, err := token.SignedString(SecretKey)if err != nil {return "", 0, err}return sign, expireTime.Unix(), nil}
// Depends on the @ohos/jsonwebtoken library to generate JWT signatures// "dependencies": {// "@ohos/jsonwebtoken": "1.0.2"// },import jwt from '@ohos/jsonwebtoken';import { JwtPayload } from '@ohos/jsonwebtoken';const SIGN_EXPIRE_TIME = 72 * III * 60 * 1000; // Expiration time, 72 hours, in millisecondsfunction generateSign(deviceName: string, secretKey: string): string {try {let exp = Math.floor((Date.now() + SIGN_EXPIRE_TIME) / 1000);let payload: JwtPayload = {deviceName: deviceName,exp: exp};let token: string = jwt.sign(payload, secretKey);hilog.info(DOMAIN, TAG, "generateSign success, jwt=%{public}s", token);return token;} catch (e) {hilog.error(DOMAIN, TAG, "generateSign error: %{public}s", JSON.stringify(e));return "";}}




//Context is the ApplicationContext.MpAccRegister.setEnv(context, 1);
public static void setSign(String appId, String sign)
Parameter | Type | Description |
appId | String | Application appId |
sign | String | Signature string |
func enableOverseas(enabled: Bool)
func setSign(appId: String, sign: String)
Parameter | Description |
appId | Application ID |
sign | Signature |
curl -X POST "http://127.0.0.1:9801/api/v2/client/mp-speeder" ^-H "accept: */*" ^-H "Content-Type: application/json" ^-d "{\\"serviceMode\\": 0,\\"appId\\":\\"app-0eoo3vpctx\\",\\"appSign\\":\\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkZXZpY2VOYW1lIjoiamFja3ktdGVzdC0yMC0xIn0.TleKYrzBL1dyp2i8WTBgs8Y8UvBJQv-n2A2BRMT1xuQ\\",\\"registerEnv\\":1,\\"scheduleMode\\":\\"rtc\\"}"
Term | Type | Description |
appId | String | Used in app mode registration. |
appSign | String | Used in app mode registration. |
scheduleMode | String | Optional: Windows default acceleration mode "RTC". |
피드백