ddnet/src/base/hash.h
heinrich5991 ca8fcc823c Use more secure hash function for map downloads
SHA256 was chosen because it is reasonably standard, the file names
don't explode in length (this rules out SHA512) and it is supported by
basically all versions of OpenSSL (this rules out SHA512/256 and SHA3).

The protocol is changed in a backward compatible way: The supporting
server sends the SHA256 corresponding to the map in the `MAP_DETAILS`
message prior to sending the `MAP_CHANGE` message. The client saves the
SHA256 obtained from the `MAP_DETAILS` message until the next
`MAP_CHANGE` message.

For servers not supporting this protocol, the client falls back to
simply opening maps like in the previous scheme.

Remove the `map_version` tool, it is not being used and would have been
a little bit effort to update.

Use the OpenSSL implementation of SHA256 if it is supported, otherwise
fall back to a public domain one.

Fix #1127.
2018-06-24 17:04:50 +02:00

44 lines
874 B
C

#ifndef BASE_HASH_H
#define BASE_HASH_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
enum
{
SHA256_DIGEST_LENGTH=256/8,
SHA256_MAXSTRSIZE=2*SHA256_DIGEST_LENGTH+1,
};
typedef struct
{
unsigned char data[SHA256_DIGEST_LENGTH];
} SHA256_DIGEST;
SHA256_DIGEST sha256(const void *message, size_t message_len);
void sha256_str(SHA256_DIGEST digest, char *str, size_t max_len);
int sha256_from_str(SHA256_DIGEST *out, const char *str);
int sha256_comp(SHA256_DIGEST digest1, SHA256_DIGEST digest2);
static const SHA256_DIGEST SHA256_ZEROED = {{0}};
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
inline bool operator==(const SHA256_DIGEST &that, const SHA256_DIGEST &other)
{
return sha256_comp(that, other) == 0;
}
inline bool operator!=(const SHA256_DIGEST &that, const SHA256_DIGEST &other)
{
return !(that == other);
}
#endif
#endif // BASE_HASH_H