hyperledger-composer:哈希文件(sha256)

时间:2018-02-18 14:10:23

标签: hash sha256 hyperledger-composer

Unfortunately is Node.js 'require' not supported - 我希望通过事务处理器导入Stanford Javascript Crypto Library。所以我的问题是:我想在事务中计算文件内容的sha256哈希值。是否存在使用事务处理器计算sha256哈希的“无痛”方法?

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

不要尝试使用Node。只需在事务定义中包含JavaScript函数即可。如果需要,请使用单独的文件。我做了一个快速谷歌,发现了一些如

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

我在客户端进行文件散列,但在事务函数中使用JavaScript函数生成GUID,因此过程是相同的。

答案 1 :(得分:0)

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

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

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

使用外部REST服务计算哈希值可能更明智(可能使用https://hyperledger.github.io/composer/integrating/call-out,但标题丢失了吗?)

编辑:它只能使用playground(客户端),请参阅Function in logic.js works in playground but not in REST server,还在学习...也许我应该尝试使用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调用外部休息API,使用休息端点而不是使用包装器......好处是:代码将被剥夺不必要的东西。尽管如此,高层作曲家的学习曲线远远不如单独的超级织物。伟大的工具!