package tdesktop

import (
	

	

	
)

type keyData struct {
	localKey    crypto.Key
	accountsIDx []uint32
}

// See https://github.com/telegramdesktop/tdesktop/blob/v2.9.8/Telegram/SourceFiles/storage/storage_domain.cpp#L119-L159.
func ( *tdesktopFile,  []byte) ( keyData,  error) {
	,  := .readArray()
	if  != nil {
		return keyData{}, errors.Wrap(, "read salt")
	}
	if  := len();  != localEncryptSaltSize {
		return keyData{}, errors.Errorf("invalid salt length %d", )
	}

	 := createLocalKey(, )
	,  := .readArray()
	if  != nil {
		return keyData{}, errors.Wrap(, "read keyEncrypted")
	}
	,  := decryptLocal(, )
	if  != nil {
		return keyData{}, errors.Wrap(, "decrypt keyEncrypted")
	}
	, ,  := readArray(, binary.LittleEndian)
	if  != nil {
		return keyData{}, errors.Wrap(, "read key")
	}

	if  := len();  < len(crypto.Key{}) {
		return keyData{}, errors.Errorf("key too small (%d)", )
	}
	var  crypto.Key
	copy([:], )

	,  := .readArray()
	if  != nil {
		return keyData{}, errors.Wrap(, "read infoEncrypted")
	}
	,  := decryptLocal(, )
	if  != nil {
		return keyData{}, ErrKeyInfoDecrypt
	}
	// Skip decrypted data length.
	 = [4:]
	// Read count of accounts.
	 := int(binary.BigEndian.Uint32())
	 = [4:]

	// Preallocate accountsIDx.
	 := make([]uint32, 0, )
	for  := 0;  < ; ++ {
		 := binary.BigEndian.Uint32([*4:])
		 = append(, )
	}

	return keyData{
		localKey:    ,
		accountsIDx: ,
	}, nil
}