checksum_helper.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*-------------------------------------------------------------------------
  2. *
  3. * checksum_helper.h
  4. * Compute a checksum of any of various types using common routines
  5. *
  6. * Portions Copyright (c) 2016-2022, PostgreSQL Global Development Group
  7. *
  8. * IDENTIFICATION
  9. * src/include/common/checksum_helper.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef CHECKSUM_HELPER_H
  14. #define CHECKSUM_HELPER_H
  15. #include "common/cryptohash.h"
  16. #include "common/sha2.h"
  17. #include "port/pg_crc32c.h"
  18. /*
  19. * Supported checksum types. It's not necessarily the case that code using
  20. * these functions needs a cryptographically strong checksum; it may only
  21. * need to detect accidental modification. That's why we include CRC-32C: it's
  22. * much faster than any of the other algorithms. On the other hand, we omit
  23. * MD5 here because any new that does need a cryptographically strong checksum
  24. * should use something better.
  25. */
  26. typedef enum pg_checksum_type
  27. {
  28. CHECKSUM_TYPE_NONE,
  29. CHECKSUM_TYPE_CRC32C,
  30. CHECKSUM_TYPE_SHA224,
  31. CHECKSUM_TYPE_SHA256,
  32. CHECKSUM_TYPE_SHA384,
  33. CHECKSUM_TYPE_SHA512
  34. } pg_checksum_type;
  35. /*
  36. * This is just a union of all applicable context types.
  37. */
  38. typedef union pg_checksum_raw_context
  39. {
  40. pg_crc32c c_crc32c;
  41. pg_cryptohash_ctx *c_sha2;
  42. } pg_checksum_raw_context;
  43. /*
  44. * This structure provides a convenient way to pass the checksum type and the
  45. * checksum context around together.
  46. */
  47. typedef struct pg_checksum_context
  48. {
  49. pg_checksum_type type;
  50. pg_checksum_raw_context raw_context;
  51. } pg_checksum_context;
  52. /*
  53. * This is the longest possible output for any checksum algorithm supported
  54. * by this file.
  55. */
  56. #define PG_CHECKSUM_MAX_LENGTH PG_SHA512_DIGEST_LENGTH
  57. extern bool pg_checksum_parse_type(char *name, pg_checksum_type *);
  58. extern char *pg_checksum_type_name(pg_checksum_type);
  59. extern int pg_checksum_init(pg_checksum_context *, pg_checksum_type);
  60. extern int pg_checksum_update(pg_checksum_context *, const uint8 *input,
  61. size_t len);
  62. extern int pg_checksum_final(pg_checksum_context *, uint8 *output);
  63. #endif