hyperledger-composer:哈希文件 (sha256)

hyperledger-composer: Hashing file (sha256)

- 我希望通过交易处理器导入 Stanford Javascript 加密库。所以我的问题是:我想计算事务中文件内容的 sha256 哈希值。是否有 "painless" 使用交易处理器计算 sha256 哈希的方法?

感谢您的帮助!

不要尝试使用 Node。只需在交易定义中包含一个 JavaScript 函数即可。如果需要,请使用单独的文件。我快速 Google 找到了一些

https://github.com/emn178/js-sha256/blob/master/src/sha256.js

我在客户端中对文件进行哈希处理,但在我的事务函数中使用了一个 JavaScript 函数来生成 GUID,因此过程是相同的。

使用 David Berger 找到的库的快速示例:

https://github.com/emn178/js-sha256/blob/master/src/sha256.js

(从网上下载文件,然后计算哈希值)。

使用外部 REST 服务计算散列可能更明智(可能使用 https://hyperledger.github.io/composer/integrating/call-out,但缺少 headers?)

编辑:它仅适用于 playground(客户端),参见 Function in logic.js works in playground but not in REST server, right still learning... Maybe I should try with https://www.npmjs.com/package/request... 或我自己的外部 REST 服务。

/**
* This part will only work on playground. Should try with
* @param {String} documentUrl 
*/
function getContent(documentUrl)
{
    return fetch(documentUrl, {
        method: 'GET'
    }).then((response) => {
        return response.text()
        .then((text) => {
          return text;
        });
     });
}

/**
 * @param {String} documentUrl
 */
function generateHash(documentUrl)
{
    return getContent(documentUrl)
    .then((documentContent) => {
        let hash = sha256.create();
        hash.update(documentContent);
        return hash.hex();
    });
}

所以,案件结案了,但我必须承认,这很容易。现在我面临着使用 Http 调用外部 rest API 的更复杂的问题,rest 端点而不是使用包装器……好处是:代码将去除不必要的东西。尽管如此,hyperleder composer 的学习曲线远没有单独的 hyperledger fabric 陡峭。很棒的工具!