【问题标题】:Using dropbox-api-content-hasher without node使用不带节点的 dropbox-api-content-hasher
【发布时间】:2017-12-04 16:53:31
【问题描述】:

我已经在 node.js 环境中使用 dropbox-api-content-hasher (gitrepo here) 用于 Electron 项目并取得了巨大成功,但现在我需要在 Phonegap/cordova 项目中使用它,但我没有想法如何替换以下内容:

const fs = require('fs');
const dch = require('dropbox-content-hasher');

... 使用 vanilla javascript,所以我可以使用它!

我已经包含了<script type="text/javascript" src="js/dropbox-content-hasher.js"></script>,但我需要访问dch 变量来执行以下操作:

const hasher = dch.create();

然后访问hasher 其余我需要做的事情。我没有如何转换它的经验,有什么想法吗?

我在 phonegap 而不是 cordova cli 中构建,因为我在这种方法方面更有经验。

编辑:

这是我根据 Alex 的回答所做的尝试,但我在 Web 控制台中得到“CryptoJS 不是构造函数”。不知道怎么引入cryptojs库!

Fiddle here

【问题讨论】:

    标签: javascript node.js cordova phonegap


    【解决方案1】:

    我只需要相同的内容并改编了dropbox repo 中的示例。 我使用crypto-js 库。该代码在 ES6 中,并在 quasar-framework 应用程序中使用(顺便说一句。由于 phonegap/cordova,您可能会感兴趣):

    /**
     * Computes a hash using the same algorithm that the Dropbox API uses for the
    * the "content_hash" metadata field.
    *
    * The `digest()` method returns a hexadecimal-encoded version
    * of the digest of the hash.
    * The "content_hash" field in the Dropbox API is a hexadecimal-encoded version
    * of the digest.
    *
    * Example:
    *
    *     import DropboxContentHasher from 'dropboxContentHasher'
    *
    *     let hasher = new DropboxContentHasher()
    *     hasher.update(content) // content is some string or CryptoJS.lib.WordArray
    *     return hasher.digest()
    */
    
    import cryptoJS from 'crypto-js'
    
    const BLOCK_SIZE = 4 * 1024 * 1024
    
    class DropboxContentHasher {
      constructor () {
        this._overallHasher = cryptoJS.algo.SHA256.create()
        this._blockHasher = cryptoJS.algo.SHA256.create()
        this._blockPos = 0
      }
    
      update (data) {
        let offset = 0
        while (offset < data.length) {
          if (this._blockPos === BLOCK_SIZE) {
            this._overallHasher.update(this._blockHasher.finalize())
            this._blockHasher = cryptoJS.algo.SHA256.create()
            this._blockPos = 0
          }
          let spaceInBlock = BLOCK_SIZE - this._blockPos
          let inputPartEnd = Math.min(data.length, offset + spaceInBlock)
          let inputPartLength = inputPartEnd - offset
          this._blockHasher.update(data.slice(offset, inputPartEnd))
          this._blockPos += inputPartLength
          offset = inputPartEnd
        }
      }
    
      digest () {
        if (this._blockPos > 0) {
          this._overallHasher.update(this._blockHasher.finalize())
          this._blockHasher = null
        }
        let r = this._overallHasher.finalize().toString()
        this._overallHasher = null // Make sure we can't use this object anymore.
        return r
      }
    }
    
    export default DropboxContentHasher
    

    【讨论】:

    • 我没有使用任何框架,所以我不能使用import...我如何真正让它独立存在?
    • 您可以通过脚本标签包含crypto-js(只需从他们的网页下载src)并将上述代码转换为简单的javascript函数。实例变量可以是全局变量。除了 import 和 class 语句之外,没有任何特定于 ES6 的内容,因此应该很简单。
    • 你好,我知道这很糟糕,但我仍然无法让它工作,我真的需要它!您能否告诉我如何将其转换为“简单的 javascript 函数”?我应该包括Git src folder 列表中的哪个文件?我只是不在那里以我的理解去做!非常感谢!
    【解决方案2】:

    使用 crypto-js 的简单 JavaScript 实现:

    function computeContentHash(content) {
      const BLOCK_SIZE = 4 * 1024 * 1024;
      let hash = CryptoJS.algo.SHA256.create();
      for (let p = 0; p < content.length; p += BLOCK_SIZE) {
        let chunk = content.substr(p, BLOCK_SIZE);
        hash.update(CryptoJS.SHA256(chunk));
      }
      return hash.finalize().toString();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多