PHP签名代码:
```php
// sign begin.
$private_key_resource = openssl_get_privatekey($rsa_private_key_string);
$private_key_details = openssl_pkey_get_details($private_key_resource);
if (!isset($private_key_details['key']) || $private_key_details['type'] !== OPENSSL_KEYTYPE_RSA) {
throw new InvalidArgumentException('This key is not compatible with RSA signatures');
}
$signature = '';
openssl_sign($string_to_sign, $signature, $private_key_resource, OPENSSL_ALGO_SHA256);
// free the key from memory
openssl_free_key($private_key_resource);
$base64url_encode_signature = $this->base64url_encode($signature);
$request_header['App-Signature'] = $base64url_encode_signature.'.'.$string_to_sign;
private function base64url_encode($data)
{
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
```
go验证代码:
```go
publicKey := LoadRSAPublicKeyFromDisk()
digest := sha256.New()
//digest.Write(bodyByte)
digest.Write([]byte(""))
hashed := digest.Sum(nil)
tt1 := strings.Replace(elementArray[0], "-", "+", -1)
tt2 := strings.Replace(tt1, "_", "/", -1)
repeatChar := strings.Repeat("=", 4 - len(tt2) % 4)
tt2 += repeatChar
ttt,_ := base64.StdEncoding.DecodeString(tt2)
//ttt,_ := base64.StdEncoding.DecodeString(elementArray[0])
logger.AccessLog("ttt1=%X, elementArray[0]=%v, tt2=%v", ttt, elementArray[0])
err4 := rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, hashed, ttt)
```
PHP签名的字符串无法用go验证成功?
大家有没有人遇到过这种问题?
更多评论
这一句 $request_header['App-Signature'] = $base64url_encode_signature.'.'.$string_to_sign; 已经算好的签名为啥还要拼上原始字符串??
#1