title: How to generate a Google ReCaptcha V2 secure token with Perl?
body: <p>I am trying to use google secure token using perl as described in the link
  <a href="https://developers.google.com/recaptcha/docs/secure_token" rel="nofollow">https://developers.google.com/recaptcha/docs/secure_token</a></p>
  
  <p>However it always throws up that the secure token is invalid.
  I checked this link as well but with no success
  <a href="https://developers.google.com/recaptcha/docs/secure_token" rel="nofollow">https://stackoverflow.com/questions/31478724/how-to-generate-a-google-recaptcha-v2-secure-token-with-php</a></p>
  
  <pre><code>#!/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  =&gt; '...';
  use constant PRIVATE_KEY =&gt; '...';
  
  my $public_key = PUBLIC_KEY;
  
  print "Content-type: text/html;charset=UTF-8\n\n";
  
  my $uuid = Data::UUID-&gt;new();
  
  my $uuid1 = $uuid-&gt;create_str();
  my $uuidstr = $uuid-&gt;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-&gt;new-&gt;allow_nonref;
  my $json_text = $json-&gt;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-&gt;new($new_secret_key, Crypt::Rijndael::MODE_ECB);
  my $cipher_text = $cipher-&gt;encrypt($json_text);
  my $encoded_text = urlsafe_b64encode($cipher_text);
  
  print &lt;&lt;EOT;
  &lt;html&gt;
  &lt;head&gt;
    &lt;script src='//www.google.com/recaptcha/api.js'&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form&gt;
      &lt;div class="g-recaptcha" data-sitekey="$public_key" data-stoken="$encoded_text"&gt;&lt;/div&gt;
    &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  EOT
  </code></pre>
  
  <p>Can anybody point out any obvious mistakes in my code or suggest some already existing perl code?</p>