Add Tencent with sign

This commit is contained in:
zyppe 2025-02-20 17:19:02 +08:00
parent e6bc578e11
commit 3db228fa08
2 changed files with 66 additions and 0 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
.idea/
.vscode/
SecretKey.csv

64
Translator/sign.pl Normal file
View file

@ -0,0 +1,64 @@
#!/usr/bin/perl
use strict;
use warnings;
use v5.26;
use Time::Piece;
use Digest::SHA qw(sha256_hex hmac_sha256_hex);
say "You must provide SecretKey.csv to parent folder!";
my $SourceText = "Get Current Time in Seconds Perl";
my $payload = qq
/{"SourceText":"$SourceText","Source":"en","Target":"zh","ProjectId":0}/;
my $service = "tmt";
my $host = "$service.tencentcloudapi.com";
my $region = "ap-shanghai";
my $action = "TextTranslate";
my $version = "2018-03-21";
my $Algorithm = "TC3-HMAC-SHA256";
my $HTTPRequestMethod = "POST";
my $CanonicalURI = "/";
my $CanonicalQueryString =
( $HTTPRequestMethod eq "POST" ) ? "" : "Limit=10&Offset=0";
my $CanonicalHeaders = qq
|content-type:application/json; charset=utf-8
host:$host
x-tc-action:$action|;
my $SignedHeaders = "content-type;host;x-tc-action";
my $HashedRequestPayload = sha256_hex $payload;
my $CanonicalRequest = qq
/$HTTPRequestMethod
$CanonicalURI
$CanonicalQueryString
$CanonicalHeaders
$SignedHeaders
$HashedRequestPayload/;
my $RequestTimestamp = time();
my $Date = localtime->strftime('%Y-%m-%d');
my $CredentialScope = "$Date/$service/tc3_request";
my $HashedCanonicalRequest = sha256_hex $CanonicalRequest;
my $StringToSign = qq
/$Algorithm
$RequestTimestamp
$CredentialScope
$HashedCanonicalRequest/;
open my $in, "<", "../SecretKey.csv" or die $!;
my $key;
<$in>;
$key = <$in>;
close($in);
my @key = split( ",", $key );
my $SecretID = $key[0];
my $SecretKey = $key[1];
my $SecretDate = hmac_sha256_hex( "TC3$SecretKey", $Date );
my $SecretService = hmac_sha256_hex( $SecretDate, $service );
my $SecretSigning = hmac_sha256_hex( $SecretService, "tc3_request" );
my $Signature = hmac_sha256_hex( $SecretSigning, $StringToSign );
my $Authorization =
"$Algorithm Credential=$SecretID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$Signature";
say $Authorization;