$config = array( "digest_alg" => "sha512", "private_key_bits" => 512, "private_key_type" => OPENSSL_KEYTYPE_RSA, );
// 默认的 private_key_bits 为 1024
$r = openssl_pkey_new($config);
openssl_pkey_export($r, $privKey);
file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'private_key.pem', $privKey);
$this->_privKey = openssl_pkey_get_private($privKey);
$rp = openssl_pkey_get_details($r);
$pubKey = $rp['key'];
file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'public_key.pem', $pubKey);
//$this->_pubKey = openssl_get_publickey($pubKey);
$this->_pubKey = openssl_pkey_get_public($pubKey);
php大概用上面的代码导出 一堆.pem的证书
然后在go中
block, := pem.Decode([]byte(这里就是导出的公匙的内容)) pub, err := x509.ParsePKIXPublicKey(block.Bytes) if err != nil { fmt.Printf("Failed to parse RSA public key: %s\n", err) return false, err } rsaPub, := pub.(*rsa.PublicKey)
h := crypto.Hash.New(crypto.SHA1)
h.Write([]byte(src))
digest := h.Sum(nil)
data, _ := base64.StdEncoding.DecodeString(string(sign))
hexSig := hex.EncodeToString(data)
fmt.Printf("base decoder: %v, %v\n", string(sign), hexSig)
err = rsa.VerifyPKCS1v15(rsaPub, crypto.SHA1, digest, data)
if err != nil {
fmt.Println("Verify sig error, reason: ", err)
return false, err
}
return true, nil
然后这个签名的 始终无法在php代码里验证签名
有疑问加站长微信联系(非本文作者)

自己搞定了 哈哈 原来 签名后 返回的 []bytes 应该直接 base64返回签名就可以验证通过 happy
背景介绍 这边 我用PHP历时5个月 开发了一个 xxx钱包服务端 机后台 每个应用或者网站 在 oauth2 协议的基础上 双向rsa签名传输数据 可以实现 [支付宝,微信,百付宝,快钱...]这个采用mixin插件形式的通道 然后提供充值 以及充值原路退款 以及快钱企业付款到个人银行账号 自动对账... 用户余额等... 使用rabbitmq 实现异步任务
现在是要搞一个golang的 sdk 呵呵 go没搞过 边写边差 还好n年前有点c基础 然后写过n年python
func (this *FinanceApi) makeSign(will_sign_str string) string { MYPRIKEY, := ioutil.ReadFile("/data0/xxkey/privatekey.pem") keyobj, := pem.Decode(MY_PRIKEY) if keyobj == nil { panic("载入RSA私钥错误") }
// s2 := hex.EncodeToString(signedData) // fmt.Printf("%v\n", s2)
}