function encryptstart(pass,extrakeyslots,noforge){
		var headstart = new Uint8Array(5);
		var endext = new Uint8Array(2);//0x0000 for no more extensions
		var extensions=[];
		var bufarray=new ArrayBuffer(96);
		var bufi=0;
		var buffer=new Uint8Array(bufarray);
		var cred='';
		var keyiv, extlen=0, output, 
		 i=0, //blank=new String;
		 blank;
		var emptydata=new ArrayBuffer(0);
		var cipher, hmac, extrapasses=[];
		//console.log(toType(pass));
		if (toType(pass)=='array') {
			extrapasses=pass;
			pass=extrapasses.shift();
			//console.log(pass);
			//console.log(extrapasses);
		}
		
		
		// TODO: support for other than 16, and for extending a block that is too small to fit another 
		//       password (in addpass()).  Right now, a code review of other projects would be required
		//       since they might rely on file header size being constant (dunno, hence need for review).
		// if (extrakeyslots===undefined) 
			extrakeyslots=16;
		// max size of extension is 65536
		if (extrakeyslots>682) extrakeyslots=682;
		copyToArray("AES",headstart,0);
		headstart[3]=2;
		
		//headstart[4]=0; //aready 0;
		
		// make credentials
		//this is now mostly solved and should be successful on every attempt
		var i=0;
		while ( cred=='' && i++ < 9 ) {
			//if(i>1) console.log("FAILED making new credential block");
			cred=newcredentialblock(pass);
		}
		if (cred =='') {
			//console.log("tried 8 time, bailing...");
			return {head:emptydata,error:"could not make a new set of credentials"};
		}

		/* **** add extensions here **** */
		
		extensions.push(makeext(["CREATED_BY","aescrypt.js 0.1"]));
			
		//add blank 128 byte extension
		
		blank=new Uint8Array(130);
		blank.set([128],1);
		extensions.push(blank);
		
		//add extended credential block
		//and add extra passwords, if available;
		if (extrakeyslots>0) {
			var csize=16+(96*extrakeyslots);
			var h= String.fromCharCode(csize >>> 8) + String.fromCharCode(csize  & 0xFF)+"enckeyblk v 0.1"+String.fromCharCode(0);
			blank=new Uint8Array(csize+2);
			copyToArray(h,blank);
			var len=(extrapasses.length<16)?extrapasses.length:16;
			for (var i=0; i<len;i++) {
				var newpass=extrapasses[i],
				    newblock=newcredentialblock(newpass,cred.key,cred.fileiv),
				    start=18+(i*96);
				
				copyToArray(newblock.buffer,blank,start);
			}
			extensions.push(blank);
		}
		
		/*
		blank=String.fromCharCode(0)+String.fromCharCode(128);
		for (var i=0;i<128;i++)
			blank+=String.fromCharCode(0);
		extensions.push(blank);	
		
		//add an area for 16 96-byte blocks for extra keyiv-enckeyblk-hmac combos ("credential blocks")
		blank=String.fromCharCode(06)+String.fromCharCode(16)+"enckeyblk v 0.1"+String.fromCharCode(0);
		for (var i=0;i<1536;i++)
			blank+=String.fromCharCode(0);
		extensions.push(blank);
		*/	


		/* **** end add extensions  **** */
		
		//the last extension  - 0x0000 to mark end of extensions
		extensions.push(endext);	

		// get length of all extensions
		for (var i=0;i<extensions.length;i++)
			extlen+=extensions[i].length;

		output=new Uint8Array(
			headstart.length +
			extlen+
			cred.buffer.length
		);

		bufi=0;
		bufi+=	copyToArray( headstart,		output,bufi);
		for (var i=0;i<extensions.length;i++)
			bufi+=copyToArray( extensions[i],       output,bufi);
		bufi+=	copyToArray( cred.buffer,	      output,bufi);
		
		if (!noforge) {
			cipher = forge.cipher.createCipher('AES-CBC', cred.key);
			cipher.start({iv: cred.fileiv});
		
			hmac = forge.hmac.create();
			hmac.start('sha256', cred.key);
			return {head:output,error:"", cipher:cipher, hmac: hmac};
		}
		
		return {head:output,error:"", fileiv: cred.fileiv, key: cred.key};
	}
//* set up file header and return keys and iv
set up file header and return keys and iv
set up file header and return keys and iv
function encryptstart(pass,extrakeyslots,noforge){
		var headstart = new Uint8Array(5);
		var endext = new Uint8Array(2);
		var extensions=[];
		var bufarray=new ArrayBuffer(96);
		var bufi=0;
		var buffer=new Uint8Array(bufarray);
		var cred='';
		var keyiv, extlen=0, output, 
		 i=0, 
		 blank;
		var emptydata=new ArrayBuffer(0);
		var cipher, hmac, extrapasses=[];
		if (toType(pass)=='array') {
			extrapasses=pass;
			pass=extrapasses.shift();
		}
			extrakeyslots=16;
		if (extrakeyslots>682) extrakeyslots=682;
		copyToArray("AES",headstart,0);
		headstart[3]=2;
		var i=0;
		while ( cred=='' && i++ < 9 ) {
			cred=newcredentialblock(pass);
		}
		if (cred =='') {
			return {head:emptydata,error:"could not make a new set of credentials"};
		}
		extensions.push(makeext(["CREATED_BY","aescrypt.js 0.1"]));
		blank=new Uint8Array(130);
		blank.set([128],1);
		extensions.push(blank);
		if (extrakeyslots>0) {
			var csize=16+(96*extrakeyslots);
			var h= String.fromCharCode(csize >>> 8) + String.fromCharCode(csize  & 0xFF)+"enckeyblk v 0.1"+String.fromCharCode(0);
			blank=new Uint8Array(csize+2);
			copyToArray(h,blank);
			var len=(extrapasses.length<16)?extrapasses.length:16;
			for (var i=0; i<len;i++) {
				var newpass=extrapasses[i],
				    newblock=newcredentialblock(newpass,cred.key,cred.fileiv),
				    start=18+(i*96);
				copyToArray(newblock.buffer,blank,start);
			}
			extensions.push(blank);
		}
		extensions.push(endext);	
		for (var i=0;i<extensions.length;i++)
			extlen+=extensions[i].length;
		output=new Uint8Array(
			headstart.length +
			extlen+
			cred.buffer.length
		);
		bufi=0;
		bufi+=	copyToArray( headstart,		output,bufi);
		for (var i=0;i<extensions.length;i++)
			bufi+=copyToArray( extensions[i],       output,bufi);
		bufi+=	copyToArray( cred.buffer,	      output,bufi);
		if (!noforge) {
			cipher = forge.cipher.createCipher('AES-CBC', cred.key);
			cipher.start({iv: cred.fileiv});
			hmac = forge.hmac.create();
			hmac.start('sha256', cred.key);
			return {head:output,error:"", cipher:cipher, hmac: hmac};
		}
		return {head:output,error:"", fileiv: cred.fileiv, key: cred.key};
	}
