PostgreSQL Source Code  git master
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)
 
bool pg_md5_binary (const void *buff, size_t len, void *outbuf)
 
bool pg_md5_encrypt (const char *passwd, const char *salt, size_t salt_len, char *buf)
 

Function Documentation

◆ bytesToHex()

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

Definition at line 28 of file md5_common.c.

Referenced by pg_md5_hash().

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 }

◆ pg_md5_binary()

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

Definition at line 93 of file md5_common.c.

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

Referenced by PerformRadiusTransaction().

94 {
95  pg_cryptohash_ctx *ctx;
96 
98  if (ctx == NULL)
99  return false;
100 
101  if (pg_cryptohash_init(ctx) < 0 ||
102  pg_cryptohash_update(ctx, buff, len) < 0 ||
103  pg_cryptohash_final(ctx, outbuf, MD5_DIGEST_LENGTH) < 0)
104  {
105  pg_cryptohash_free(ctx);
106  return false;
107  }
108 
109  pg_cryptohash_free(ctx);
110  return true;
111 }
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:168
#define MD5_DIGEST_LENGTH
Definition: md5.h:20
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:66
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:92
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:130
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:216

◆ pg_md5_encrypt()

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

Definition at line 124 of file md5_common.c.

References free, malloc, and pg_md5_hash().

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

126 {
127  size_t passwd_len = strlen(passwd);
128 
129  /* +1 here is just to avoid risk of unportable malloc(0) */
130  char *crypt_buf = malloc(passwd_len + salt_len + 1);
131  bool ret;
132 
133  if (!crypt_buf)
134  return false;
135 
136  /*
137  * Place salt at the end because it may be known by users trying to crack
138  * the MD5 output.
139  */
140  memcpy(crypt_buf, passwd, passwd_len);
141  memcpy(crypt_buf + passwd_len, salt, salt_len);
142 
143  strcpy(buf, "md5");
144  ret = pg_md5_hash(crypt_buf, passwd_len + salt_len, buf + 3);
145 
146  free(crypt_buf);
147 
148  return ret;
149 }
#define malloc(a)
Definition: header.h:50
static char * buf
Definition: pg_test_fsync.c:68
bool pg_md5_hash(const void *buff, size_t len, char *hexsum)
Definition: md5_common.c:70
#define free(a)
Definition: header.h:65

◆ pg_md5_hash()

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

Definition at line 70 of file md5_common.c.

References bytesToHex(), MD5_DIGEST_LENGTH, pg_cryptohash_create(), 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().

71 {
73  pg_cryptohash_ctx *ctx;
74 
76  if (ctx == NULL)
77  return false;
78 
79  if (pg_cryptohash_init(ctx) < 0 ||
80  pg_cryptohash_update(ctx, buff, len) < 0 ||
81  pg_cryptohash_final(ctx, sum, sizeof(sum)) < 0)
82  {
83  pg_cryptohash_free(ctx);
84  return false;
85  }
86 
87  bytesToHex(sum, hexsum);
88  pg_cryptohash_free(ctx);
89  return true;
90 }
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:168
unsigned char uint8
Definition: c.h:439
#define MD5_DIGEST_LENGTH
Definition: md5.h:20
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:66
static void bytesToHex(uint8 b[16], char *s)
Definition: md5_common.c:28
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:92
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:130
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:216