To expand on @aron-foster's answer, the way that this is done in public key cryptography is to first hash the message and then encrypt the hash using the sender's private key.
In public key cryptography, as you likely know, data encrypted with one of the keys can only be decrypted with the corresponding key. This means that when the hash is encrypted with the private key, the sender's public key can then be used to decrypt it, thus verifying the key.
However, even with well known hashes (let's say, a 'known good' hash set used for file integrity), the issue that you raise is a potential problem. What if an attacker not only modifies the file but discovers a hash collision that allows him to manipulate the protected data while retaining the same hash?
This is the reason that file integrity tools (rather than installation validation tools like RPM and APT) will track multiple hashes for the tracked objects. In this way, even if one hash collision is found, it is beyond belief (today) that you could modify a file and maintain more than one hash unchanged.