PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
md5_common.c File Reference
#include "postgres.h"
#include "common/cryptohash.h"
#include "common/md5.h"
Include dependency graph for md5_common.c:

Go to the source code of this file.

Functions

static void bytesToHex (uint8 b[16], char *s)
 
bool pg_md5_hash (const void *buff, size_t len, char *hexsum, const char **errstr)
 
bool pg_md5_binary (const void *buff, size_t len, void *outbuf, const char **errstr)
 
bool pg_md5_encrypt (const char *passwd, const char *salt, size_t salt_len, char *buf, const char **errstr)
 

Function Documentation

◆ bytesToHex()

static void bytesToHex ( uint8  b[16],
char *  s 
)
static

Definition at line 28 of file md5_common.c.

29 {
30  static const char *hex = "0123456789abcdef";
31  int q,
32  w;
33 
34  for (q = 0, w = 0; q < 16; q++)
35  {
36  s[w++] = hex[(b[q] >> 4) & 0x0F];
37  s[w++] = hex[b[q] & 0x0F];
38  }
39  s[w] = '\0';
40 }
int b
Definition: isn.c:69

References b.

Referenced by pg_md5_hash().

◆ pg_md5_binary()

bool pg_md5_binary ( const void *  buff,
size_t  len,
void *  outbuf,
const char **  errstr 
)

Definition at line 108 of file md5_common.c.

109 {
110  pg_cryptohash_ctx *ctx;
111 
112  *errstr = NULL;
114  if (ctx == NULL)
115  {
116  *errstr = pg_cryptohash_error(NULL); /* returns OOM */
117  return false;
118  }
119 
120  if (pg_cryptohash_init(ctx) < 0 ||
121  pg_cryptohash_update(ctx, buff, len) < 0 ||
122  pg_cryptohash_final(ctx, outbuf, MD5_DIGEST_LENGTH) < 0)
123  {
124  *errstr = pg_cryptohash_error(ctx);
125  pg_cryptohash_free(ctx);
126  return false;
127  }
128 
129  pg_cryptohash_free(ctx);
130  return true;
131 }
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:136
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:100
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:238
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:172
const char * pg_cryptohash_error(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:254
@ PG_MD5
Definition: cryptohash.h:21
#define MD5_DIGEST_LENGTH
Definition: md5.h:20
const void size_t len

References len, MD5_DIGEST_LENGTH, pg_cryptohash_create(), pg_cryptohash_error(), pg_cryptohash_final(), pg_cryptohash_free(), pg_cryptohash_init(), pg_cryptohash_update(), and PG_MD5.

Referenced by PerformRadiusTransaction().

◆ pg_md5_encrypt()

bool pg_md5_encrypt ( const char *  passwd,
const char *  salt,
size_t  salt_len,
char *  buf,
const char **  errstr 
)

Definition at line 145 of file md5_common.c.

147 {
148  size_t passwd_len = strlen(passwd);
149 
150  /* +1 here is just to avoid risk of unportable malloc(0) */
151  char *crypt_buf = malloc(passwd_len + salt_len + 1);
152  bool ret;
153 
154  if (!crypt_buf)
155  {
156  *errstr = _("out of memory");
157  return false;
158  }
159 
160  /*
161  * Place salt at the end because it may be known by users trying to crack
162  * the MD5 output.
163  */
164  memcpy(crypt_buf, passwd, passwd_len);
165  memcpy(crypt_buf + passwd_len, salt, salt_len);
166 
167  strcpy(buf, "md5");
168  ret = pg_md5_hash(crypt_buf, passwd_len + salt_len, buf + 3, errstr);
169 
170  free(crypt_buf);
171 
172  return ret;
173 }
#define _(x)
Definition: elog.c:90
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
bool pg_md5_hash(const void *buff, size_t len, char *hexsum, const char **errstr)
Definition: md5_common.c:74
static char * buf
Definition: pg_test_fsync.c:72

References _, buf, free, malloc, and pg_md5_hash().

Referenced by encrypt_password(), md5_crypt_verify(), pg_password_sendauth(), plain_crypt_verify(), PQencryptPassword(), and PQencryptPasswordConn().

◆ pg_md5_hash()

bool pg_md5_hash ( const void *  buff,
size_t  len,
char *  hexsum,
const char **  errstr 
)

Definition at line 74 of file md5_common.c.

75 {
77  pg_cryptohash_ctx *ctx;
78 
79  *errstr = NULL;
81  if (ctx == NULL)
82  {
83  *errstr = pg_cryptohash_error(NULL); /* returns OOM */
84  return false;
85  }
86 
87  if (pg_cryptohash_init(ctx) < 0 ||
88  pg_cryptohash_update(ctx, buff, len) < 0 ||
89  pg_cryptohash_final(ctx, sum, sizeof(sum)) < 0)
90  {
91  *errstr = pg_cryptohash_error(ctx);
92  pg_cryptohash_free(ctx);
93  return false;
94  }
95 
96  bytesToHex(sum, hexsum);
97  pg_cryptohash_free(ctx);
98  return true;
99 }
unsigned char uint8
Definition: c.h:490
static void bytesToHex(uint8 b[16], char *s)
Definition: md5_common.c:28

References bytesToHex(), len, MD5_DIGEST_LENGTH, pg_cryptohash_create(), pg_cryptohash_error(), pg_cryptohash_final(), pg_cryptohash_free(), pg_cryptohash_init(), pg_cryptohash_update(), and PG_MD5.

Referenced by md5_bytea(), md5_text(), and pg_md5_encrypt().