Skip to content

Commit

Permalink
Digest, feat: supports encoding. (#445)
Browse files Browse the repository at this point in the history
* Compatible with nodejs' digest method and supports encoding.

* IModify digest English document

* 1. 简化digest实现
2. 补充base64编码用例
  • Loading branch information
regtest authored and xicilion committed Jul 9, 2018
1 parent bd3a4b8 commit 7b1d7e2
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 85 deletions.
3 changes: 1 addition & 2 deletions fibjs/include/Digest.h
Expand Up @@ -22,8 +22,7 @@ class Digest : public Digest_base {
public:
// Digest_base
virtual result_t update(Buffer_base* data, obj_ptr<Digest_base>& retVal);
virtual result_t digest(Buffer_base* data, obj_ptr<Buffer_base>& retVal);
virtual result_t digest(obj_ptr<Buffer_base>& retVal);
virtual result_t digest(exlib::string codec, v8::Local<v8::Value>& retVal);
virtual result_t get_size(int32_t& retVal);

private:
Expand Down
13 changes: 4 additions & 9 deletions fibjs/include/ifs/Digest.h
Expand Up @@ -24,8 +24,7 @@ class Digest_base : public object_base {
public:
// Digest_base
virtual result_t update(Buffer_base* data, obj_ptr<Digest_base>& retVal) = 0;
virtual result_t digest(Buffer_base* data, obj_ptr<Buffer_base>& retVal) = 0;
virtual result_t digest(obj_ptr<Buffer_base>& retVal) = 0;
virtual result_t digest(exlib::string codec, v8::Local<v8::Value>& retVal) = 0;
virtual result_t get_size(int32_t& retVal) = 0;

public:
Expand Down Expand Up @@ -89,22 +88,18 @@ inline void Digest_base::s_update(const v8::FunctionCallbackInfo<v8::Value>& arg

inline void Digest_base::s_digest(const v8::FunctionCallbackInfo<v8::Value>& args)
{
obj_ptr<Buffer_base> vr;
v8::Local<v8::Value> vr;

METHOD_NAME("Digest.digest");
METHOD_INSTANCE(Digest_base);
METHOD_ENTER();

METHOD_OVER(1, 1);
METHOD_OVER(1, 0);

ARG(obj_ptr<Buffer_base>, 0);
OPT_ARG(exlib::string, 0, "buffer");

hr = pInst->digest(v0, vr);

METHOD_OVER(0, 0);

hr = pInst->digest(vr);

METHOD_RETURN();
}

Expand Down
31 changes: 15 additions & 16 deletions fibjs/src/crypto/Digest.cpp
Expand Up @@ -9,6 +9,7 @@
#include "ifs/hash.h"
#include "Digest.h"
#include "Buffer.h"
#include "encoding.h"
#include <string.h>

namespace fibjs {
Expand Down Expand Up @@ -58,13 +59,13 @@ result_t Digest::update(Buffer_base* data, obj_ptr<Digest_base>& retVal)
return 0;
}

result_t Digest::digest(obj_ptr<Buffer_base>& retVal)
result_t Digest::digest(exlib::string codec,
v8::Local<v8::Value>& retVal)
{
exlib::string strBuf;
if (m_iAlgo < 0)
return CHECK_ERROR(CALL_E_INVALID_CALL);

exlib::string strBuf;

strBuf.resize(mbedtls_md_get_size(m_ctx.md_info));

if (m_bMac)
Expand All @@ -75,22 +76,20 @@ result_t Digest::digest(obj_ptr<Buffer_base>& retVal)
m_iAlgo = -1;
mbedtls_md_hmac_reset(&m_ctx);

retVal = new Buffer(strBuf);

if ((codec == "buffer")) {
obj_ptr<Buffer_base> buf = new Buffer(strBuf);
retVal = buf->wrap();
} else {
exlib::string data;
result_t hr = commonEncode(codec, strBuf, data);
if (hr < 0)
return hr;

retVal = holder()->NewString(data);
}
return 0;
}

result_t Digest::digest(Buffer_base* data,
obj_ptr<Buffer_base>& retVal)
{
if (m_iAlgo < 0)
return CHECK_ERROR(CALL_E_INVALID_CALL);

obj_ptr<Digest_base> r;
update(data, r);
return digest(retVal);
}

result_t Digest::get_size(int32_t& retVal)
{
if (m_iAlgo < 0)
Expand Down
11 changes: 3 additions & 8 deletions idl/us-en/Digest.idl
Expand Up @@ -7,15 +7,10 @@ interface Digest : object
update(Buffer data);

/*! @brief Compute and return the digest
@param data Binary data packet which will be updated into the digest before computing
@return The binary digest
@param codec The encode format, can be "buffer", "hex", “base64”, "utf8" or any other character sets supported by the system.
@return The encode value representing the value of the digest
*/
Buffer digest(Buffer data);

/*! @brief Compute and return the digest
@return The binary digest
*/
Buffer digest();
Value digest(String codec = "buffer");

/*! @brief Query the number of bytes of the digest of the digest algorithm currently being used */
readonly Integer size;
Expand Down
11 changes: 3 additions & 8 deletions idl/zh-cn/Digest.idl
Expand Up @@ -8,15 +8,10 @@ interface Digest : object
Digest update(Buffer data);

/*! @brief 计算并返回摘要
@param data 二进制数据块,此数据块将在计算前更新进摘要
@return 返回摘要的二进制数据
@param codec 指定编码格式,允许值为:"buffer", "hex", "base64", "utf8", 或者系统支持的字符集
@return 返回指定编码的摘要表示
*/
Buffer digest(Buffer data);

/*! @brief 计算并返回摘要
@return 返回摘要的二进制数据
*/
Buffer digest();
Value digest(String codec = "buffer");

/*! @brief 查询当前信息摘要算法的摘要字节数 */
readonly Integer size;
Expand Down

0 comments on commit 7b1d7e2

Please sign in to comment.