function decryptparsehead (filebuf,pass,progress) {
		if (progress === undefined) progress={stage:0};
		//check first 5 bytes
		if (progress.stage==0) {
			//need 5 bytes for stage 1;
			var file=filebuf.get(5);
			if (file==undefined) return progress;

			if (!(file[0]==0x41 && file[1]==0x45 && file[2]==0x53)){
				//return error
				//console.log("bad magic");
				return {error:"bad magic"}
			}
			
			if (file[3]!=02) {
				//return error
				//console.log("wrong file version, only supports v2");
				return {error:"wrong file version, only supports v2"}
			}
			progress.stage=1;
		}
		if (progress.stage==1) {
			file=filebuf.preview(2);
			if (file===undefined) return progress;
			while (file[0]!=0 || file[1]!=0 ) {
				var sig='';
				var extsize = (file[0] << 8) + file[1]+2;
				var lengthbytes=file;
				file=filebuf.get(extsize);
				if (file===undefined) return progress;
				// look for extended credential block
				if (file.length > 17 ) {
					sig=arrayToString(file.subarray(2,17));
					if (sig == "enckeyblk v 0.1") {
						var k=getanykey(file,pass,true,0);
						if (k.error=="") progress.key=k;
					}
				}
				file=filebuf.preview(2);
				if (file===undefined) return progress;
			}
			file=filebuf.get(2); //skip past the final 0x0000
			progress.stage=2;
		}
		if (progress.stage==2) {
			// this is our main credential block containing 
			// keyiv, encrypted key and file iv and hmac of ( encrypted key + file iv)
			file=filebuf.get(96);
			if (file===undefined) return progress;
			if (progress.key) {
				progress.key.stage=3;
				return progress.key
			} else { 
				var key=getkey(file,pass);
				key.stage=3;
				return key;
			}
			//this is the end of the header section
			//encrypted data is next
		}
	}
// cant use parsefile() if we really want to be able to handle incoming a byte at a time
cant use parsefile() if we really want to be able to handle incoming a byte at a time
cant use parsefile () if we really want to be able to handle incoming a byte at a time
function decryptparsehead (filebuf,pass,progress) {
		if (progress === undefined) progress={stage:0};
		if (progress.stage==0) {
			var file=filebuf.get(5);
			if (file==undefined) return progress;
			if (!(file[0]==0x41 && file[1]==0x45 && file[2]==0x53)){
				return {error:"bad magic"}
			}
			if (file[3]!=02) {
				return {error:"wrong file version, only supports v2"}
			}
			progress.stage=1;
		}
		if (progress.stage==1) {
			file=filebuf.preview(2);
			if (file===undefined) return progress;
			while (file[0]!=0 || file[1]!=0 ) {
				var sig='';
				var extsize = (file[0] << 8) + file[1]+2;
				var lengthbytes=file;
				file=filebuf.get(extsize);
				if (file===undefined) return progress;
				if (file.length > 17 ) {
					sig=arrayToString(file.subarray(2,17));
					if (sig == "enckeyblk v 0.1") {
						var k=getanykey(file,pass,true,0);
						if (k.error=="") progress.key=k;
					}
				}
				file=filebuf.preview(2);
				if (file===undefined) return progress;
			}
			file=filebuf.get(2); 
			progress.stage=2;
		}
		if (progress.stage==2) {
			file=filebuf.get(96);
			if (file===undefined) return progress;
			if (progress.key) {
				progress.key.stage=3;
				return progress.key
			} else { 
				var key=getkey(file,pass);
				key.stage=3;
				return key;
			}
		}
	}
function decryptparsehead ( filebuf , pass , progress ) { if ( progress === undefined ) progress = { stage : 0 } ; if ( progress . stage == 0 ) { var file = filebuf . get ( 5 ) ; if ( file == undefined ) return progress ; if ( ! ( file [ 0 ] == 0x41 && file [ 1 ] == 0x45 && file [ 2 ] == 0x53 ) ) { return { error : "bad magic" } } if ( file [ 3 ] != 02 ) { return { error : "wrong file version, only supports v2" } } progress . stage = 1 ; } if ( progress . stage == 1 ) { file = filebuf . preview ( 2 ) ; if ( file === undefined ) return progress ; while ( file [ 0 ] != 0 || file [ 1 ] != 0 ) { var sig = '' ; var extsize = ( file [ 0 ] << 8 ) + file [ 1 ] + 2 ; var lengthbytes = file ; file = filebuf . get ( extsize ) ; if ( file === undefined ) return progress ; if ( file . length > 17 ) { sig = arrayToString ( file . subarray ( 2 , 17 ) ) ; if ( sig == "enckeyblk v 0.1" ) { var k = getanykey ( file , pass , true , 0 ) ; if ( k . error == "" ) progress . key = k ; } } file = filebuf . preview ( 2 ) ; if ( file === undefined ) return progress ; } file = filebuf . get ( 2 ) ; progress . stage = 2 ; } if ( progress . stage == 2 ) { file = filebuf . get ( 96 ) ; if ( file === undefined ) return progress ; if ( progress . key ) { progress . key . stage = 3 ; return progress . key } else { var key = getkey ( file , pass ) ; key . stage = 3 ; return key ; } } }
cant use parsefile() if we really want to be able to handle incoming a byte at a time
cant use parsefile () if we really want to be able to handle incoming a byte at a time
//check first 5 bytes //need 5 bytes for stage 1; //return error //console.log("bad magic"); //return error //console.log("wrong file version, only supports v2"); // look for extended credential block //skip past the final 0x0000 // this is our main credential block containing  // keyiv, encrypted key and file iv and hmac of ( encrypted key + file iv) //this is the end of the header section //encrypted data is next