What are similarities and differences between a "checksum" algorithm and a "hash" function?
Can they be used instead of each other? Or their usage are different?
For example, for verifying the integrity of a text, which one is better to be used?
Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. It only takes a minute to sign up.Sign up to join this community
A checksum is an application of a hash function.
A cryptographic hash... is a mathematical algorithm that maps data of arbitrary size to a bit string of a fixed size (a hash) and is designed to be a one-way function, that is, a function which is infeasible to invert.
A checksum is a small-sized datum derived from a block of digital data for the purpose of detecting errors which may have been introduced during its transmission or storage.
As an example of how hash functions and checksums are applied in bitcoin, consider how a bitcoin address is generated from a public key (for full details see this link):
By employing a hash function, the user's public key remains unknown until an address is spent from, so this keeps a user's funds safe in the case ECDSA is broken (see this question for further reasoning).
Adding a checksum to the address ensures that a user will not lose coins due to a simple keying/copying error when submitting payments.
So, we can see that using a hash function allows the user to hide their public key (but easily confirm the address once we know the public key), while also retaining enough bytes in the address to ensure a very large address space.
In contrast, the checksum is short (4 bytes), so obviously we could not use a checksum as the address (the address space would be far too tiny!). But a checksum works well to ensure the address is legitimate, the chance of making a keying error and still having a valid checksum is incredibly small- it is basically impossible in practise! Using the entire hash output as the checksum would work as well, but it would be overkill, and an inefficient use of space (instead of an extra 4 bytes, the address would basically double in size).
Sometimes these terms are used more or less interchangeably. Traditionally, a checksum is used to detect corruption and a hash is used to map large elements into slots randomly. In Bitcoin-land we're often concerned with "cryptographic hashes" which are hashes that have special security properties.
Things called checksums are usually designed to have certain uniformity properties that make them maximally likely to detect errors. Things called hashes are usually designed to be very fast to compute and not be too non-uniform. Things called cryptographic hashes are designed to make it difficult to compute a collision.
The security properties of cryptographic hashes often make them slower and less uniform than functions you would use for cases where the security isn't required.
Generally any function from any of the three groups could be used in any of the applications, the result would just not achieve the security, performance, or reliability that you'd get for using the right tool for the job.