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 75 of file cryptohashfuncs.c.

76 {
77  const uint8 *data;
78  const char *typestr = NULL;
79  int digest_len = 0;
80  size_t len;
81  pg_cryptohash_ctx *ctx;
82  bytea *result;
83 
84  switch (type)
85  {
86  case PG_SHA224:
87  typestr = "SHA224";
88  digest_len = PG_SHA224_DIGEST_LENGTH;
89  break;
90  case PG_SHA256:
91  typestr = "SHA256";
92  digest_len = PG_SHA256_DIGEST_LENGTH;
93  break;
94  case PG_SHA384:
95  typestr = "SHA384";
96  digest_len = PG_SHA384_DIGEST_LENGTH;
97  break;
98  case PG_SHA512:
99  typestr = "SHA512";
100  digest_len = PG_SHA512_DIGEST_LENGTH;
101  break;
102  case PG_MD5:
103  case PG_SHA1:
104  elog(ERROR, "unsupported cryptohash type %d", type);
105  break;
106  }
107 
108  result = palloc0(digest_len + VARHDRSZ);
109  len = VARSIZE_ANY_EXHDR(input);
110  data = (unsigned char *) VARDATA_ANY(input);
111 
112  ctx = pg_cryptohash_create(type);
113  if (pg_cryptohash_init(ctx) < 0)
114  elog(ERROR, "could not initialize %s context", typestr);
115  if (pg_cryptohash_update(ctx, data, len) < 0)
116  elog(ERROR, "could not update %s context", typestr);
117  if (pg_cryptohash_final(ctx, (unsigned char *) VARDATA(result),
118  digest_len) < 0)
119  elog(ERROR, "could not finalize %s context", typestr);
120  pg_cryptohash_free(ctx);
121 
122  SET_VARSIZE(result, digest_len + VARHDRSZ);
123 
124  return result;
125 }
#define VARHDRSZ
Definition: c.h:627
unsigned char uint8
Definition: c.h:439
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:130
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:92
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:216
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:66
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:168
@ 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
#define elog(elevel,...)
Definition: elog.h:218
void * palloc0(Size size)
Definition: mcxt.c:1093
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:622

References data, elog, ERROR, len, palloc0(), pg_cryptohash_create(), 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 56 of file cryptohashfuncs.c.

57 {
58  bytea *in = PG_GETARG_BYTEA_PP(0);
59  size_t len;
60  char hexsum[MD5_HASH_LEN + 1];
61 
62  len = VARSIZE_ANY_EXHDR(in);
63  if (pg_md5_hash(VARDATA_ANY(in), len, hexsum) == false)
64  ereport(ERROR,
65  (errcode(ERRCODE_OUT_OF_MEMORY),
66  errmsg("out of memory")));
67 
69 }
#define MD5_HASH_LEN
int errcode(int sqlerrcode)
Definition: elog.c:698
int errmsg(const char *fmt,...)
Definition: elog.c:909
#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)
Definition: md5_common.c:70
text * cstring_to_text(const char *s)
Definition: varlena.c:189

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 
39  /* Calculate the length of the buffer using varlena metadata */
40  len = VARSIZE_ANY_EXHDR(in_text);
41 
42  /* get the hash result */
43  if (pg_md5_hash(VARDATA_ANY(in_text), len, hexsum) == false)
44  ereport(ERROR,
45  (errcode(ERRCODE_OUT_OF_MEMORY),
46  errmsg("out of memory")));
47 
48  /* convert to text and return it */
50 }
#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 132 of file cryptohashfuncs.c.

133 {
135 
136  PG_RETURN_BYTEA_P(result);
137 }
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 140 of file cryptohashfuncs.c.

141 {
143 
144  PG_RETURN_BYTEA_P(result);
145 }

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 148 of file cryptohashfuncs.c.

149 {
151 
152  PG_RETURN_BYTEA_P(result);
153 }

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 156 of file cryptohashfuncs.c.

157 {
159 
160  PG_RETURN_BYTEA_P(result);
161 }

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