PostgreSQL Source Code  git master
cryptohashfuncs.c File Reference
#include "postgres.h"
#include "common/cryptohash.h"
#include "common/md5.h"
#include "common/sha2.h"
#include "utils/builtins.h"
Include dependency graph for cryptohashfuncs.c:

Go to the source code of this file.

Macros

#define MD5_HASH_LEN   32
 

Functions

Datum md5_text (PG_FUNCTION_ARGS)
 
Datum md5_bytea (PG_FUNCTION_ARGS)
 
static byteacryptohash_internal (pg_cryptohash_type type, bytea *input)
 
Datum sha224_bytea (PG_FUNCTION_ARGS)
 
Datum sha256_bytea (PG_FUNCTION_ARGS)
 
Datum sha384_bytea (PG_FUNCTION_ARGS)
 
Datum sha512_bytea (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ MD5_HASH_LEN

#define MD5_HASH_LEN   32

Definition at line 27 of file cryptohashfuncs.c.

Function Documentation

◆ cryptohash_internal()

static bytea* cryptohash_internal ( pg_cryptohash_type  type,
bytea input 
)
inlinestatic

Definition at line 79 of file cryptohashfuncs.c.

80 {
81  const uint8 *data;
82  const char *typestr = NULL;
83  int digest_len = 0;
84  size_t len;
85  pg_cryptohash_ctx *ctx;
86  bytea *result;
87 
88  switch (type)
89  {
90  case PG_SHA224:
91  typestr = "SHA224";
92  digest_len = PG_SHA224_DIGEST_LENGTH;
93  break;
94  case PG_SHA256:
95  typestr = "SHA256";
96  digest_len = PG_SHA256_DIGEST_LENGTH;
97  break;
98  case PG_SHA384:
99  typestr = "SHA384";
100  digest_len = PG_SHA384_DIGEST_LENGTH;
101  break;
102  case PG_SHA512:
103  typestr = "SHA512";
104  digest_len = PG_SHA512_DIGEST_LENGTH;
105  break;
106  case PG_MD5:
107  case PG_SHA1:
108  elog(ERROR, "unsupported cryptohash type %d", type);
109  break;
110  }
111 
112  result = palloc0(digest_len + VARHDRSZ);
113  len = VARSIZE_ANY_EXHDR(input);
114  data = (unsigned char *) VARDATA_ANY(input);
115 
116  ctx = pg_cryptohash_create(type);
117  if (pg_cryptohash_init(ctx) < 0)
118  elog(ERROR, "could not initialize %s context: %s", typestr,
119  pg_cryptohash_error(ctx));
120  if (pg_cryptohash_update(ctx, data, len) < 0)
121  elog(ERROR, "could not update %s context: %s", typestr,
122  pg_cryptohash_error(ctx));
123  if (pg_cryptohash_final(ctx, (unsigned char *) VARDATA(result),
124  digest_len) < 0)
125  elog(ERROR, "could not finalize %s context: %s", typestr,
126  pg_cryptohash_error(ctx));
127  pg_cryptohash_free(ctx);
128 
129  SET_VARSIZE(result, digest_len + VARHDRSZ);
130 
131  return result;
132 }
#define VARHDRSZ
Definition: c.h:638
unsigned char uint8
Definition: c.h:450
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:139
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:101
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:243
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:74
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:177
const char * pg_cryptohash_error(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:259
@ PG_SHA512
Definition: cryptohash.h:26
@ PG_SHA224
Definition: cryptohash.h:23
@ PG_SHA384
Definition: cryptohash.h:25
@ PG_SHA1
Definition: cryptohash.h:22
@ PG_SHA256
Definition: cryptohash.h:24
@ PG_MD5
Definition: cryptohash.h:21
#define ERROR
Definition: elog.h:33
void * palloc0(Size size)
Definition: mcxt.c:1099
const void size_t len
const void * data
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:23
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:26
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:29
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:20
Definition: c.h:633

References data, elog(), ERROR, len, palloc0(), pg_cryptohash_create(), pg_cryptohash_error(), pg_cryptohash_final(), pg_cryptohash_free(), pg_cryptohash_init(), pg_cryptohash_update(), PG_MD5, PG_SHA1, PG_SHA224, PG_SHA224_DIGEST_LENGTH, PG_SHA256, PG_SHA256_DIGEST_LENGTH, PG_SHA384, PG_SHA384_DIGEST_LENGTH, PG_SHA512, PG_SHA512_DIGEST_LENGTH, SET_VARSIZE, generate_unaccent_rules::type, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

Referenced by sha224_bytea(), sha256_bytea(), sha384_bytea(), and sha512_bytea().

◆ md5_bytea()

Datum md5_bytea ( PG_FUNCTION_ARGS  )

Definition at line 58 of file cryptohashfuncs.c.

59 {
60  bytea *in = PG_GETARG_BYTEA_PP(0);
61  size_t len;
62  char hexsum[MD5_HASH_LEN + 1];
63  const char *errstr = NULL;
64 
65  len = VARSIZE_ANY_EXHDR(in);
66  if (pg_md5_hash(VARDATA_ANY(in), len, hexsum, &errstr) == false)
67  ereport(ERROR,
68  (errcode(ERRCODE_INTERNAL_ERROR),
69  errmsg("could not compute %s hash: %s", "MD5",
70  errstr)));
71 
73 }
#define MD5_HASH_LEN
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ereport(elevel,...)
Definition: elog.h:143
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:308
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
bool pg_md5_hash(const void *buff, size_t len, char *hexsum, const char **errstr)
Definition: md5_common.c:73
text * cstring_to_text(const char *s)
Definition: varlena.c:188

References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, len, MD5_HASH_LEN, PG_GETARG_BYTEA_PP, pg_md5_hash(), PG_RETURN_TEXT_P, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ md5_text()

Datum md5_text ( PG_FUNCTION_ARGS  )

Definition at line 33 of file cryptohashfuncs.c.

34 {
35  text *in_text = PG_GETARG_TEXT_PP(0);
36  size_t len;
37  char hexsum[MD5_HASH_LEN + 1];
38  const char *errstr = NULL;
39 
40  /* Calculate the length of the buffer using varlena metadata */
41  len = VARSIZE_ANY_EXHDR(in_text);
42 
43  /* get the hash result */
44  if (pg_md5_hash(VARDATA_ANY(in_text), len, hexsum, &errstr) == false)
45  ereport(ERROR,
46  (errcode(ERRCODE_INTERNAL_ERROR),
47  errmsg("could not compute %s hash: %s", "MD5",
48  errstr)));
49 
50  /* convert to text and return it */
52 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309

References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, len, MD5_HASH_LEN, PG_GETARG_TEXT_PP, pg_md5_hash(), PG_RETURN_TEXT_P, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ sha224_bytea()

Datum sha224_bytea ( PG_FUNCTION_ARGS  )

Definition at line 139 of file cryptohashfuncs.c.

140 {
142 
143  PG_RETURN_BYTEA_P(result);
144 }
static bytea * cryptohash_internal(pg_cryptohash_type type, bytea *input)
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371

References cryptohash_internal(), PG_GETARG_BYTEA_PP, PG_RETURN_BYTEA_P, and PG_SHA224.

◆ sha256_bytea()

Datum sha256_bytea ( PG_FUNCTION_ARGS  )

Definition at line 147 of file cryptohashfuncs.c.

148 {
150 
151  PG_RETURN_BYTEA_P(result);
152 }

References cryptohash_internal(), PG_GETARG_BYTEA_PP, PG_RETURN_BYTEA_P, and PG_SHA256.

◆ sha384_bytea()

Datum sha384_bytea ( PG_FUNCTION_ARGS  )

Definition at line 155 of file cryptohashfuncs.c.

156 {
158 
159  PG_RETURN_BYTEA_P(result);
160 }

References cryptohash_internal(), PG_GETARG_BYTEA_PP, PG_RETURN_BYTEA_P, and PG_SHA384.

◆ sha512_bytea()

Datum sha512_bytea ( PG_FUNCTION_ARGS  )

Definition at line 163 of file cryptohashfuncs.c.

164 {
166 
167  PG_RETURN_BYTEA_P(result);
168 }

References cryptohash_internal(), PG_GETARG_BYTEA_PP, PG_RETURN_BYTEA_P, and PG_SHA512.