-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshacrypt.c
More file actions
60 lines (46 loc) · 1.56 KB
/
shacrypt.c
File metadata and controls
60 lines (46 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* shacrypt.c
*
* PostgreSQL extension to generate SHA512-CRYPT and SHA256-CRYPT
* password hashes.
*
* SHA-crypt code by Ulrich Drepper
* See https://www.akkadia.org/drepper/SHA-crypt.txt
* for the specification and code.
*
* PostgreSQL interface by Daniel Vérité, 2018. See LICENSE.md
*/
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "sha256.h"
#include "sha512.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(pg_sha256_crypt);
Datum pg_sha256_crypt(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pg_sha512_crypt);
Datum pg_sha512_crypt(PG_FUNCTION_ARGS);
Datum pg_sha256_crypt(PG_FUNCTION_ARGS)
{
char* str_input = text_to_cstring(PG_GETARG_TEXT_PP(0));
char* str_salt = text_to_cstring(PG_GETARG_TEXT_PP(1));
/* The allocation size is computed as in sha256_crypt() */
int buf_size = (sizeof (SHA256_SALT_PREFIX) - 1
+ sizeof (SHA256_ROUNDS_PREFIX) + 9 + 1
+ strlen (str_salt) + 1 + 86 + 1);
char *buffer = palloc(buf_size);
char *result = sha256_crypt_r(str_input, str_salt, buffer, buf_size);
PG_RETURN_TEXT_P(cstring_to_text(result));
}
Datum pg_sha512_crypt(PG_FUNCTION_ARGS)
{
char* str_input = text_to_cstring(PG_GETARG_TEXT_PP(0));
char* str_salt = text_to_cstring(PG_GETARG_TEXT_PP(1));
/* The allocation size is computed as in sha512_crypt() */
int buf_size = (sizeof (SHA512_SALT_PREFIX) - 1
+ sizeof (SHA512_ROUNDS_PREFIX) + 9 + 1
+ strlen (str_salt) + 1 + 86 + 1);
char *buffer = palloc(buf_size);
char *result = sha512_crypt_r(str_input, str_salt, buffer, buf_size);
PG_RETURN_TEXT_P(cstring_to_text(result));
}