• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/lib/digest.c

  1 /*
  2  * Copyright (C) 2024 Sebastian Ertz <sebastian.ertz@gmx.de>
  3  *
  4  * Permission to use, copy, modify, and/or distribute this software for any
  5  * purpose with or without fee is hereby granted, provided that the above
  6  * copyright notice and this permission notice appear in all copies.
  7  *
  8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15  */
 16 
 17 /**
 18  * # Digest Functions
 19  *
 20  * The `digest` module bundles various digest functions.
 21  *
 22  * @module digest
 23  */
 24 
 25 #include <md5.h>
 26 #include <sha1.h>
 27 #include <sha2.h>
 28 
 29 #ifdef HAVE_DIGEST_EXTENDED
 30 #include <md2.h>
 31 #include <md4.h>
 32 #endif
 33 
 34 #include "ucode/module.h"
 35 
 36 
 37 static uc_value_t *
 38 uc_digest_calc_data(uc_value_t *str, char *(*fn)(const uint8_t *,size_t,char *))
 39 {
 40         char buf[SHA512_DIGEST_STRING_LENGTH];
 41 
 42         if( ucv_type(str) != UC_STRING )
 43                 return NULL;
 44 
 45         if( fn((const uint8_t *)ucv_string_get(str), ucv_string_length(str), buf) )
 46                 return ucv_string_new(buf);
 47 
 48         return NULL;
 49 }
 50 
 51 static uc_value_t *
 52 uc_digest_calc_file(uc_value_t *path, char *(fn)(const char *,char *))
 53 {
 54         char buf[SHA512_DIGEST_STRING_LENGTH];
 55 
 56         if( ucv_type(path) != UC_STRING )
 57                 return NULL;
 58 
 59         if( fn(ucv_string_get(path), buf) )
 60                 return ucv_string_new(buf);
 61 
 62         return NULL;
 63 }
 64 
 65 /**
 66  * Calculates the MD5 hash of string and returns that hash.
 67  *
 68  * Returns `null` if a non-string argument is given.
 69  *
 70  * @function module:digest#md5
 71  *
 72  * @param {string} str
 73  * The string to hash.
 74  *
 75  * @returns {?string}
 76  *
 77  * @example
 78  * md5("This is a test");  // Returns "ce114e4501d2f4e2dcea3e17b546f339"
 79  * md5(123);               // Returns null
 80  */
 81 static uc_value_t *
 82 uc_digest_md5(uc_vm_t *vm, size_t nargs)
 83 {
 84         return uc_digest_calc_data(uc_fn_arg(0), MD5Data);
 85 }
 86 
 87 /**
 88  * Calculates the SHA1 hash of string and returns that hash.
 89  *
 90  * Returns `null` if a non-string argument is given.
 91  *
 92  * @function module:digest#sha1
 93  *
 94  * @param {string} str
 95  * The string to hash.
 96  *
 97  * @returns {?string}
 98  *
 99  * @example
100  * sha1("This is a test");  // Returns "a54d88e06612d820bc3be72877c74f257b561b19"
101  * sha1(123);               // Returns null
102  */
103 static uc_value_t *
104 uc_digest_sha1(uc_vm_t *vm, size_t nargs)
105 {
106         return uc_digest_calc_data(uc_fn_arg(0), SHA1Data);
107 }
108 
109 /**
110  * Calculates the SHA256 hash of string and returns that hash.
111  *
112  * Returns `null` if a non-string argument is given.
113  *
114  * @function module:digest#sha256
115  *
116  * @param {string} str
117  * The string to hash.
118  *
119  * @returns {?string}
120  *
121  * @example
122  * sha256("This is a test");  // Returns "c7be1ed902fb8dd4d48997c6452f5d7e509fbcdbe2808b16bcf4edce4c07d14e"
123  * sha256(123);               // Returns null
124  */
125 static uc_value_t *
126 uc_digest_sha256(uc_vm_t *vm, size_t nargs)
127 {
128         return uc_digest_calc_data(uc_fn_arg(0), SHA256Data);
129 }
130 
131 #ifdef HAVE_DIGEST_EXTENDED
132 /**
133  * Calculates the MD2 hash of string and returns that hash.
134  *
135  * Returns `null` if a non-string argument is given.
136  *
137  * @function module:digest#md2
138  *
139  * @param {string} str
140  * The string to hash.
141  *
142  * @returns {?string}
143  *
144  * @example
145  * md2("This is a test");  // Returns "dc378580fd0722e56b82666a6994c718"
146  * md2(123);               // Returns null
147  */
148 static uc_value_t *
149 uc_digest_md2(uc_vm_t *vm, size_t nargs)
150 {
151         return uc_digest_calc_data(uc_fn_arg(0), MD2Data);
152 }
153 
154 /**
155  * Calculates the MD4 hash of string and returns that hash.
156  *
157  * Returns `null` if a non-string argument is given.
158  *
159  * @function module:digest#md4
160  *
161  * @param {string} str
162  * The string to hash.
163  *
164  * @returns {?string}
165  *
166  * @example
167  * md4("This is a test");  // Returns "3b487cf6856af7e330bc4b1b7d977ef8"
168  * md4(123);               // Returns null
169  */
170 static uc_value_t *
171 uc_digest_md4(uc_vm_t *vm, size_t nargs)
172 {
173         return uc_digest_calc_data(uc_fn_arg(0), MD4Data);
174 }
175 
176 /**
177  * Calculates the SHA384 hash of string and returns that hash.
178  *
179  * Returns `null` if a non-string argument is given.
180  *
181  * @function module:digest#sha384
182  *
183  * @param {string} str
184  * The string to hash.
185  *
186  * @returns {?string}
187  *
188  * @example
189  * sha384("This is a test");  // Returns "a27c7667e58200d4c0688ea136968404a0da366b1a9fc19bb38a0c7a609a1eef2bcc82837f4f4d92031a66051494b38c"
190  * sha384(123);               // Returns null
191  */
192 static uc_value_t *
193 uc_digest_sha384(uc_vm_t *vm, size_t nargs)
194 {
195         return uc_digest_calc_data(uc_fn_arg(0), SHA384Data);
196 }
197 
198 /**
199  * Calculates the SHA384 hash of string and returns that hash.
200  *
201  * Returns `null` if a non-string argument is given.
202  *
203  * @function module:digest#sha384
204  *
205  * @param {string} str
206  * The string to hash.
207  *
208  * @returns {?string}
209  *
210  * @example
211  * sha512("This is a test");  // Returns "a028d4f74b602ba45eb0a93c9a4677240dcf281a1a9322f183bd32f0bed82ec72de9c3957b2f4c9a1ccf7ed14f85d73498df38017e703d47ebb9f0b3bf116f69"
212  * sha512(123);               // Returns null
213  */
214 static uc_value_t *
215 uc_digest_sha512(uc_vm_t *vm, size_t nargs)
216 {
217         return uc_digest_calc_data(uc_fn_arg(0), SHA512Data);
218 }
219 #endif
220 
221 /**
222  * Calculates the MD5 hash of a given file and returns that hash.
223  *
224  * Returns `null` if an error occurred.
225  *
226  * @function module:digest#md5_file
227  *
228  * @param {string} path
229  * The path to the file.
230  *
231  * @returns {?string}
232  */
233 static uc_value_t *
234 uc_digest_md5_file(uc_vm_t *vm, size_t nargs)
235 {
236         return uc_digest_calc_file(uc_fn_arg(0), MD5File);
237 }
238 
239 /**
240  * Calculates the SHA1 hash of a given file and returns that hash.
241  *
242  * Returns `null` if an error occurred.
243  *
244  * @function module:digest#sha1_file
245  *
246  * @param {string} path
247  * The path to the file.
248  *
249  * @returns {?string}
250  */
251 static uc_value_t *
252 uc_digest_sha1_file(uc_vm_t *vm, size_t nargs)
253 {
254         return uc_digest_calc_file(uc_fn_arg(0), SHA1File);
255 }
256 
257 /**
258  * Calculates the SHA256 hash of a given file and returns that hash.
259  *
260  * Returns `null` if an error occurred.
261  *
262  * @function module:digest#sha256_file
263  *
264  * @param {string} path
265  * The path to the file.
266  *
267  * @returns {?string}
268  */
269 static uc_value_t *
270 uc_digest_sha256_file(uc_vm_t *vm, size_t nargs)
271 {
272         return uc_digest_calc_file(uc_fn_arg(0), SHA256File);
273 }
274 
275 #ifdef HAVE_DIGEST_EXTENDED
276 /**
277  * Calculates the MD2 hash of a given file and returns that hash.
278  *
279  * Returns `null` if an error occurred.
280  *
281  * @function module:digest#md2_file
282  *
283  * @param {string} path
284  * The path to the file.
285  *
286  * @returns {?string}
287  */
288 static uc_value_t *
289 uc_digest_md2_file(uc_vm_t *vm, size_t nargs)
290 {
291         return uc_digest_calc_file(uc_fn_arg(0), MD2File);
292 }
293 
294 /**
295  * Calculates the MD4 hash of a given file and returns that hash.
296  *
297  * Returns `null` if an error occurred.
298  *
299  * @function module:digest#md4_file
300  *
301  * @param {string} path
302  * The path to the file.
303  *
304  * @returns {?string}
305  */
306 static uc_value_t *
307 uc_digest_md4_file(uc_vm_t *vm, size_t nargs)
308 {
309         return uc_digest_calc_file(uc_fn_arg(0), MD4File);
310 }
311 
312 /**
313  * Calculates the SHA384 hash of a given file and returns that hash.
314  *
315  * Returns `null` if an error occurred.
316  *
317  * @function module:digest#sha384_file
318  *
319  * @param {string} path
320  * The path to the file.
321  *
322  * @returns {?string}
323  */
324 static uc_value_t *
325 uc_digest_sha384_file(uc_vm_t *vm, size_t nargs)
326 {
327         return uc_digest_calc_file(uc_fn_arg(0), SHA384File);
328 }
329 
330 /**
331  * Calculates the SHA512 hash of a given file and returns that hash.
332  *
333  * Returns `null` if an error occurred.
334  *
335  * @function module:digest#sha512_file
336  *
337  * @param {string} path
338  * The path to the file.
339  *
340  * @returns {?string}
341  */
342 static uc_value_t *
343 uc_digest_sha512_file(uc_vm_t *vm, size_t nargs)
344 {
345         return uc_digest_calc_file(uc_fn_arg(0), SHA512File);
346 }
347 #endif
348 
349 
350 static const uc_function_list_t global_fns[] = {
351         { "md5",         uc_digest_md5         },
352         { "sha1",        uc_digest_sha1        },
353         { "sha256",      uc_digest_sha256      },
354         { "md5_file",    uc_digest_md5_file    },
355         { "sha1_file",   uc_digest_sha1_file   },
356         { "sha256_file", uc_digest_sha256_file },
357 #ifdef HAVE_DIGEST_EXTENDED
358         { "md2",         uc_digest_md2         },
359         { "md4",         uc_digest_md4         },
360         { "sha384",      uc_digest_sha384      },
361         { "sha512",      uc_digest_sha512      },
362         { "md2_file",    uc_digest_md2_file    },
363         { "md4_file",    uc_digest_md4_file    },
364         { "sha384_file", uc_digest_sha384_file },
365         { "sha512_file", uc_digest_sha512_file },
366 #endif
367 };
368 
369 void uc_module_init(uc_vm_t *vm, uc_value_t *scope)
370 {
371         uc_function_list_register(scope, global_fns);
372 }
373 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt