B2G/gecko/toolkit/mozapps/update/nsUpdateService.js
/**
   * Verify the downloaded file.  We assume that the download is complete at
   * this point.
   */
Verify the downloaded file.  We assume that the download is complete at
this point.
Verify the downloaded file . We assume that the download is complete at this point .
function Downloader__verifyDownload() {
    LOG("Downloader:_verifyDownload called");
    if (!this._request)
      return false;
    var destination = this._request.destination;
    if (destination.fileSize != this._patch.size) {
      LOG("Downloader:_verifyDownload downloaded size != expected size.");
      return false;
    }
    LOG("Downloader:_verifyDownload downloaded size == expected size.");
    var fileStream = Cc["@mozilla.org/network/file-input-stream;1"].
                     createInstance(Ci.nsIFileInputStream);
    fileStream.init(destination, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, 0);
    try {
      var hash = Cc["@mozilla.org/security/hash;1"].
                 createInstance(Ci.nsICryptoHash);
      var hashFunction = Ci.nsICryptoHash[this._patch.hashFunction.toUpperCase()];
      if (hashFunction == undefined)
        throw Cr.NS_ERROR_UNEXPECTED;
      hash.init(hashFunction);
      hash.updateFromStream(fileStream, -1);
      digest = binaryToHex(hash.finish(false));
    } catch (e) {
      LOG("Downloader:_verifyDownload - failed to compute hash of the " +
          "downloaded update archive");
      digest = "";
    }
    fileStream.close();
    if (digest == this._patch.hashValue.toLowerCase()) {
      LOG("Downloader:_verifyDownload hashes match.");
      return true;
    }
    LOG("Downloader:_verifyDownload hashes do not match. ");
    return false;
  }
function Downloader__verifyDownload ( ) { LOG ( "Downloader:_verifyDownload called" ) ; if ( ! this . _request ) return false ; var destination = this . _request . destination ; if ( destination . fileSize != this . _patch . size ) { LOG ( "Downloader:_verifyDownload downloaded size != expected size." ) ; return false ; } LOG ( "Downloader:_verifyDownload downloaded size == expected size." ) ; var fileStream = Cc [ "@mozilla.org/network/file-input-stream;1" ] . createInstance ( Ci . nsIFileInputStream ) ; fileStream . init ( destination , FileUtils . MODE_RDONLY , FileUtils . PERMS_FILE , 0 ) ; try { var hash = Cc [ "@mozilla.org/security/hash;1" ] . createInstance ( Ci . nsICryptoHash ) ; var hashFunction = Ci . nsICryptoHash [ this . _patch . hashFunction . toUpperCase ( ) ] ; if ( hashFunction == undefined ) throw Cr . NS_ERROR_UNEXPECTED ; hash . init ( hashFunction ) ; hash . updateFromStream ( fileStream , - 1 ) ; digest = binaryToHex ( hash . finish ( false ) ) ; } catch ( e ) { LOG ( "Downloader:_verifyDownload - failed to compute hash of the " + "downloaded update archive" ) ; digest = "" ; } fileStream . close ( ) ; if ( digest == this . _patch . hashValue . toLowerCase ( ) ) { LOG ( "Downloader:_verifyDownload hashes match." ) ; return true ; } LOG ( "Downloader:_verifyDownload hashes do not match. " ) ; return false ; }
Verify the downloaded file.
Verify the downloaded file .
// Ensure that the file size matches the expected file size. // NOTE: For now, we assume that the format of _patch.hashValue is hex // encoded binary (such as what is typically output by programs like // sha1sum).  In the future, this may change to base64 depending on how // we choose to compute these hashes.