1/*-------------------------------------------------------------------------
4 * Cryptographic hash functions
6 * Portions Copyright (c) 2018-2025, PostgreSQL Global Development Group
10 * src/backend/utils/adt/cryptohashfuncs.c
12 *-------------------------------------------------------------------------
27/* MD5 produces a 16 byte (128 bit) hash; double it for hex */
28 #define MD5_HASH_LEN 32
31 * Create an MD5 hash of a text value and return it as hex string.
39 const char *errstr = NULL;
41 /* Calculate the length of the buffer using varlena metadata */
44 /* get the hash result */
47 (
errcode(ERRCODE_INTERNAL_ERROR),
48 errmsg(
"could not compute %s hash: %s",
"MD5",
51 /* convert to text and return it */
56 * Create an MD5 hash of a bytea value and return it as a hex string.
64 const char *errstr = NULL;
69 (
errcode(ERRCODE_INTERNAL_ERROR),
70 errmsg(
"could not compute %s hash: %s",
"MD5",
77 * Internal routine to compute a cryptohash with the given bytea input.
83 const char *typestr = NULL;
119 elog(
ERROR,
"could not initialize %s context: %s", typestr,
122 elog(
ERROR,
"could not update %s context: %s", typestr,
126 elog(
ERROR,
"could not finalize %s context: %s", typestr,
const char * pg_cryptohash_error(pg_cryptohash_ctx *ctx)
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Datum md5_bytea(PG_FUNCTION_ARGS)
Datum sha384_bytea(PG_FUNCTION_ARGS)
Datum sha256_bytea(PG_FUNCTION_ARGS)
Datum sha512_bytea(PG_FUNCTION_ARGS)
Datum sha224_bytea(PG_FUNCTION_ARGS)
Datum md5_text(PG_FUNCTION_ARGS)
static bytea * cryptohash_internal(pg_cryptohash_type type, bytea *input)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define PG_GETARG_BYTEA_PP(n)
#define PG_GETARG_TEXT_PP(n)
#define PG_RETURN_BYTEA_P(x)
#define PG_RETURN_TEXT_P(x)
void * palloc0(Size size)
bool pg_md5_hash(const void *buff, size_t len, char *hexsum, const char **errstr)
#define PG_SHA256_DIGEST_LENGTH
#define PG_SHA384_DIGEST_LENGTH
#define PG_SHA512_DIGEST_LENGTH
#define PG_SHA224_DIGEST_LENGTH
static Size VARSIZE_ANY_EXHDR(const void *PTR)
static char * VARDATA(const void *PTR)
static char * VARDATA_ANY(const void *PTR)
static void SET_VARSIZE(void *PTR, Size len)
text * cstring_to_text(const char *s)