function encryptstart ( pass , extrakeyslots , noforge ) { var headstart = new Uint8Array ( 5 ) ; var endext = new Uint8Array ( 2 ) ; var extensions = [ ] ; var bufarray = new ArrayBuffer ( 96 ) ; var bufi = 0 ; var buffer = new Uint8Array ( bufarray ) ; var cred = '' ; var keyiv , extlen = 0 , output , i = 0 , blank ; var emptydata = new ArrayBuffer ( 0 ) ; var cipher , hmac , extrapasses = [ ] ; if ( toType ( pass ) == 'array' ) { extrapasses = pass ; pass = extrapasses . shift ( ) ; } extrakeyslots = 16 ; if ( extrakeyslots > 682 ) extrakeyslots = 682 ; copyToArray ( "AES" , headstart , 0 ) ; headstart [ 3 ] = 2 ; var i = 0 ; while ( cred == '' && i ++ < 9 ) { cred = newcredentialblock ( pass ) ; } if ( cred == '' ) { return { head : emptydata , error : "could not make a new set of credentials" } ; } extensions . push ( makeext ( [ "CREATED_BY" , "aescrypt.js 0.1" ] ) ) ; blank = new Uint8Array ( 130 ) ; blank . set ( [ 128 ] , 1 ) ; extensions . push ( blank ) ; if ( extrakeyslots > 0 ) { var csize = 16 + ( 96 * extrakeyslots ) ; var h = String . fromCharCode ( csize >>> 8 ) + String . fromCharCode ( csize & 0xFF ) + "enckeyblk v 0.1" + String . fromCharCode ( 0 ) ; blank = new Uint8Array ( csize + 2 ) ; copyToArray ( h , blank ) ; var len = ( extrapasses . length < 16 ) ? extrapasses . length : 16 ; for ( var i = 0 ; i < len ; i ++ ) { var newpass = extrapasses [ i ] , newblock = newcredentialblock ( newpass , cred . key , cred . fileiv ) , start = 18 + ( i * 96 ) ; copyToArray ( newblock . buffer , blank , start ) ; } extensions . push ( blank ) ; } extensions . push ( endext ) ; for ( var i = 0 ; i < extensions . length ; i ++ ) extlen += extensions [ i ] . length ; output = new Uint8Array ( headstart . length + extlen + cred . buffer . length ) ; bufi = 0 ; bufi += copyToArray ( headstart , output , bufi ) ; for ( var i = 0 ; i < extensions . length ; i ++ ) bufi += copyToArray ( extensions [ i ] , output , bufi ) ; bufi += copyToArray ( cred . buffer , output , bufi ) ; if ( ! noforge ) { cipher = forge . cipher . createCipher ( 'AES-CBC' , cred . key ) ; cipher . start ( { iv : cred . fileiv } ) ; hmac = forge . hmac . create ( ) ; hmac . start ( 'sha256' , cred . key ) ; return { head : output , error : "" , cipher : cipher , hmac : hmac } ; } return { head : output , error : "" , fileiv : cred . fileiv , key : cred . key } ; }
set up file header and return keys and iv
set up file header and return keys and iv
//0x0000 for no more extensions //blank=new String; //console.log(toType(pass)); //console.log(pass); //console.log(extrapasses); // TODO: support for other than 16, and for extending a block that is too small to fit another  //       password (in addpass()).  Right now, a code review of other projects would be required //       since they might rely on file header size being constant (dunno, hence need for review). // if (extrakeyslots===undefined)  // max size of extension is 65536 //headstart[4]=0; //aready 0; // make credentials //this is now mostly solved and should be successful on every attempt //if(i>1) console.log("FAILED making new credential block"); //console.log("tried 8 time, bailing..."); /* **** add extensions here **** */ //add blank 128 byte extension //add extended credential block //and add extra passwords, if available; /*
		blank=String.fromCharCode(0)+String.fromCharCode(128);
		for (var i=0;i<128;i++)
			blank+=String.fromCharCode(0);
		extensions.push(blank);	
		
		//add an area for 16 96-byte blocks for extra keyiv-enckeyblk-hmac combos ("credential blocks")
		blank=String.fromCharCode(06)+String.fromCharCode(16)+"enckeyblk v 0.1"+String.fromCharCode(0);
		for (var i=0;i<1536;i++)
			blank+=String.fromCharCode(0);
		extensions.push(blank);
		*/ /* **** end add extensions  **** */ //the last extension  - 0x0000 to mark end of extensions // get length of all extensions