text: var vscode = require('vscode');
  var crypto = require('crypto');
  
  var encryptionAlgorithm = "aes-256-cbc";
  var hashAlgorithm = 'sha256'
  var keyLength=32;
  var ivLength=16;
  var iterations = 10000;
  var keyandIvLength = keyLength + ivLength;
  
  var SplitKeyAndIV = function (keyAndIv)
  {
      var keypair = {};
      keypair.key = Buffer.alloc(keyLength);
      keypair.iv = Buffer.alloc(ivLength);
      keyAndIv.copy(keypair.key,0,0,keyLength);
      keyAndIv.copy(keypair.iv,0,keyLength,keyandIvLength);
      return keypair;
  }
  
  var Encrypt = function (text, password)
  {
      var salt = crypto.randomBytes(8);
      var keypair = SplitKeyAndIV(crypto.pbkdf2Sync(password, salt, iterations, keyandIvLength, hashAlgorithm));
      
      var cipher = crypto.createCipheriv(encryptionAlgorithm, keypair.key, keypair.iv);
      var crypted = cipher.update(text,'utf8','binary');
      crypted += cipher.final('binary');
      
      var cryptedBuffer = Buffer.from(crypted, 'binary')
      var cryptedWithHeader = Buffer.alloc(16 + cryptedBuffer.length);
  
      Buffer.from('Salted__').copy(cryptedWithHeader, 0, 0, 8);
      salt.copy(cryptedWithHeader, 8, 0, 8);
      cryptedBuffer.copy(cryptedWithHeader, 16, 0);
  
      return Buffer.from(cryptedWithHeader, 'binary').toString('base64');
  }
  
  var Decrypt = function (text, password)
  {
      var cryptedWithHeader = Buffer.from(text, 'base64');
  
      if (cryptedWithHeader.length < 16)
      {
          vscode.window.showErrorMessage("Missing Salt Header");
          throw new Error("Missing Salt Header");
      }
  
      var header = Buffer.alloc(8);
      cryptedWithHeader.copy(header, 0, 0, 8);
  
      if (header.toString('utf8') !== "Salted__")
      {
          vscode.window.showErrorMessage("Invalid Salt Header");
          throw new Error("Invalid Salt Header");
      }
  
      var salt = Buffer.alloc(8);
      cryptedWithHeader.copy(salt, 0, 8, 16);
      var crypted = Buffer.alloc(cryptedWithHeader.length - 16);
      cryptedWithHeader.copy(crypted, 0, 16);
  
      var keypair = SplitKeyAndIV(crypto.pbkdf2Sync(password, salt, iterations, keyandIvLength, hashAlgorithm));
  
      var decipher = crypto.createDecipheriv(encryptionAlgorithm, keypair.key, keypair.iv);
      let decrypted = '';
        
      decipher.on('readable', () =>
      {
        let chunk;
        while (null !== (chunk = decipher.read()))
        {
          decrypted += chunk.toString('utf8');
        }
      });
  
      try
      {
          decipher.write(crypted, 'binary');
          decipher.end();
      }
      catch
      {
          vscode.window.showErrorMessage("Decryption Failed");
          throw new Error("Decryption Failed");
      }
  
      return decrypted;
  }
  
  var GetEditorText = function ()
  {
      return vscode.window.activeTextEditor.document.getText();
  }
  
  SetEditorText = function (text)
  {
      var e = vscode.window.activeTextEditor;
      var lastLine = e.document.lineCount-1;
      if(lastLine < 0) lastLine = 0;
  
      var lastChar = e.document.lineAt(lastLine).text.length;
      if(lastChar <= 0) lastChar = 1;
  
      e.selection = new vscode.Selection(new vscode.Position(0,0), new vscode.Position(lastLine,lastChar));
      e.edit(function(edit)
      {
          edit.replace(e.selection, text);
      });
  }
  
  exports.activate = function activate(context)
  {
      console.log('Encrypt Files Activated');
  
      const EncryptHandler = async () =>
      {
          if (!vscode.window.activeTextEditor) return;
  
          var pw1 = await vscode.window.showInputBox({password: true, prompt: "New Password"});
          if (pw1 === undefined) return;
  
          var pw2 = await vscode.window.showInputBox({password: true, prompt: "Confirm New Password"});
          if (pw2 === undefined) return;
  
          if (pw1 !== pw2)
          {
              vscode.window.showErrorMessage("Password Mismatch");
              return;
          }
  
          SetEditorText(Encrypt(GetEditorText(), pw1));
      };
  
      const DecryptHandler = async () =>
      {
          if (!vscode.window.activeTextEditor) return;
  
          var pw = await vscode.window.showInputBox({password: true, prompt: "Password"});
          if (pw === undefined) return;
  
          SetEditorText(Decrypt(GetEditorText(), pw));
      };
      
  
      context.subscriptions.push(vscode.commands.registerCommand('EncryptFiles.Encrypt', EncryptHandler));
      context.subscriptions.push(vscode.commands.registerCommand('EncryptFiles.Decrypt', DecryptHandler));
  }
  
  exports.deactivate = function deactivate()
  {
      console.log('Encrypt Files Deactivated');
  }
  