PostgreSQL Source Code  git master
cryptohashes.c File Reference
#include "postgres.h"
#include "common/md5.h"
#include "common/sha2.h"
#include "utils/builtins.h"
Include dependency graph for cryptohashes.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)
 
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 26 of file cryptohashes.c.

Referenced by md5_bytea(), and md5_text().

Function Documentation

◆ md5_bytea()

Datum md5_bytea ( PG_FUNCTION_ARGS  )

Definition at line 55 of file cryptohashes.c.

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

56 {
57  bytea *in = PG_GETARG_BYTEA_PP(0);
58  size_t len;
59  char hexsum[MD5_HASH_LEN + 1];
60 
61  len = VARSIZE_ANY_EXHDR(in);
62  if (pg_md5_hash(VARDATA_ANY(in), len, hexsum) == false)
63  ereport(ERROR,
64  (errcode(ERRCODE_OUT_OF_MEMORY),
65  errmsg("out of memory")));
66 
68 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
#define MD5_HASH_LEN
Definition: cryptohashes.c:26
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
bool pg_md5_hash(const void *buff, size_t len, char *hexsum)
Definition: md5.c:293
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:302
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549

◆ md5_text()

Datum md5_text ( PG_FUNCTION_ARGS  )

Definition at line 32 of file cryptohashes.c.

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

33 {
34  text *in_text = PG_GETARG_TEXT_PP(0);
35  size_t len;
36  char hexsum[MD5_HASH_LEN + 1];
37 
38  /* Calculate the length of the buffer using varlena metadata */
39  len = VARSIZE_ANY_EXHDR(in_text);
40 
41  /* get the hash result */
42  if (pg_md5_hash(VARDATA_ANY(in_text), len, hexsum) == false)
43  ereport(ERROR,
44  (errcode(ERRCODE_OUT_OF_MEMORY),
45  errmsg("out of memory")));
46 
47  /* convert to text and return it */
49 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define ERROR
Definition: elog.h:43
#define MD5_HASH_LEN
Definition: cryptohashes.c:26
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
bool pg_md5_hash(const void *buff, size_t len, char *hexsum)
Definition: md5.c:293
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549

◆ sha224_bytea()

Datum sha224_bytea ( PG_FUNCTION_ARGS  )

Definition at line 76 of file cryptohashes.c.

References buf, palloc(), PG_GETARG_BYTEA_PP, PG_RETURN_BYTEA_P, PG_SHA224_DIGEST_LENGTH, pg_sha224_final(), pg_sha224_init(), pg_sha224_update(), SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

77 {
78  bytea *in = PG_GETARG_BYTEA_PP(0);
79  const uint8 *data;
80  size_t len;
81  pg_sha224_ctx ctx;
82  unsigned char buf[PG_SHA224_DIGEST_LENGTH];
83  bytea *result;
84 
85  len = VARSIZE_ANY_EXHDR(in);
86  data = (unsigned char *) VARDATA_ANY(in);
87 
88  pg_sha224_init(&ctx);
89  pg_sha224_update(&ctx, data, len);
90  pg_sha224_final(&ctx, buf);
91 
92  result = palloc(sizeof(buf) + VARHDRSZ);
93  SET_VARSIZE(result, sizeof(buf) + VARHDRSZ);
94  memcpy(VARDATA(result), buf, sizeof(buf));
95 
96  PG_RETURN_BYTEA_P(result);
97 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:555
unsigned char uint8
Definition: c.h:356
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
static char * buf
Definition: pg_test_fsync.c:68
void pg_sha224_final(pg_sha224_ctx *context, uint8 *digest)
Definition: sha2.c:983
void pg_sha224_init(pg_sha224_ctx *context)
Definition: sha2.c:967
void pg_sha224_update(pg_sha224_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:977
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:302
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define PG_SHA224_DIGEST_LENGTH
Definition: sha2.h:59

◆ sha256_bytea()

Datum sha256_bytea ( PG_FUNCTION_ARGS  )

Definition at line 100 of file cryptohashes.c.

References buf, palloc(), PG_GETARG_BYTEA_PP, PG_RETURN_BYTEA_P, PG_SHA256_DIGEST_LENGTH, pg_sha256_final(), pg_sha256_init(), pg_sha256_update(), SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

101 {
102  bytea *in = PG_GETARG_BYTEA_PP(0);
103  const uint8 *data;
104  size_t len;
105  pg_sha256_ctx ctx;
106  unsigned char buf[PG_SHA256_DIGEST_LENGTH];
107  bytea *result;
108 
109  len = VARSIZE_ANY_EXHDR(in);
110  data = (unsigned char *) VARDATA_ANY(in);
111 
112  pg_sha256_init(&ctx);
113  pg_sha256_update(&ctx, data, len);
114  pg_sha256_final(&ctx, buf);
115 
116  result = palloc(sizeof(buf) + VARHDRSZ);
117  SET_VARSIZE(result, sizeof(buf) + VARHDRSZ);
118  memcpy(VARDATA(result), buf, sizeof(buf));
119 
120  PG_RETURN_BYTEA_P(result);
121 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:555
#define PG_SHA256_DIGEST_LENGTH
Definition: sha2.h:62
unsigned char uint8
Definition: c.h:356
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
void pg_sha256_init(pg_sha256_ctx *context)
Definition: sha2.c:268
static char * buf
Definition: pg_test_fsync.c:68
void pg_sha256_update(pg_sha256_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:465
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:302
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
void pg_sha256_final(pg_sha256_ctx *context, uint8 *digest)
Definition: sha2.c:566
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ sha384_bytea()

Datum sha384_bytea ( PG_FUNCTION_ARGS  )

Definition at line 124 of file cryptohashes.c.

References buf, palloc(), PG_GETARG_BYTEA_PP, PG_RETURN_BYTEA_P, PG_SHA384_DIGEST_LENGTH, pg_sha384_final(), pg_sha384_init(), pg_sha384_update(), SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

125 {
126  bytea *in = PG_GETARG_BYTEA_PP(0);
127  const uint8 *data;
128  size_t len;
129  pg_sha384_ctx ctx;
130  unsigned char buf[PG_SHA384_DIGEST_LENGTH];
131  bytea *result;
132 
133  len = VARSIZE_ANY_EXHDR(in);
134  data = (unsigned char *) VARDATA_ANY(in);
135 
136  pg_sha384_init(&ctx);
137  pg_sha384_update(&ctx, data, len);
138  pg_sha384_final(&ctx, buf);
139 
140  result = palloc(sizeof(buf) + VARHDRSZ);
141  SET_VARSIZE(result, sizeof(buf) + VARHDRSZ);
142  memcpy(VARDATA(result), buf, sizeof(buf));
143 
144  PG_RETURN_BYTEA_P(result);
145 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:555
unsigned char uint8
Definition: c.h:356
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
void pg_sha384_update(pg_sha384_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:933
static char * buf
Definition: pg_test_fsync.c:68
void pg_sha384_final(pg_sha384_ctx *context, uint8 *digest)
Definition: sha2.c:939
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:302
void pg_sha384_init(pg_sha384_ctx *context)
Definition: sha2.c:923
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define PG_SHA384_DIGEST_LENGTH
Definition: sha2.h:65

◆ sha512_bytea()

Datum sha512_bytea ( PG_FUNCTION_ARGS  )

Definition at line 148 of file cryptohashes.c.

References buf, palloc(), PG_GETARG_BYTEA_PP, PG_RETURN_BYTEA_P, PG_SHA512_DIGEST_LENGTH, pg_sha512_final(), pg_sha512_init(), pg_sha512_update(), SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

149 {
150  bytea *in = PG_GETARG_BYTEA_PP(0);
151  const uint8 *data;
152  size_t len;
153  pg_sha512_ctx ctx;
154  unsigned char buf[PG_SHA512_DIGEST_LENGTH];
155  bytea *result;
156 
157  len = VARSIZE_ANY_EXHDR(in);
158  data = (unsigned char *) VARDATA_ANY(in);
159 
160  pg_sha512_init(&ctx);
161  pg_sha512_update(&ctx, data, len);
162  pg_sha512_final(&ctx, buf);
163 
164  result = palloc(sizeof(buf) + VARHDRSZ);
165  SET_VARSIZE(result, sizeof(buf) + VARHDRSZ);
166  memcpy(VARDATA(result), buf, sizeof(buf));
167 
168  PG_RETURN_BYTEA_P(result);
169 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:555
unsigned char uint8
Definition: c.h:356
#define PG_SHA512_DIGEST_LENGTH
Definition: sha2.h:68
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
void pg_sha512_final(pg_sha512_ctx *context, uint8 *digest)
Definition: sha2.c:894
static char * buf
Definition: pg_test_fsync.c:68
void pg_sha512_update(pg_sha512_ctx *context, const uint8 *data, size_t len)
Definition: sha2.c:791
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:302
void pg_sha512_init(pg_sha512_ctx *context)
Definition: sha2.c:594
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329