如何在PHP中制作SHA256withRSA?我怎么知道官方示例中的什么[签名字节]? [英] How Can I Make SHA256withRSA in PHP? How i can know what [signature bytes] in official example?

查看:133
本文介绍了如何在PHP中制作SHA256withRSA?我怎么知道官方示例中的什么[签名字节]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在PHP中制作SHA256withRSA?

示例: https://developers.google.com/identity/protocols/OAuth2ServiceAccount

  {"alg":"RS256","typ":"JWT"}.{"iss":"761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com",作用域":"https://www.googleapis.com/auth/prediction","aud":"https://www.googleapis.com/oauth2/v4/token","exp":1328554385,"iat":1328550785}.[签名字节] 


以下是已签名并准备好传输的JWT的示例:

<预> <代码> eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI3NjEzMjY3OTgwNjktcjVtbGpsbG4xcmQ0bHJiaGc3NWVmZ2lncDM2bTc4ajVAZGV2ZWxvcGVyLmdzZXJ2aWNlYWNjb3VudC5jb20iLCJzY29wZSI6Imh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL2F1dGgvcHJlZGljdGlvbiIsImF1ZCI6Imh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL29hdXRoMi92NC90b2tlbiIsImV4cCI6MTMyODU1NDM4NSwiaWF0IjoxMzI4NTUwNzg1fQ.UFUt59SUM2_AW4cRU8Y0BYVQsNTo4n7AFsNrqOpYiICDu37vVt-tw38UKzjmUKtcRsLLjrR3gFW3dNDMx_pL9DVjgVHDdYirtrCekUHOYoa1CMR66nxep5q5cBQ4y4u2kIgSvChCTc9pmLLNoIem-ruCecAJYgI9Ks7pTnW1gkOKs0x3YpiLpzplVHAkkHztaXiJdtpBcY1OXyo6jTQCa3Lk2Q3va1dPkh_d - GU2M5flgd8xNBPYw4vxyt0mP59XZlHMpztZt0soSgObf7G3GXArreF_6tpbFsS3z2t5zkEiHuWJXpzcYr5zWTRPDEHsejeBSG8EgpLDce2380ROQ

如何检查[签名字节]?如何在PHP中制作SHA256withRSA?

使用SHA256withRSA签名输入的UTF-8表示形式(也带有SHA-256哈希函数的称为RSASSA-PKCS1-V1_5-SIGN)

解决方案

您可以使用PHP函数 openssl_sign():

 //helper函数函数base64url_encode($ data){返回rtrim(strtr(base64_encode($ data),'+/','-_'),'=');}//Google创建JWT的文档:https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests//{Base64url编码的JSON标头}$ jwtHeader = base64url_encode(json_encode(array("alg"=>"RS256","typ"=>"JWT")));//{Base64url编码的JSON声明集}$ now = time();$ jwtClaim = base64url_encode(json_encode(array("iss"=>"761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com",范围"=>"https://www.googleapis.com/auth/prediction","aud"=>"https://www.googleapis.com/oauth2/v4/token","exp"表示=>$ now + 3600,"iat"=>$ now)));//签名的基本字符串:{Base64url编码的JSON标头}.{Base64url编码的JSON声明集}openssl_sign($ jwtHeader.".. $ jwtClaim,$ jwtSig,$ your_private_key_from_google_api_console,"sha256WithRSAEncryption");$ jwtSign = base64url_encode($ jwtSig);//{Base64url编码的JSON标头}.{Base64url编码的JSON声明集}.{Base64url编码的签名}$ jwtAssertion = $ jwtHeader..".$ jwtClaim..".$ jwtSign;. 

How can I make SHA256withRSA in PHP?

example: https://developers.google.com/identity/protocols/OAuth2ServiceAccount

   {"alg":"RS256","typ":"JWT"}.
    {
    "iss":"761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com",
    "scope":"https://www.googleapis.com/auth/prediction",
    "aud":"https://www.googleapis.com/oauth2/v4/token",
    "exp":1328554385,
    "iat":1328550785
    }.
    [signature bytes]


Below is an example of a JWT that has been signed and is ready for transmission:

eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI3NjEzMjY3OTgwNjktcjVtbGpsbG4xcmQ0bHJiaGc3NWVmZ2lncDM2bTc4ajVAZGV2ZWxvcGVyLmdzZXJ2aWNlYWNjb3VudC5jb20iLCJzY29wZSI6Imh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL2F1dGgvcHJlZGljdGlvbiIsImF1ZCI6Imh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL29hdXRoMi92NC90b2tlbiIsImV4cCI6MTMyODU1NDM4NSwiaWF0IjoxMzI4NTUwNzg1fQ.UFUt59SUM2_AW4cRU8Y0BYVQsNTo4n7AFsNrqOpYiICDu37vVt-tw38UKzjmUKtcRsLLjrR3gFW3dNDMx_pL9DVjgVHDdYirtrCekUHOYoa1CMR66nxep5q5cBQ4y4u2kIgSvChCTc9pmLLNoIem-ruCecAJYgI9Ks7pTnW1gkOKs0x3YpiLpzplVHAkkHztaXiJdtpBcY1OXyo6jTQCa3Lk2Q3va1dPkh_d--GU2M5flgd8xNBPYw4vxyt0mP59XZlHMpztZt0soSgObf7G3GXArreF_6tpbFsS3z2t5zkEiHuWJXpzcYr5zWTRPDEHsejeBSG8EgpLDce2380ROQ

How do I check what [signature bytes]? How do I make SHA256withRSA in PHP?:

Sign the UTF-8 representation of the input using SHA256withRSA (also known as RSASSA-PKCS1-V1_5-SIGN with the SHA-256 hash function)

解决方案

You can use the PHP function openssl_sign():

//helper function
function base64url_encode($data) { 
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); 
}

//Google's Documentation of Creating a JWT: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests

//{Base64url encoded JSON header}
$jwtHeader = base64url_encode(json_encode(array(
    "alg" => "RS256",
    "typ" => "JWT"
)));
//{Base64url encoded JSON claim set}
$now = time();
$jwtClaim = base64url_encode(json_encode(array(
    "iss" => "761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com",
    "scope" => "https://www.googleapis.com/auth/prediction",
    "aud" => "https://www.googleapis.com/oauth2/v4/token",
    "exp" => $now + 3600,
    "iat" => $now
)));
//The base string for the signature: {Base64url encoded JSON header}.{Base64url encoded JSON claim set}
openssl_sign(
    $jwtHeader.".".$jwtClaim,
    $jwtSig,
    $your_private_key_from_google_api_console,
    "sha256WithRSAEncryption"
);
$jwtSign = base64url_encode($jwtSig);

//{Base64url encoded JSON header}.{Base64url encoded JSON claim set}.{Base64url encoded signature}
$jwtAssertion = $jwtHeader.".".$jwtClaim.".".$jwtSign;

这篇关于如何在PHP中制作SHA256withRSA?我怎么知道官方示例中的什么[签名字节]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