2017-12-04 4 views
0

전자 프로젝트의 node.js 환경에서 dropbox-api-content-hasher (gitrepo here)를 사용했지만 큰 성공을 거두었지만 이제는 Phonegap/코르도바 프로젝트와 나는 다음과 같은 교체하는 방법에 대해 아무 생각도 없어 :노드가없는 dropbox-api-content-hasher 사용

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

... 그래서 그것을 사용할 수있는 자바 스크립트 바닐라!

나는 <script type="text/javascript" src="js/dropbox-content-hasher.js"></script> 포함했다 그러나 나는 다음을 수행 할 수 dch 변수에 액세스해야합니다

다음
const hasher = dch.create(); 

하고 hasher 내가해야 할 일의 나머지 부분에 액세스 할 수 있습니다. 그것을 바꾸는 방법에 대한 경험이 없습니다, 어떤 생각입니까?

나는이 접근법에 대해 더 잘 알고 있으므로 cordova cli보다는 phonegap에 구축하고 있습니다.

편집 :

이 알렉스의 답변에 따라 내 시도의 바이올린는하지만 웹 콘솔에서 'CryptoJS 생성자 아니다'지고 있어요. cryptojs 라이브러리를 가져 오는 방법을 모르겠습니다!

Fiddle here

답변

0

난 똑같이 필요하고 dropbox repo에서 예 장치. crypto-js 라이브러리를 사용합니다. 코드는 ES6에 있고 quasar-framework 응용 프로그램에 사용됩니다 (BTW하는 인해 폰갭/코르도바에, 당신을 위해 재미있을 수 있습니다.) :

/** 
* 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 
+0

브릴리언트, 감사합니다! 나는 그것을 시험해 볼 것이다! –

+0

나는'import'를 사용할 수 없기 때문에 어떤 프레임 워크도 사용하지 않고 있습니다 ... 어떻게 문자 그대로 이것을 자신의 것 위에 서게 할 수 있습니까? –

+0

cripto-js를 스크립트 태그 (웹 페이지에서 src를 다운로드하기 만하면 됨)에 넣고 위의 코드를 간단한 javascript 함수로 변환 할 수 있습니다. 인스턴스 변수는 글로벌 변수 일 수 있습니다. 가져 오기 및 클래스 문 옆에는 ES6과 관련된 내용이 없으므로 간단해야합니다. –