【发布时间】:2015-08-18 10:08:32
【问题描述】:
我正在尝试使用链接中描述的使用 perl 的 google 安全令牌 https://developers.google.com/recaptcha/docs/secure_token
但是它总是抛出安全令牌无效。 我也检查了这个链接,但没有成功 https://stackoverflow.com/questions/31478724/how-to-generate-a-google-recaptcha-v2-secure-token-with-php
#!/usr/bin/perl
use Data::UUID;
use JSON;
use Time::HiRes qw/gettimeofday/;
use MIME::Base64::URLSafe;
use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
use Crypt::Rijndael;
use constant PUBLIC_KEY => '...';
use constant PRIVATE_KEY => '...';
my $public_key = PUBLIC_KEY;
print "Content-type: text/html;charset=UTF-8\n\n";
my $uuid = Data::UUID->new();
my $uuid1 = $uuid->create_str();
my $uuidstr = $uuid->to_string( $uuid );
my $seconds = gettimeofday(); #in scalar context it returns a
my $ms = int($seconds*1000);
my %hash;
$hash{'session_id'} = $uuidstr;
$hash{'timestamp'} = $ms;
my $json = JSON->new->allow_nonref;
my $json_text = $json->encode(\%hash);
$json_text =~ s/"//g;
my $sha_one = sha1(PRIVATE_KEY);
my $new_secret_key = substr $sha_one, 0, 16;
my $block_size = 16;
my $pad = $block_size - ((length $json_text) % $block_size);
my $append_str = $pad x $pad;
$json_text = $json_text . $append_str;
my $cipher = Crypt::Rijndael->new($new_secret_key, Crypt::Rijndael::MODE_ECB);
my $cipher_text = $cipher->encrypt($json_text);
my $encoded_text = urlsafe_b64encode($cipher_text);
print <<EOT;
<html>
<head>
<script src='//www.google.com/recaptcha/api.js'></script>
</head>
<body>
<form>
<div class="g-recaptcha" data-sitekey="$public_key" data-stoken="$encoded_text"></div>
</form>
</body>
</html>
EOT
谁能指出我的代码中的任何明显错误或建议一些已经存在的 perl 代码?
【问题讨论】:
-
你有什么理由不使用render the widget的JavaScript库吗?
-
@MattJacob javascript 库需要每个域的唯一密钥。我不想使用安全令牌进行重新验证,这是其中的一部分。其他我想使用 perl 本身来完成。谢谢
-
您是否尝试过任何可用的modules?
标签: perl security encryption token recaptcha