diff options
Diffstat (limited to 'src/common/md5_common.c')
-rw-r--r-- | src/common/md5_common.c | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/common/md5_common.c b/src/common/md5_common.c index 65b0be4b661..edc935eeb8d 100644 --- a/src/common/md5_common.c +++ b/src/common/md5_common.c @@ -67,7 +67,7 @@ bytesToHex(uint8 b[16], char *s) */ bool -pg_md5_hash(const void *buff, size_t len, char *hexsum) +pg_md5_hash(const void *buff, size_t len, char *hexsum, const char **errstr) { uint8 sum[MD5_DIGEST_LENGTH]; pg_cryptohash_ctx *ctx; @@ -80,6 +80,7 @@ pg_md5_hash(const void *buff, size_t len, char *hexsum) pg_cryptohash_update(ctx, buff, len) < 0 || pg_cryptohash_final(ctx, sum, sizeof(sum)) < 0) { + *errstr = pg_cryptohash_error(ctx); pg_cryptohash_free(ctx); return false; } @@ -90,18 +91,23 @@ pg_md5_hash(const void *buff, size_t len, char *hexsum) } bool -pg_md5_binary(const void *buff, size_t len, void *outbuf) +pg_md5_binary(const void *buff, size_t len, void *outbuf, const char **errstr) { pg_cryptohash_ctx *ctx; + *errstr = NULL; ctx = pg_cryptohash_create(PG_MD5); if (ctx == NULL) + { + *errstr = pg_cryptohash_error(NULL); /* returns OOM */ return false; + } if (pg_cryptohash_init(ctx) < 0 || pg_cryptohash_update(ctx, buff, len) < 0 || pg_cryptohash_final(ctx, outbuf, MD5_DIGEST_LENGTH) < 0) { + *errstr = pg_cryptohash_error(ctx); pg_cryptohash_free(ctx); return false; } @@ -118,11 +124,12 @@ pg_md5_binary(const void *buff, size_t len, void *outbuf) * Output format is "md5" followed by a 32-hex-digit MD5 checksum. * Hence, the output buffer "buf" must be at least 36 bytes long. * - * Returns true if okay, false on error (out of memory). + * Returns true if okay, false on error with *errstr providing some + * error context. */ bool pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len, - char *buf) + char *buf, const char **errstr) { size_t passwd_len = strlen(passwd); @@ -131,7 +138,10 @@ pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len, bool ret; if (!crypt_buf) + { + *errstr = _("out of memory"); return false; + } /* * Place salt at the end because it may be known by users trying to crack @@ -141,7 +151,7 @@ pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len, memcpy(crypt_buf + passwd_len, salt, salt_len); strcpy(buf, "md5"); - ret = pg_md5_hash(crypt_buf, passwd_len + salt_len, buf + 3); + ret = pg_md5_hash(crypt_buf, passwd_len + salt_len, buf + 3, errstr); free(crypt_buf); |