package crypto

import (
	
	 // #nosec
	

	

	

	
)

// DecryptExchangeAnswer decrypts messages created during key exchange.
func (, ,  []byte) ( []byte,  error) {
	// Decrypting inner data.
	,  := aes.NewCipher()
	if  != nil {
		return nil, errors.Wrap(, "create aes cipher")
	}

	 := make([]byte, len())
	// Checking length. Invalid length will lead to panic in CryptBlocks.
	if len()%.BlockSize() != 0 {
		return nil, errors.Errorf("invalid len of data_with_hash (%d %% 16 != 0)", len())
	}
	ige.DecryptBlocks(, , , )

	 = GuessDataWithHash()
	if  == nil {
		// Most common cause of this error is invalid crypto implementation,
		// i.e. invalid keys are used to decrypt payload which lead to
		// decrypt failure, so data does not match sha1 with any padding.
		return nil, errors.New("guess data from data_with_hash")
	}

	return
}

// EncryptExchangeAnswer encrypts messages created during key exchange.
func ( io.Reader, , ,  []byte) ( []byte,  error) {
	,  := aes.NewCipher()
	if  != nil {
		return nil, errors.Wrap(, "create aes cipher")
	}

	,  := DataWithHash(, )
	if  != nil {
		return nil, errors.Wrap(, "get answer with hash")
	}

	 = make([]byte, len())
	ige.EncryptBlocks(, , , )
	return
}

// NonceHash1 computes nonce_hash_1.
// See https://core.telegram.org/mtproto/auth_key#dh-key-exchange-complete.
func ( bin.Int256,  Key) ( bin.Int128) {
	var  []byte
	 = append(, [:]...)
	 = append(, 1)
	 = append(, sha([:])[0:8]...)
	 = sha()[4:20]
	copy([:], )
	return
}

func ( []byte) []byte {
	 := sha1.Sum() // #nosec
	return [:]
}