package crypto

import (
	
	
	 // #nosec G505
	

	
)

// RSAEncryptHashed encrypts given data with RSA, prefixing with a hash.
func ( []byte,  *rsa.PublicKey,  io.Reader) ([]byte, error) {
	// Preparing `data_with_hash`.
	// data_with_hash := SHA1(data) + data + (any random bytes);
	// such that the length equals 255 bytes;
	var  [rsaWithHashLen]byte
	if len() > rsaDataLen {
		return nil, errors.Errorf("data length %d is too big", len())
	}

	// Filling data_with_hash with random bytes.
	if ,  := io.ReadFull(, [:]);  != nil {
		return nil, 
	}

	 := sha1.Sum() // #nosec

	// Replacing first 20 bytes with sha1(data).
	copy([:sha1.Size], [:])
	// Replacing other bytes with data itself.
	copy([sha1.Size:], )

	// Encrypting "dataWithHash" with RSA.
	 := rsaEncrypt([:], )

	return , nil
}

// RSADecryptHashed decrypts given data with RSA.
func ( []byte,  *rsa.PrivateKey) ([]byte, error) {
	var  [rsaWithHashLen]byte
	if !rsaDecrypt(, , [:]) {
		return nil, errors.New("invalid data_with_hash")
	}

	 := [:sha1.Size]
	 := [sha1.Size:]

	// Guessing such data that sha1(data) == hash.
	for  := 0;  <= len(); ++ {
		 := [:len()-]
		 := sha1.Sum() // #nosec
		if bytes.Equal([:], ) {
			// Found.
			return , nil
		}
	}

	// This can be caused by invalid keys or implementation bug.
	return nil, errors.New("hash mismatch")
}