@@ -68,123 +68,93 @@ md5_bytea(PG_FUNCTION_ARGS)
68
68
PG_RETURN_TEXT_P (cstring_to_text (hexsum ));
69
69
}
70
70
71
-
72
71
/*
73
- * SHA-2 variants
72
+ * Internal routine to compute a cryptohash with the given bytea input.
74
73
*/
75
-
76
- Datum
77
- sha224_bytea (PG_FUNCTION_ARGS )
74
+ static inline bytea *
75
+ cryptohash_internal (pg_cryptohash_type type , bytea * input )
78
76
{
79
- bytea * in = PG_GETARG_BYTEA_PP (0 );
80
77
const uint8 * data ;
78
+ const char * typestr = NULL ;
79
+ int digest_len = 0 ;
81
80
size_t len ;
82
81
pg_cryptohash_ctx * ctx ;
83
- unsigned char buf [PG_SHA224_DIGEST_LENGTH ];
84
82
bytea * result ;
85
83
86
- len = VARSIZE_ANY_EXHDR (in );
87
- data = (unsigned char * ) VARDATA_ANY (in );
88
-
89
- ctx = pg_cryptohash_create (PG_SHA224 );
84
+ switch (type )
85
+ {
86
+ case PG_SHA224 :
87
+ typestr = "SHA224" ;
88
+ digest_len = PG_SHA224_DIGEST_LENGTH ;
89
+ break ;
90
+ case PG_SHA256 :
91
+ typestr = "SHA256" ;
92
+ digest_len = PG_SHA256_DIGEST_LENGTH ;
93
+ break ;
94
+ case PG_SHA384 :
95
+ typestr = "SHA384" ;
96
+ digest_len = PG_SHA384_DIGEST_LENGTH ;
97
+ break ;
98
+ case PG_SHA512 :
99
+ typestr = "SHA512" ;
100
+ digest_len = PG_SHA512_DIGEST_LENGTH ;
101
+ break ;
102
+ case PG_MD5 :
103
+ case PG_SHA1 :
104
+ elog (ERROR , "unsupported cryptohash type %d" , type );
105
+ break ;
106
+ }
107
+
108
+ result = palloc0 (digest_len + VARHDRSZ );
109
+ len = VARSIZE_ANY_EXHDR (input );
110
+ data = (unsigned char * ) VARDATA_ANY (input );
111
+
112
+ ctx = pg_cryptohash_create (type );
90
113
if (pg_cryptohash_init (ctx ) < 0 )
91
- elog (ERROR , "could not initialize %s context" , "SHA224" );
114
+ elog (ERROR , "could not initialize %s context" , typestr );
92
115
if (pg_cryptohash_update (ctx , data , len ) < 0 )
93
- elog (ERROR , "could not update %s context" , "SHA224" );
94
- if (pg_cryptohash_final (ctx , buf ) < 0 )
95
- elog (ERROR , "could not finalize %s context" , "SHA224" );
116
+ elog (ERROR , "could not update %s context" , typestr );
117
+ if (pg_cryptohash_final (ctx , ( unsigned char * ) VARDATA ( result ) ) < 0 )
118
+ elog (ERROR , "could not finalize %s context" , typestr );
96
119
pg_cryptohash_free (ctx );
97
120
98
- result = palloc (sizeof (buf ) + VARHDRSZ );
99
- SET_VARSIZE (result , sizeof (buf ) + VARHDRSZ );
100
- memcpy (VARDATA (result ), buf , sizeof (buf ));
121
+ SET_VARSIZE (result , digest_len + VARHDRSZ );
101
122
102
- PG_RETURN_BYTEA_P ( result ) ;
123
+ return result ;
103
124
}
104
125
126
+ /*
127
+ * SHA-2 variants
128
+ */
129
+
105
130
Datum
106
- sha256_bytea (PG_FUNCTION_ARGS )
131
+ sha224_bytea (PG_FUNCTION_ARGS )
107
132
{
108
- bytea * in = PG_GETARG_BYTEA_PP (0 );
109
- const uint8 * data ;
110
- size_t len ;
111
- pg_cryptohash_ctx * ctx ;
112
- unsigned char buf [PG_SHA256_DIGEST_LENGTH ];
113
- bytea * result ;
133
+ bytea * result = cryptohash_internal (PG_SHA224 , PG_GETARG_BYTEA_PP (0 ));
114
134
115
- len = VARSIZE_ANY_EXHDR (in );
116
- data = (unsigned char * ) VARDATA_ANY (in );
117
-
118
- ctx = pg_cryptohash_create (PG_SHA256 );
119
- if (pg_cryptohash_init (ctx ) < 0 )
120
- elog (ERROR , "could not initialize %s context" , "SHA256" );
121
- if (pg_cryptohash_update (ctx , data , len ) < 0 )
122
- elog (ERROR , "could not update %s context" , "SHA256" );
123
- if (pg_cryptohash_final (ctx , buf ) < 0 )
124
- elog (ERROR , "could not finalize %s context" , "SHA256" );
125
- pg_cryptohash_free (ctx );
135
+ PG_RETURN_BYTEA_P (result );
136
+ }
126
137
127
- result = palloc (sizeof (buf ) + VARHDRSZ );
128
- SET_VARSIZE (result , sizeof (buf ) + VARHDRSZ );
129
- memcpy (VARDATA (result ), buf , sizeof (buf ));
138
+ Datum
139
+ sha256_bytea (PG_FUNCTION_ARGS )
140
+ {
141
+ bytea * result = cryptohash_internal (PG_SHA256 , PG_GETARG_BYTEA_PP (0 ));
130
142
131
143
PG_RETURN_BYTEA_P (result );
132
144
}
133
145
134
146
Datum
135
147
sha384_bytea (PG_FUNCTION_ARGS )
136
148
{
137
- bytea * in = PG_GETARG_BYTEA_PP (0 );
138
- const uint8 * data ;
139
- size_t len ;
140
- pg_cryptohash_ctx * ctx ;
141
- unsigned char buf [PG_SHA384_DIGEST_LENGTH ];
142
- bytea * result ;
143
-
144
- len = VARSIZE_ANY_EXHDR (in );
145
- data = (unsigned char * ) VARDATA_ANY (in );
146
-
147
- ctx = pg_cryptohash_create (PG_SHA384 );
148
- if (pg_cryptohash_init (ctx ) < 0 )
149
- elog (ERROR , "could not initialize %s context" , "SHA384" );
150
- if (pg_cryptohash_update (ctx , data , len ) < 0 )
151
- elog (ERROR , "could not update %s context" , "SHA384" );
152
- if (pg_cryptohash_final (ctx , buf ) < 0 )
153
- elog (ERROR , "could not finalize %s context" , "SHA384" );
154
- pg_cryptohash_free (ctx );
155
-
156
- result = palloc (sizeof (buf ) + VARHDRSZ );
157
- SET_VARSIZE (result , sizeof (buf ) + VARHDRSZ );
158
- memcpy (VARDATA (result ), buf , sizeof (buf ));
149
+ bytea * result = cryptohash_internal (PG_SHA384 , PG_GETARG_BYTEA_PP (0 ));
159
150
160
151
PG_RETURN_BYTEA_P (result );
161
152
}
162
153
163
154
Datum
164
155
sha512_bytea (PG_FUNCTION_ARGS )
165
156
{
166
- bytea * in = PG_GETARG_BYTEA_PP (0 );
167
- const uint8 * data ;
168
- size_t len ;
169
- pg_cryptohash_ctx * ctx ;
170
- unsigned char buf [PG_SHA512_DIGEST_LENGTH ];
171
- bytea * result ;
172
-
173
- len = VARSIZE_ANY_EXHDR (in );
174
- data = (unsigned char * ) VARDATA_ANY (in );
175
-
176
- ctx = pg_cryptohash_create (PG_SHA512 );
177
- if (pg_cryptohash_init (ctx ) < 0 )
178
- elog (ERROR , "could not initialize %s context" , "SHA512" );
179
- if (pg_cryptohash_update (ctx , data , len ) < 0 )
180
- elog (ERROR , "could not update %s context" , "SHA512" );
181
- if (pg_cryptohash_final (ctx , buf ) < 0 )
182
- elog (ERROR , "could not finalize %s context" , "SHA512" );
183
- pg_cryptohash_free (ctx );
184
-
185
- result = palloc (sizeof (buf ) + VARHDRSZ );
186
- SET_VARSIZE (result , sizeof (buf ) + VARHDRSZ );
187
- memcpy (VARDATA (result ), buf , sizeof (buf ));
157
+ bytea * result = cryptohash_internal (PG_SHA512 , PG_GETARG_BYTEA_PP (0 ));
188
158
189
159
PG_RETURN_BYTEA_P (result );
190
160
}
0 commit comments