php的openssl_sign($data,$sign,$private_key)这个函数的源码是啥?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php的openssl_sign($data,$sign,$private_key)这个函数的源码是啥?相关的知识,希望对你有一定的参考价值。
看到有文件描述$data是待签名字符串,$sign是签名后的变量,$private_key是私钥,这个函数的功能就是用私钥给待签名的字符串签名,我想问下,他们这个签名是如何完成的?是私钥和字符串简单的链接成一个新的字符串再用sha1生成吗?
或者假如我用其他语言,例如asp自己写,sha1函数已经有了,如何用私钥给字符串做签名呢?
zval **key, *signature;
EVP_PKEY *pkey;
int siglen;
unsigned char *sigbuf;
long keyresource = -1;
char * data;
int data_len;
EVP_MD_CTX md_ctx;
zval *method = NULL;
long signature_algo = OPENSSL_ALGO_SHA1;
const EVP_MD *mdtype;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szZ|z", &data, &data_len, &signature, &key, &method) == FAILURE)
return;
pkey = php_openssl_evp_from_zval(key, 0, "", 0, &keyresource TSRMLS_CC);
if (pkey == NULL)
php_error_docref(NULL TSRMLS_CC, E_WARNING, "supplied key param cannot be coerced into a private key");
RETURN_FALSE;
if (method == NULL || Z_TYPE_P(method) == IS_LONG)
if (method != NULL)
signature_algo = Z_LVAL_P(method);
mdtype = php_openssl_get_evp_md_from_algo(signature_algo);
else if (Z_TYPE_P(method) == IS_STRING)
mdtype = EVP_get_digestbyname(Z_STRVAL_P(method));
else
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown signature algorithm.");
RETURN_FALSE;
if (!mdtype)
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown signature algorithm.");
RETURN_FALSE;
siglen = EVP_PKEY_size(pkey);
sigbuf = emalloc(siglen + 1);
EVP_SignInit(&md_ctx, mdtype);
EVP_SignUpdate(&md_ctx, data, data_len);
if (EVP_SignFinal (&md_ctx, sigbuf,(unsigned int *)&siglen, pkey))
zval_dtor(signature);
sigbuf[siglen] = '\\0';
ZVAL_STRINGL(signature, (char *)sigbuf, siglen, 0);
RETVAL_TRUE;
else
efree(sigbuf);
RETVAL_FALSE;
EVP_MD_CTX_cleanup(&md_ctx);
if (keyresource == -1)
EVP_PKEY_free(pkey);
这个是函数的源码,C写的。源码可以在php官网下载一个php版本,解压打开里面的ext/openssl.c搜上面代码即可。
参考技术A 这个是打开https的访问所用的到的函数追问asp版本的的sha1withrsa签名函数该怎么写?
本回答被提问者采纳 参考技术B openssl_sign(PHP 4 >= 4.0.4, PHP 5)
openssl_sign — Generate signature
说明
bool openssl_sign ( string $data , string &$signature , mixed $priv_key_id [, int $signature_alg = OPENSSL_ALGO_SHA1 ] )
openssl_sign() computes a signature for the specified data by using SHA1 for hashing followed by encryption using the private key associated with priv_key_id. Note that the data itself is not encrypted.
参数
data
signature
If the call was successful the signature is returned in signature.
priv_key_id
signature_alg
For more information see the list of Signature Algorithms.
返回值
成功时返回 TRUE, 或者在失败时返回 FALSE.
更新日志
版本 说明
5.0.0 The signature_alg parameter was added.
范例
Example #1 openssl_sign() example
<?php
// $data is assumed to contain the data to be signed
// fetch private key from file and ready it
$fp = fopen("/src/openssl-0.9.6/demos/sign/key.pem", "r");
$priv_key = fread($fp, 8192);
fclose($fp);
$pkeyid = openssl_get_privatekey($priv_key);
// compute signature
openssl_sign($data, $signature, $pkeyid);
// free the key from memory
openssl_free_key($pkeyid);
?>
参见
openssl_verify() - Verify signature
« openssl_seal
» openssl_verify
此 PHP 版本不支持 PHP openssl_sign() ecparam 键类型
【中文标题】此 PHP 版本不支持 PHP openssl_sign() ecparam 键类型【英文标题】:PHP openssl_sign() ecparam key type not supported in this PHP build 【发布时间】:2012-05-11 19:00:03 【问题描述】:openssl_sign
正在返回签名字符串但打印此错误。我正在使用椭圆曲线键
Warning: openssl_sign() [function.openssl-sign]: key type not supported in this PHP build! in
【问题讨论】:
我也看到了这种行为 【参考方案1】:已在 php 问题跟踪器中为此提交了一个问题: https://bugs.php.net/bug.php?id=66501
【讨论】:
以上是关于php的openssl_sign($data,$sign,$private_key)这个函数的源码是啥?的主要内容,如果未能解决你的问题,请参考以下文章
AS3 RSAKey.sign() != PHP openssl_sign()
支付宝支付php的demo或sdk报错 Warning: openssl_sign() [function.openssl-sign]: Unknown signature algorithm. in