小编ali*_*gex的帖子

如何使用PHP生成Google ReCaptcha V2安全令牌?

我正在尝试为ReCaptcha V2生成安全令牌,如下所述:https: //developers.google.com/recaptcha/docs/secure_token

不幸的是,我生成的stoken无效,我无法找到一种方法来检查它为什么不起作用.有一个工作的Java示例(STokenUtils.java),但我发现自己无法将其转换为PHP.

public static function generateSecurityToken($secretKey){
    $stoken = array(
        'session_id' => session_id(),
        'ts_ms' => round(microtime(true)*1000)
    );
    $secretKey = self::pkcs5_pad(hash('sha1', $secretKey), 16);
    $stoken_json = json_encode($stoken);
    $stoken_crypt = self::encrypt(self::pkcs5_pad($stoken_json, 16), $secretKey);
    return $stoken_crypt;
}

public static function encrypt($sStr, $sKey) {
    return base64_encode(
        mcrypt_encrypt(
            MCRYPT_RIJNDAEL_128, 
            base64_decode($sKey),
            $sStr,
            MCRYPT_MODE_ECB
        )
    );
}

public static function pkcs5_pad ($text, $blocksize) { 
    $pad = $blocksize - (strlen($text) % $blocksize); 
    return $text . str_repeat(chr($pad), $pad); 
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以提供一个有效的PHP示例或指出我的代码中的任何明显错误吗?

php security encryption token recaptcha

9
推荐指数
1
解决办法
5692
查看次数

标签 统计

encryption ×1

php ×1

recaptcha ×1

security ×1

token ×1