77 lines
2.2 KiB
Perl
77 lines
2.2 KiB
Perl
#!/usr/bin/perl
|
|
use strict;
|
|
use warnings;
|
|
use v5.26;
|
|
|
|
use Carp;
|
|
use Time::Piece;
|
|
use Digest::SHA qw(sha256_hex hmac_sha256_hex);
|
|
use JSON::MaybeXS qw(encode_json);
|
|
|
|
use Exporter 'import';
|
|
our @EXPORT_OK = 'sign';
|
|
|
|
sub sign {
|
|
my $SourceText = @_;
|
|
carp "You must provide SecretKey.csv to parent folder!";
|
|
my $data = {
|
|
SourceText => $SourceText,
|
|
Source => 'en',
|
|
Target => 'zh',
|
|
ProjectID => 0
|
|
};
|
|
my $payload = encode_json($data);
|
|
|
|
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 confess;
|
|
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";
|
|
return $Authorization;
|
|
}
|
|
|