My answer assumes you are asking this for a production system, and that you aren't asking this because you are starting to learn to be an anti-virus researcher.
Can you do it?
Yes, as other answers have stated there are many databases which list hashes, or can ID a threat by a hash.
but should that be your only line of defence?
However, there is a major caveat to this approach. Do you think that the file in question has come from an automated, or not very smart/persistent threat, or is this coming from a smart and persistent threat?
If you think the file has come from an automated campaign (like a massive phishing attack) then, yes, this approach might help.
Smart attackers will just keep trying
However, if you at all suspect that an intelligent human being is behind this attack, he will already be modifying his file so that it doesn't show up on a hash database. There are many ways of "poking" at an antivirus to see what it will allow. Smart attackers are experts at tricking AV software.
My advice to you
- Regardless of the files origin, make sure you have a good antivirus running. Especially if your system has to take random files from the internet.
- If your system MUST accept arbitrary files, make sure that you are validating them before accepting them. Check the size, extension, headers, whatever you can.
- And also double check that those files can't be accessed in unintended ways.
- If we are talking about a desktop system, ensure that you are doing everything you can to make sure people aren't going to funky websites or are clicking on suspicious links.
- Make sure everyone has their firewall on (firewalls are harder to trick than AV)
- Use an IDS (also harder to trick than AV)
- If possible, try a host based IDS like http://www.ossec.net/ - this will scan your logs for issues too, and make life very hard on an attacker.
- If you are on Redhat/Cent make sure to use their fine grain permissions system.
Don't use hash checking as a single line of defense. You will be lulled into a false sense of security