diff options
| author | Robert Haas | 2020-04-03 15:50:38 +0000 |
|---|---|---|
| committer | Robert Haas | 2020-04-03 15:52:43 +0000 |
| commit | c12e43a2e0d45a6b59f2cea53f1b82e52fdcff7a (patch) | |
| tree | cc142267874bb46682561a0ac2ecb821feff5863 /src/include/common | |
| parent | 6dd9f357792545b626e28b2e1f73cb4c32c437f1 (diff) | |
Add checksum helper functions.
These functions make it easier to write code that wants to compute a
checksum for some data while allowing the user to configure the type
of checksum that gets used.
This is another piece of infrastructure for the upcoming patch to add
backup manifests.
Patch written from scratch by me, but it is similar to previous work
by Rushabh Lathia and Suraj Kharage. Suraj also reviewed this version
off-list. Advice on how not to break Windows from Davinder Singh.
Discussion: http://postgr.es/m/CA+TgmoZV8dw1H2bzZ9xkKwdrk8+XYa+DC9H=F7heO2zna5T6qg@mail.gmail.com
Discussion: http://postgr.es/m/CA+TgmoZRTBiPyvQEwV79PU1ePTtSEo2UeVncrkJMbn1sU1gnRA@mail.gmail.com
Diffstat (limited to 'src/include/common')
| -rw-r--r-- | src/include/common/checksum_helper.h | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/include/common/checksum_helper.h b/src/include/common/checksum_helper.h new file mode 100644 index 00000000000..48b0745dadd --- /dev/null +++ b/src/include/common/checksum_helper.h @@ -0,0 +1,74 @@ +/*------------------------------------------------------------------------- + * + * checksum_helper.h + * Compute a checksum of any of various types using common routines + * + * Portions Copyright (c) 2016-2020, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/include/common/checksum_helper.h + * + *------------------------------------------------------------------------- + */ + +#ifndef CHECKSUM_HELPER_H +#define CHECKSUM_HELPER_H + +#include "common/sha2.h" +#include "port/pg_crc32c.h" + +/* + * Supported checksum types. It's not necessarily the case that code using + * these functions needs a cryptographically strong checksum; it may only + * need to detect accidental modification. That's why we include CRC-32C: it's + * much faster than any of the other algorithms. On the other hand, we omit + * MD5 here because any new that does need a cryptographically strong checksum + * should use something better. + */ +typedef enum pg_checksum_type +{ + CHECKSUM_TYPE_NONE, + CHECKSUM_TYPE_CRC32C, + CHECKSUM_TYPE_SHA224, + CHECKSUM_TYPE_SHA256, + CHECKSUM_TYPE_SHA384, + CHECKSUM_TYPE_SHA512 +} pg_checksum_type; + +/* + * This is just a union of all applicable context types. + */ +typedef union pg_checksum_raw_context +{ + pg_crc32c c_crc32c; + pg_sha224_ctx c_sha224; + pg_sha256_ctx c_sha256; + pg_sha384_ctx c_sha384; + pg_sha512_ctx c_sha512; +} pg_checksum_raw_context; + +/* + * This structure provides a convenient way to pass the checksum type and the + * checksum context around together. + */ +typedef struct pg_checksum_context +{ + pg_checksum_type type; + pg_checksum_raw_context raw_context; +} pg_checksum_context; + +/* + * This is the longest possible output for any checksum algorithm supported + * by this file. + */ +#define PG_CHECKSUM_MAX_LENGTH PG_SHA512_DIGEST_LENGTH + +extern bool pg_checksum_parse_type(char *name, pg_checksum_type *); +extern char *pg_checksum_type_name(pg_checksum_type); + +extern void pg_checksum_init(pg_checksum_context *, pg_checksum_type); +extern void pg_checksum_update(pg_checksum_context *, const uint8 *input, + size_t len); +extern int pg_checksum_final(pg_checksum_context *, uint8 *output); + +#endif |
