Permalink
Browse files

crypto, feat: PKey constructor support parameters.

  • Loading branch information...
xicilion committed Nov 16, 2017
1 parent 575dedd commit 076830eaa1be6834ed8913f3ebad16c8722adaa1
Showing with 118 additions and 0 deletions.
  1. +23 −0 fibjs/include/ifs/PKey.h
  2. +36 −0 fibjs/src/crypto/PKey.cpp
  3. +59 −0 idl/zh-cn/PKey.idl
View
@@ -24,6 +24,9 @@ class PKey_base : public object_base {
public:
// PKey_base
static result_t _new(obj_ptr<PKey_base>& retVal, v8::Local<v8::Object> This = v8::Local<v8::Object>());
static result_t _new(Buffer_base* DerKey, exlib::string password, obj_ptr<PKey_base>& retVal, v8::Local<v8::Object> This = v8::Local<v8::Object>());
static result_t _new(exlib::string pemKey, exlib::string password, obj_ptr<PKey_base>& retVal, v8::Local<v8::Object> This = v8::Local<v8::Object>());
static result_t _new(v8::Local<v8::Object> jsonKey, obj_ptr<PKey_base>& retVal, v8::Local<v8::Object> This = v8::Local<v8::Object>());
virtual result_t get_name(exlib::string& retVal) = 0;
virtual result_t get_keySize(int32_t& retVal) = 0;
virtual result_t get_publicKey(obj_ptr<PKey_base>& retVal) = 0;
@@ -137,6 +140,26 @@ void PKey_base::__new(const T& args)
hr = _new(vr, args.This());
METHOD_OVER(2, 1);
ARG(obj_ptr<Buffer_base>, 0);
OPT_ARG(exlib::string, 1, "");
hr = _new(v0, v1, vr, args.This());
METHOD_OVER(2, 1);
ARG(exlib::string, 0);
OPT_ARG(exlib::string, 1, "");
hr = _new(v0, v1, vr, args.This());
METHOD_OVER(1, 1);
ARG(v8::Local<v8::Object>, 0);
hr = _new(v0, vr, args.This());
CONSTRUCT_RETURN();
}
View
@@ -22,6 +22,42 @@ result_t PKey_base::_new(obj_ptr<PKey_base>& retVal, v8::Local<v8::Object> This)
return 0;
}
result_t PKey_base::_new(Buffer_base* DerKey, exlib::string password, obj_ptr<PKey_base>& retVal,
v8::Local<v8::Object> This)
{
obj_ptr<PKey> pk = new PKey();
result_t hr = pk->importKey(DerKey, password);
if (hr < 0)
return hr;
retVal = pk;
return 0;
}
result_t PKey_base::_new(exlib::string pemKey, exlib::string password, obj_ptr<PKey_base>& retVal,
v8::Local<v8::Object> This)
{
obj_ptr<PKey> pk = new PKey();
result_t hr = pk->importKey(pemKey, password);
if (hr < 0)
return hr;
retVal = pk;
return 0;
}
result_t PKey_base::_new(v8::Local<v8::Object> jsonKey, obj_ptr<PKey_base>& retVal,
v8::Local<v8::Object> This)
{
obj_ptr<PKey> pk = new PKey();
result_t hr = pk->importKey(jsonKey);
if (hr < 0)
return hr;
retVal = pk;
return 0;
}
PKey::PKey()
{
mbedtls_pk_init(&m_key);
View
@@ -11,6 +11,65 @@ interface PKey : object
/*! @brief PKey 构造函数 */
PKey();
/*! @brief 通过 DER 格式的密钥构造 PKey
@param DerKey DER 格式的密钥
@param password 解密密码
*/
PKey(Buffer DerKey, String password = "");
/*! @brief 通过 PEM 格式的密钥构造 PKey
@param pemKey PEM 格式的密钥
@param password 解密密码
*/
PKey(String pemKey, String password = "");
/*! @brief 通过 JSON 格式的密钥构造 PKey
jsonKey 的格式支持以下四种,RSA 私钥:
```JavaScript
{
"kty": "RSA",
"n": "0m5lvKpWqy9JS7tV2HIPqHCYHLquSuxIC3F8strIQLJKO3rZmTT96KTnhsOfBO7Y1bI7mnT0PB3_vcHd9ekWMEoZJQw7MuB8KeM_Wn54-elJr5DNLk5bMppSGxX7ZnumiXGG51_X3Yp-_EbGtDG80GxXXix7Mucyo7K25uE0uW8=",
"e": "AQAB",
"d": "agN2O9NxMHL1MTMi75WfL9Pxvl-KWXKqZSF6mjzAsF9iKI8euyHIXYFepzU8kual1RsjDhCnzvWqFvZplW8lXqrHf_P-rS_9Y4gBUw6pjnI_DnFIRwWHRvrUHHSCfWOdTCIKdOTkgLZuGFuhEY3RMIW0WSYejjLtftwy0RVxAzk=",
"p": "6a4G1qmfwWmn1biigN7IVFlkbLf9oVe6g7rOmHxI-hn1GRxKDSVuAUrmR1IhuAnca9M0y7SD-7TUs6wjOxWxaw==",
"q": "5ofkxFKdPBD0CQHMb9q13AMHUVe0rJ-hSjqqIBrmqApUOneyAcMV76M0QyIQnI2p3POa4Qu_7XChDwRVl7LlDQ==",
"dp": "2mXGiGwCHl8j-FBWuID-1C6z-BRB3MBEVoeKPOOzxOPruatB3mWEGXsqG7A8SWgV9URxTI2K6P3J6Z7RUpBkvw==",
"dq": "oagn5vfb5NQqnOpS9xkSsD67cfIj821ZSFlNFYhnuOzNVda7z_qCtnHm4zDPH0lEFXoKYMfBhfqWJpaugttjPQ==",
"qi": "dqEQgxNmOVFrF4s776hTqeC6oEDila8EvpVb2F2ZvwAOLjCQ66OiAZK1BiYGHqUy0NeqNmtlsLSuBEZQZvqZwg=="
}
```
RSA 公钥:
```JavaScript
{
"kty": "RSA",
"n": "0m5lvKpWqy9JS7tV2HIPqHCYHLquSuxIC3F8strIQLJKO3rZmTT96KTnhsOfBO7Y1bI7mnT0PB3_vcHd9ekWMEoZJQw7MuB8KeM_Wn54-elJr5DNLk5bMppSGxX7ZnumiXGG51_X3Yp-_EbGtDG80GxXXix7Mucyo7K25uE0uW8=",
"e": "AQAB"
}
```
EC 私钥:
```JavaScript
{
"kty": "EC",
"crv": "P-521",
"x": "ATfNNFuuvlGxrTGoXgyfSAGgRNNDnO3rN3k74urKJdVS14RYhdnSwm91Bm-F1l-T1XKlAY2yRnzG9w1Ukvo8c0wL",
"y": "ASBHqrruB6kdkEUB3vlW3-UIkk4HtKdUeTwN-7m3j2rgZvYR1ffRAapDvWqKGiBjomqWafxokBkbDI0c95f6f4XU",
"d": "AfkIbUHXfW41njdpoKuqqKludcoLJS8D_oMEwkj-GVaXFNKccIoF5iKGu2c69kNDjo83R_7wyGlfRczsklkik1ST"
}
```
EC 公钥:
```JavaScript
{
"kty": "EC",
"crv": "P-521",
"x": "ATfNNFuuvlGxrTGoXgyfSAGgRNNDnO3rN3k74urKJdVS14RYhdnSwm91Bm-F1l-T1XKlAY2yRnzG9w1Ukvo8c0wL",
"y": "ASBHqrruB6kdkEUB3vlW3-UIkk4HtKdUeTwN-7m3j2rgZvYR1ffRAapDvWqKGiBjomqWafxokBkbDI0c95f6f4XU"
}
```
@param jsonKey JSON 格式的密钥
*/
PKey(Object jsonKey);
/*! @brief 返回当前算法名称 */
readonly String name;

0 comments on commit 076830e

Please sign in to comment.