examples/jobs-agent.js
function validateSignature(fileName, signature, cb) {
   if (isUndefined(signature) || isUndefined(signature.codesign)) {
      cb();
      return;
   }

   if (isUndefined(codeSignCertFileName)) {
      cb(new Error('No code sign certificate file specified'));
      return;
   }

   var codeSignCert;

   try {
      codeSignCert = fs.readFileSync(codeSignCertFileName, 'utf8');
   } catch (err) {
      // unable to read codeSignCertFileName file
      cb(new Error('Error encountered trying to read ' + codeSignCertFileName));
      return;
   }

   var verify;
   try {
      verify = crypto.createVerify(signature.codesign.signatureAlgorithm);
   } catch (err) {
      console.warn('Unable to use signature algorithm: ' + signature.codesign.signatureAlgorithm + ' attempting to use default algorithm SHA256');
      try {
         verify = crypto.createVerify('SHA256');
      } catch (err) {
         cb(err);
         return;
      }
   }

   var stream = fs.createReadStream(fileName);

   stream.on('data', function (data) {
      verify.write(data);
   }).on('end', function () {
      verify.end();

      try {
         if (!verify.verify(codeSignCert, signature.codesign.signature, 'base64')) {
            var err = new Error('Signature validation failed');
            err.fileName = fileName;
            cb(err);
         } else {
            cb();
         }
      } catch (err) {
         cb(err);
         return;
      }
   }).on('error', function (err) {
      err.fileName = fileName;
      cb(err);
   });
}
function validateSignature(fileName, signature, cb) {
   if (isUndefined(signature) || isUndefined(signature.codesign)) {
      cb();
      return;
   }

   if (isUndefined(codeSignCertFileName)) {
      cb(new Error('No code sign certificate file specified'));
      return;
   }

   var codeSignCert;

   try {
      codeSignCert = fs.readFileSync(codeSignCertFileName, 'utf8');
   } catch (err) {
      // unable to read codeSignCertFileName file
      cb(new Error('Error encountered trying to read ' + codeSignCertFileName));
      return;
   }

   var verify;
   try {
      verify = crypto.createVerify(signature.codesign.signatureAlgorithm);
   } catch (err) {
      console.warn('Unable to use signature algorithm: ' + signature.codesign.signatureAlgorithm + ' attempting to use default algorithm SHA256');
      try {
         verify = crypto.createVerify('SHA256');
      } catch (err) {
         cb(err);
         return;
      }
   }

   var stream = fs.createReadStream(fileName);

   stream.on('data', function (data) {
      verify.write(data);
   }).on('end', function () {
      verify.end();

      try {
         if (!verify.verify(codeSignCert, signature.codesign.signature, 'base64')) {
            var err = new Error('Signature validation failed');
            err.fileName = fileName;
            cb(err);
         } else {
            cb();
         }
      } catch (err) {
         cb(err);
         return;
      }
   }).on('error', function (err) {
      err.fileName = fileName;
      cb(err);
   });
}
function validateSignature ( fileName , signature , cb ) { if ( isUndefined ( signature ) || isUndefined ( signature . codesign ) ) { cb ( ) ; return ; } if ( isUndefined ( codeSignCertFileName ) ) { cb ( new Error ( 'No code sign certificate file specified' ) ) ; return ; } var codeSignCert ; try { codeSignCert = fs . readFileSync ( codeSignCertFileName , 'utf8' ) ; } catch ( err ) { // unable to read codeSignCertFileName file cb ( new Error ( 'Error encountered trying to read ' + codeSignCertFileName ) ) ; return ; } var verify ; try { verify = crypto . createVerify ( signature . codesign . signatureAlgorithm ) ; } catch ( err ) { console . warn ( 'Unable to use signature algorithm: ' + signature . codesign . signatureAlgorithm + ' attempting to use default algorithm SHA256' ) ; try { verify = crypto . createVerify ( 'SHA256' ) ; } catch ( err ) { cb ( err ) ; return ; } } var stream = fs . createReadStream ( fileName ) ; stream . on ( 'data' , function ( data ) { verify . write ( data ) ; } ) . on ( 'end' , function ( ) { verify . end ( ) ; try { if ( ! verify . verify ( codeSignCert , signature . codesign . signature , 'base64' ) ) { var err = new Error ( 'Signature validation failed' ) ; err . fileName = fileName ; cb ( err ) ; } else { cb ( ) ; } } catch ( err ) { cb ( err ) ; return ; } } ) . on ( 'error' , function ( err ) { err . fileName = fileName ; cb ( err ) ; } ) ; }
Private function to validate signature
Private function to validate signature