| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Asynchronous hash computer for the Blob interface. |
| 17 |
* |
| 18 |
* The Blob interface, part of the HTML5 File API, is supported on Chrome 7+, |
| 19 |
* Firefox 4.0 and Opera 11. No Blob interface implementation is expected on |
| 20 |
* Internet Explorer 10. Chrome 11, Firefox 5.0 and the subsequent release of |
| 21 |
* Opera are supposed to use vendor prefixes due to evolving API, see |
| 22 |
* http://dev.w3.org/2006/webapi/FileAPI/ for details. |
| 23 |
* |
| 24 |
* This implementation currently uses upcoming Chrome and Firefox prefixes, |
| 25 |
* plus the original Blob.slice specification, as implemented on Chrome 10 |
| 26 |
* and Firefox 4.0. |
| 27 |
* |
| 28 |
*/ |
| 29 |
|
| 30 |
goog.provide('goog.crypt.BlobHasher'); |
| 31 |
goog.provide('goog.crypt.BlobHasher.EventType'); |
| 32 |
|
| 33 |
goog.require('goog.asserts'); |
| 34 |
goog.require('goog.events.EventTarget'); |
| 35 |
goog.require('goog.fs'); |
| 36 |
goog.require('goog.log'); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Construct the hash computer. |
| 42 |
* |
| 43 |
* @param {!goog.crypt.Hash} hashFn The hash function to use. |
| 44 |
* @param {number=} opt_blockSize Processing block size. |
| 45 |
* @constructor |
| 46 |
* @struct |
| 47 |
* @extends {goog.events.EventTarget} |
| 48 |
* @final |
| 49 |
*/ |
| 50 |
goog.crypt.BlobHasher = function(hashFn, opt_blockSize) { |
| 51 |
goog.crypt.BlobHasher.base(this, 'constructor'); |
| 52 |
|
| 53 |
/** |
| 54 |
* The actual hash function. |
| 55 |
* @type {!goog.crypt.Hash} |
| 56 |
* @private |
| 57 |
*/ |
| 58 |
this.hashFn_ = hashFn; |
| 59 |
|
| 60 |
/** |
| 61 |
* The blob being processed or null if no blob is being processed. |
| 62 |
* @type {Blob} |
| 63 |
* @private |
| 64 |
*/ |
| 65 |
this.blob_ = null; |
| 66 |
|
| 67 |
/** |
| 68 |
* Computed hash value. |
| 69 |
* @type {Array<number>} |
| 70 |
* @private |
| 71 |
*/ |
| 72 |
this.hashVal_ = null; |
| 73 |
|
| 74 |
/** |
| 75 |
* Number of bytes already processed. |
| 76 |
* @type {number} |
| 77 |
* @private |
| 78 |
*/ |
| 79 |
this.bytesProcessed_ = 0; |
| 80 |
|
| 81 |
/** |
| 82 |
* The number of bytes to hash or Infinity for no limit. |
| 83 |
* @type {number} |
| 84 |
* @private |
| 85 |
*/ |
| 86 |
this.hashingLimit_ = Infinity; |
| 87 |
|
| 88 |
/** |
| 89 |
* Processing block size. |
| 90 |
* @type {number} |
| 91 |
* @private |
| 92 |
*/ |
| 93 |
this.blockSize_ = opt_blockSize || 5000000; |
| 94 |
|
| 95 |
/** |
| 96 |
* File reader object. Will be null if no chunk is currently being read. |
| 97 |
* @type {FileReader} |
| 98 |
* @private |
| 99 |
*/ |
| 100 |
this.fileReader_ = null; |
| 101 |
|
| 102 |
/** |
| 103 |
* The logger used by this object. |
| 104 |
* @type {goog.log.Logger} |
| 105 |
* @private |
| 106 |
*/ |
| 107 |
this.logger_ = goog.log.getLogger('goog.crypt.BlobHasher'); |
| 108 |
}; |
| 109 |
goog.inherits(goog.crypt.BlobHasher, goog.events.EventTarget); |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Event names for hash computation events |
| 114 |
* @enum {string} |
| 115 |
*/ |
| 116 |
goog.crypt.BlobHasher.EventType = { |
| 117 |
STARTED: 'started', |
| 118 |
PROGRESS: 'progress', |
| 119 |
THROTTLED: 'throttled', |
| 120 |
COMPLETE: 'complete', |
| 121 |
ABORT: 'abort', |
| 122 |
ERROR: 'error' |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* Start the hash computation. |
| 128 |
* @param {!Blob} blob The blob of data to compute the hash for. |
| 129 |
*/ |
| 130 |
goog.crypt.BlobHasher.prototype.hash = function(blob) { |
| 131 |
this.abort(); |
| 132 |
this.hashFn_.reset(); |
| 133 |
this.blob_ = blob; |
| 134 |
this.hashVal_ = null; |
| 135 |
this.bytesProcessed_ = 0; |
| 136 |
this.dispatchEvent(goog.crypt.BlobHasher.EventType.STARTED); |
| 137 |
|
| 138 |
this.processNextBlock_(); |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* Sets the maximum number of bytes to hash or Infinity for no limit. Can be |
| 144 |
* called before hash() to throttle the hash computation. The hash computation |
| 145 |
* can then be continued by repeatedly calling setHashingLimit() with greater |
| 146 |
* byte offsets. This is useful if you don't need the hash until some time in |
| 147 |
* the future, for example when uploading a file and you don't need the hash |
| 148 |
* until the transfer is complete. |
| 149 |
* @param {number} byteOffset The byte offset to compute the hash up to. |
| 150 |
* Should be a non-negative integer or Infinity for no limit. Negative |
| 151 |
* values are not allowed. |
| 152 |
*/ |
| 153 |
goog.crypt.BlobHasher.prototype.setHashingLimit = function(byteOffset) { |
| 154 |
goog.asserts.assert(byteOffset >= 0, 'Hashing limit must be non-negative.'); |
| 155 |
this.hashingLimit_ = byteOffset; |
| 156 |
|
| 157 |
// Resume processing if a blob is currently being hashed, but no block read |
| 158 |
// is currently in progress. |
| 159 |
if (this.blob_ && !this.fileReader_) { |
| 160 |
this.processNextBlock_(); |
| 161 |
} |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* Abort hash computation. |
| 167 |
*/ |
| 168 |
goog.crypt.BlobHasher.prototype.abort = function() { |
| 169 |
if (this.fileReader_) { |
| 170 |
this.fileReader_.abort(); |
| 171 |
this.fileReader_ = null; |
| 172 |
} |
| 173 |
|
| 174 |
if (this.blob_) { |
| 175 |
this.blob_ = null; |
| 176 |
this.dispatchEvent(goog.crypt.BlobHasher.EventType.ABORT); |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* @return {number} Number of bytes processed so far. |
| 183 |
*/ |
| 184 |
goog.crypt.BlobHasher.prototype.getBytesProcessed = function() { |
| 185 |
return this.bytesProcessed_; |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
/** |
| 190 |
* @return {Array<number>} The computed hash value or null if not ready. |
| 191 |
*/ |
| 192 |
goog.crypt.BlobHasher.prototype.getHash = function() { |
| 193 |
return this.hashVal_; |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
/** |
| 198 |
* Helper function setting up the processing for the next block, or finalizing |
| 199 |
* the computation if all blocks were processed. |
| 200 |
* @private |
| 201 |
*/ |
| 202 |
goog.crypt.BlobHasher.prototype.processNextBlock_ = function() { |
| 203 |
goog.asserts.assert(this.blob_, 'A hash computation must be in progress.'); |
| 204 |
|
| 205 |
if (this.bytesProcessed_ < this.blob_.size) { |
| 206 |
if (this.hashingLimit_ <= this.bytesProcessed_) { |
| 207 |
// Throttle limit reached. Wait until we are allowed to hash more bytes. |
| 208 |
this.dispatchEvent(goog.crypt.BlobHasher.EventType.THROTTLED); |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
// We have to reset the FileReader every time, otherwise it fails on |
| 213 |
// Chrome, including the latest Chrome 12 beta. |
| 214 |
// http://code.google.com/p/chromium/issues/detail?id=82346 |
| 215 |
this.fileReader_ = new FileReader(); |
| 216 |
this.fileReader_.onload = goog.bind(this.onLoad_, this); |
| 217 |
this.fileReader_.onerror = goog.bind(this.onError_, this); |
| 218 |
|
| 219 |
var endOffset = Math.min(this.hashingLimit_, this.blob_.size); |
| 220 |
var size = Math.min(endOffset - this.bytesProcessed_, this.blockSize_); |
| 221 |
var chunk = goog.fs.sliceBlob( |
| 222 |
this.blob_, this.bytesProcessed_, this.bytesProcessed_ + size); |
| 223 |
if (!chunk || chunk.size != size) { |
| 224 |
goog.log.error(this.logger_, 'Failed slicing the blob'); |
| 225 |
this.onError_(); |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
if (this.fileReader_.readAsArrayBuffer) { |
| 230 |
this.fileReader_.readAsArrayBuffer(chunk); |
| 231 |
} else if (this.fileReader_.readAsBinaryString) { |
| 232 |
this.fileReader_.readAsBinaryString(chunk); |
| 233 |
} else { |
| 234 |
goog.log.error(this.logger_, 'Failed calling the chunk reader'); |
| 235 |
this.onError_(); |
| 236 |
} |
| 237 |
} else { |
| 238 |
this.hashVal_ = this.hashFn_.digest(); |
| 239 |
this.blob_ = null; |
| 240 |
this.dispatchEvent(goog.crypt.BlobHasher.EventType.COMPLETE); |
| 241 |
} |
| 242 |
}; |
| 243 |
|
| 244 |
|
| 245 |
/** |
| 246 |
* Handle processing block loaded. |
| 247 |
* @private |
| 248 |
*/ |
| 249 |
goog.crypt.BlobHasher.prototype.onLoad_ = function() { |
| 250 |
goog.log.info(this.logger_, 'Successfully loaded a chunk'); |
| 251 |
|
| 252 |
var array = null; |
| 253 |
if (this.fileReader_.result instanceof Array || |
| 254 |
goog.isString(this.fileReader_.result)) { |
| 255 |
array = this.fileReader_.result; |
| 256 |
} else if ( |
| 257 |
goog.global['ArrayBuffer'] && goog.global['Uint8Array'] && |
| 258 |
this.fileReader_.result instanceof ArrayBuffer) { |
| 259 |
array = new Uint8Array(this.fileReader_.result); |
| 260 |
} |
| 261 |
if (!array) { |
| 262 |
goog.log.error(this.logger_, 'Failed reading the chunk'); |
| 263 |
this.onError_(); |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
this.hashFn_.update(array); |
| 268 |
this.bytesProcessed_ += array.length; |
| 269 |
this.fileReader_ = null; |
| 270 |
this.dispatchEvent(goog.crypt.BlobHasher.EventType.PROGRESS); |
| 271 |
|
| 272 |
this.processNextBlock_(); |
| 273 |
}; |
| 274 |
|
| 275 |
|
| 276 |
/** |
| 277 |
* Handles error. |
| 278 |
* @private |
| 279 |
*/ |
| 280 |
goog.crypt.BlobHasher.prototype.onError_ = function() { |
| 281 |
this.fileReader_ = null; |
| 282 |
this.blob_ = null; |
| 283 |
this.dispatchEvent(goog.crypt.BlobHasher.EventType.ERROR); |
| 284 |
}; |