From 3db228fa0881d57bc6b3c19c5af0d14282714166 Mon Sep 17 00:00:00 2001 From: zyppe <210hcl@gmail.com> Date: Thu, 20 Feb 2025 17:19:02 +0800 Subject: [PATCH] Add Tencent with sign --- .gitignore | 2 ++ Translator/sign.pl | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 Translator/sign.pl diff --git a/.gitignore b/.gitignore index 9f11b75..de11b0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .idea/ +.vscode/ +SecretKey.csv \ No newline at end of file diff --git a/Translator/sign.pl b/Translator/sign.pl new file mode 100644 index 0000000..6b34b7a --- /dev/null +++ b/Translator/sign.pl @@ -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;