protected synchronized void readAndVerify() throws InvalidKeyException, IOException, FileEncryptionException, IllegalBlockSizeException, BadPaddingException, FileIntegrityException {
  if (backingRandomAccessFile.length() < headerSize()) {
    throw new FileEncryptionException("Invalid file header");
  }
  if (shareKey == null || shareKey.getEncoded().length == 0) {
    throw new FileEncryptionException("Invalid share key in encrypting random access file!");
  }
 else {
    headerAuthHMac.reset();
    KeyParameter keyParameter=new KeyParameter(shareKey.getEncoded());
    headerAuthHMac.init(keyParameter);
  }
  long oldpos=backingRandomAccessFile.getFilePointer();
  backingRandomAccessFile.seek(0);
  byte[] header_data=new byte[headerSize() - FileHeader.AUTH_TAG_SIZE];
  backingRandomAccessFile.read(header_data);
  backingRandomAccessFile.read(headerAuthTag);
  backingRandomAccessFile.seek(oldpos);
  headerAuthHMac.update(header_data,0,header_data.length);
  byte[] hmacRef=new byte[AUTH_TAG_SIZE];
  headerAuthHMac.doFinal(hmacRef,0);
  if (!Arrays.equals(hmacRef,headerAuthTag)) {
    throw new FileIntegrityException("HMac of file header is invalid!");
  }
 else {
    DataInputStream istream=new DataInputStream(new ByteArrayInputStream(header_data));
    byte[] tmpmagic=new byte[PANBOX_FILE_MAGIC.length];
    istream.read(tmpmagic);
    if (!Arrays.equals(tmpmagic,PANBOX_FILE_MAGIC)) {
      throw new FileEncryptionException("Invalid magic number in file header");
    }
    byte[] tmpversion=new byte[PANBOX_FILE_VERSION.length];
    istream.read(tmpversion);
    if (!Arrays.equals(tmpversion,PANBOX_FILE_VERSION)) {
      throw new FileEncryptionException("Invalid version in file header. Expected version is " + PANBOX_FILE_VERSION.toString());
    }
    this.shareKeyVersion=istream.readInt();
    byte[] tmpencryptedFileKey=new byte[KeyConstants.SYMMETRIC_FILE_KEY_SIZE_BYTES];
    istream.read(tmpencryptedFileKey);
    filekeyCipher.init(Cipher.DECRYPT_MODE,shareKey);
    this.decryptedFileKey=new SecretKeySpec(filekeyCipher.doFinal(tmpencryptedFileKey),getAlgorithmIdentifier());
    if (implementsAuthentication()) {
      byte[] tmpFileAuthBuf=new byte[AuthTagVerifier.AUTH_TAG_SIZE];
      istream.read(tmpFileAuthBuf);
      byte[] zeroBuf=new byte[AuthTagVerifier.AUTH_TAG_SIZE];
      Arrays.fill(zeroBuf,(byte)0x00);
      if (Arrays.equals(zeroBuf,tmpFileAuthBuf)) {
        setFileAuthTag(null);
      }
 else {
        setFileAuthTag(tmpFileAuthBuf);
      }
    }
    istream.close();
  }
}

protected synchronized void readAndVerify ( ) throws InvalidKeyException , IOException , FileEncryptionException , IllegalBlockSizeException , BadPaddingException , FileIntegrityException { if ( backingRandomAccessFile . length ( ) < headerSize ( ) ) { throw new FileEncryptionException ( "Invalid file header" ) ; } if ( shareKey == null || shareKey . getEncoded ( ) . length == 0 ) { throw new FileEncryptionException ( "Invalid share key in encrypting random access file!" ) ; } else { headerAuthHMac . reset ( ) ; KeyParameter keyParameter = new KeyParameter ( shareKey . getEncoded ( ) ) ; headerAuthHMac . init ( keyParameter ) ; } long oldpos = backingRandomAccessFile . getFilePointer ( ) ; backingRandomAccessFile . seek ( 0 ) ; byte [ ] header_data = new byte [ headerSize ( ) - FileHeader . AUTH_TAG_SIZE ] ; backingRandomAccessFile . read ( header_data ) ; backingRandomAccessFile . read ( headerAuthTag ) ; backingRandomAccessFile . seek ( oldpos ) ; headerAuthHMac . update ( header_data , 0 , header_data . length ) ; byte [ ] hmacRef = new byte [ AUTH_TAG_SIZE ] ; headerAuthHMac . doFinal ( hmacRef , 0 ) ; if ( ! Arrays . equals ( hmacRef , headerAuthTag ) ) { throw new FileIntegrityException ( "HMac of file header is invalid!" ) ; } else { DataInputStream istream = new DataInputStream ( new ByteArrayInputStream ( header_data ) ) ; byte [ ] tmpmagic = new byte [ PANBOX_FILE_MAGIC . length ] ; istream . read ( tmpmagic ) ; if ( ! Arrays . equals ( tmpmagic , PANBOX_FILE_MAGIC ) ) { throw new FileEncryptionException ( "Invalid magic number in file header" ) ; } byte [ ] tmpversion = new byte [ PANBOX_FILE_VERSION . length ] ; istream . read ( tmpversion ) ; if ( ! Arrays . equals ( tmpversion , PANBOX_FILE_VERSION ) ) { throw new FileEncryptionException ( "Invalid version in file header. Expected version is " + PANBOX_FILE_VERSION . toString ( ) ) ; } this . shareKeyVersion = istream . readInt ( ) ; byte [ ] tmpencryptedFileKey = new byte [ KeyConstants . SYMMETRIC_FILE_KEY_SIZE_BYTES ] ; istream . read ( tmpencryptedFileKey ) ; filekeyCipher . init ( Cipher . DECRYPT_MODE , shareKey ) ; this . decryptedFileKey = new SecretKeySpec ( filekeyCipher . doFinal ( tmpencryptedFileKey ) , getAlgorithmIdentifier ( ) ) ; if ( implementsAuthentication ( ) ) { byte [ ] tmpFileAuthBuf = new byte [ AuthTagVerifier . AUTH_TAG_SIZE ] ; istream . read ( tmpFileAuthBuf ) ; byte [ ] zeroBuf = new byte [ AuthTagVerifier . AUTH_TAG_SIZE ] ; Arrays . fill ( zeroBuf , ( byte ) 0x00 ) ; if ( Arrays . equals ( zeroBuf , tmpFileAuthBuf ) ) { setFileAuthTag ( null ) ; } else { setFileAuthTag ( tmpFileAuthBuf ) ; } } istream . close ( ) ; } }
reads and verifies all header data from the file
