Skip to content
Permalink
Browse files

Digest, feat: supports encoding. (#445)

* Compatible with nodejs' digest method and supports encoding.

* IModify digest English document

* 1. 简化digest实现
2. 补充base64编码用例
  • Loading branch information...
bengbengca authored and xicilion committed Jul 9, 2018
1 parent bd3a4b8 commit 7b1d7e2853f87b84be6ca6411525a11aec52301e
Showing with 166 additions and 85 deletions.
  1. +1 −2 fibjs/include/Digest.h
  2. +4 −9 fibjs/include/ifs/Digest.h
  3. +15 −16 fibjs/src/crypto/Digest.cpp
  4. +3 −8 idl/us-en/Digest.idl
  5. +3 −8 idl/zh-cn/Digest.idl
  6. +140 −42 test/hash_test.js
@@ -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:
@@ -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:
@@ -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();
}

@@ -9,6 +9,7 @@
#include "ifs/hash.h"
#include "Digest.h"
#include "Buffer.h"
#include "encoding.h"
#include <string.h>

namespace fibjs {
@@ -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)
@@ -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)
@@ -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;
@@ -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;
Oops, something went wrong.

0 comments on commit 7b1d7e2

Please sign in to comment.
You can’t perform that action at this time.