2014-01-04 18:59:55 +00:00
/ *
2018-01-04 00:52:49 +00:00
Copyright 2014 The Perkeep Authors
2014-01-04 18:59:55 +00:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
2014-01-07 04:52:30 +00:00
http : //www.apache.org/licenses/LICENSE-2.0
2014-01-04 18:59:55 +00:00
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2014-01-08 05:28:01 +00:00
goog . provide ( 'cam.blob' ) ;
2014-01-04 18:59:55 +00:00
goog . require ( 'goog.crypt' ) ;
goog . require ( 'goog.crypt.Sha1' ) ;
2018-01-17 11:44:11 +00:00
goog . require ( 'goog.crypt.Sha224' ) ;
2014-01-04 18:59:55 +00:00
2018-01-30 11:02:56 +00:00
// Returns the Perkeep blobref for hash object. The hash function is currently sha224 or sha1, but more might be added later.
2014-01-04 18:59:55 +00:00
// @param {!goog.crypt.Hash} hash
// @returns {!string}
2014-01-08 05:28:01 +00:00
cam . blob . refFromHash = function ( hash ) {
2018-01-17 11:44:11 +00:00
if ( hash instanceof goog . crypt . Sha224 ) {
return 'sha224-' + goog . crypt . byteArrayToHex ( hash . digest ( ) ) ;
}
2014-01-04 18:59:55 +00:00
if ( hash instanceof goog . crypt . Sha1 ) {
return 'sha1-' + goog . crypt . byteArrayToHex ( hash . digest ( ) ) ;
}
throw new Error ( 'Unsupported hash function type' ) ;
} ;
2018-01-30 11:02:56 +00:00
// Returns the Perkeep blobref for a string using the currently recommended hash function.
2014-01-04 18:59:55 +00:00
// @param {!string} str
// @returns {!string}
2014-01-08 05:28:01 +00:00
cam . blob . refFromString = function ( str ) {
var hash = cam . blob . createHash ( ) ;
2014-06-20 19:03:49 +00:00
// update only supports 8 bit chars: http://docs.closure-library.googlecode.com/git/class_goog_crypt_Sha1.html
hash . update ( goog . crypt . stringToUtf8ByteArray ( str ) ) ;
2014-01-08 05:28:01 +00:00
return cam . blob . refFromHash ( hash ) ;
2014-01-04 18:59:55 +00:00
} ;
2018-01-30 11:02:56 +00:00
// Returns the Perkeep blobref for a DOM blob (different from Perkeep blob) using the currently recommended hash function. This function currently only works within workers.
2014-01-04 18:59:55 +00:00
// @param {Blob} blob
// @returns {!string}
2014-01-08 05:28:01 +00:00
cam . blob . refFromDOMBlob = function ( blob ) {
2014-01-04 18:59:55 +00:00
if ( ! goog . global . FileReaderSync ) {
// TODO(aa): If necessary, we can also implement this using FileReader for use on the main thread. But beware that should not be done for very large objects without checking the effect on framerate carefully.
throw new Error ( 'FileReaderSync not available. Perhaps we are on the main thread?' ) ;
}
var fr = new FileReaderSync ( ) ;
2014-01-08 05:28:01 +00:00
var hash = cam . blob . createHash ( ) ;
2014-01-04 18:59:55 +00:00
var chunkSize = 1024 * 1024 ;
for ( var start = 0 ; start < blob . size ; start += chunkSize ) {
var end = Math . min ( start + chunkSize , blob . size ) ;
var slice = blob . slice ( start , end ) ;
hash . update ( new Uint8Array ( fr . readAsArrayBuffer ( slice ) ) ) ;
}
2014-01-08 05:28:01 +00:00
return cam . blob . refFromHash ( hash ) ;
2014-01-04 18:59:55 +00:00
} ;
// Creates an instance of the currently recommened hash function.
// @return {!goog.crypt.Hash'}
2014-01-08 05:28:01 +00:00
cam . blob . createHash = function ( ) {
2018-01-17 11:44:11 +00:00
return new goog . crypt . Sha224 ( ) ;
2014-01-04 18:59:55 +00:00
} ;