使用不带节点的dropbox-api-content-hasher

时间:2017-12-04 16:53:31

标签: javascript node.js cordova phonegap

我在node.js环境中使用了dropbox-api-content-hasher(gitrepo here),取得了巨大的成功,但现在我需要在Phonegap / cordova项目中使用它,而且我我不知道如何更换以下内容:

k=math.floor(counts[0]*0.1-l)-1

x=dta_filter[(dta.YRSSCH==0)][['LogEarn for AgeInDays in [20, 25)']]

x=x['LogEarn for AgeInDays in [20, 25)'].tolist()

lower_bound=np.partition(np.asarray(x), k)[k]

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

我已添加np.partition(np.asarray(x), k)[k]但我需要访问output变量才能执行以下操作:

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

然后访问<script type="text/javascript" src="js/dropbox-content-hasher.js"></script>我需要做的其余事情。我没有关于如何将它转换成任何想法的经验?

我建立的是phonegap而不是cordova cli,因为我对这种方法更有经验。

修改

这是我基于Alex的答案尝试的一个小提琴,但我在网络控制台中得到'CryptoJS不是构造函数'。我不知道如何引入cryptojs库!

Fiddle here

2 个答案:

答案 0 :(得分:0)

我只是需要相同并改编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

答案 1 :(得分:0)

使用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();
}