| 1 | /* |
| 2 | * Asterisk -- An open source telephony toolkit. |
| 3 | * |
| 4 | * Copyright (C) 2006, Digium, Inc. |
| 5 | * Copyright (C) 2006, Claude Patry |
| 6 | * |
| 7 | * See http://www.asterisk.org for more information about |
| 8 | * the Asterisk project. Please do not directly contact |
| 9 | * any of the maintainers of this project for assistance; |
| 10 | * the project provides a web site, mailing lists and IRC |
| 11 | * channels for your use. |
| 12 | * |
| 13 | * This program is free software, distributed under the terms of |
| 14 | * the GNU General Public License Version 2. See the LICENSE file |
| 15 | * at the top of the source tree. |
| 16 | */ |
| 17 | |
| 18 | /*! \file |
| 19 | * |
| 20 | * \brief SHA1 digest related dialplan functions |
| 21 | * |
| 22 | * \author Claude Patry <[email protected]> |
| 23 | * |
| 24 | * \ingroup functions |
| 25 | */ |
| 26 | |
| 27 | /*** MODULEINFO |
| 28 | <support_level>core</support_level> |
| 29 | ***/ |
| 30 | |
| 31 | #include "asterisk.h" |
| 32 | |
| 33 | #include "asterisk/module.h" |
| 34 | #include "asterisk/pbx.h" |
| 35 | |
| 36 | /*** DOCUMENTATION |
| 37 | <function name="SHA1" language="en_US"> |
| 38 | <since> |
| 39 | <version>1.4.0</version> |
| 40 | </since> |
| 41 | <synopsis> |
| 42 | Computes a SHA1 digest. |
| 43 | </synopsis> |
| 44 | <syntax> |
| 45 | <parameter name="data" required="true"> |
| 46 | <para>Input string</para> |
| 47 | </parameter> |
| 48 | </syntax> |
| 49 | <description> |
| 50 | <para>Generate a SHA1 digest via the SHA1 algorythm.</para> |
| 51 | <example title="Set sha1hash variable to SHA1 hash of junky"> |
| 52 | exten => s,1,Set(sha1hash=${SHA1(junky)}) |
| 53 | </example> |
| 54 | <para>The example above sets the asterisk variable sha1hash to the string <literal>60fa5675b9303eb62f99a9cd47f9f5837d18f9a0</literal> |
| 55 | which is known as its hash</para> |
| 56 | </description> |
| 57 | </function> |
| 58 | ***/ |
| 59 | |
| 60 | static int sha1(struct ast_channel *chan, const char *cmd, char *data, |
| 61 | char *buf, size_t len) |
| 62 | { |
| 63 | *buf = '\0'; |
| 64 | |
| 65 | if (ast_strlen_zero(data)) { |
| 66 | ast_log(LOG_WARNING, "Syntax: SHA1(<data>) - missing argument!\n"); |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | if (len >= 41) |
| 71 | ast_sha1_hash(buf, data); |
| 72 | else { |
| 73 | ast_log(LOG_ERROR, |
| 74 | "Insufficient space to produce SHA1 hash result (%d < 41)\n", |
| 75 | (int) len); |
| 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static struct ast_custom_function sha1_function = { |
| 82 | .name = "SHA1", |
| 83 | .read = sha1, |
| 84 | .read_max = 42, |
| 85 | }; |
| 86 | |
| 87 | static int unload_module(void) |
| 88 | { |
| 89 | return ast_custom_function_unregister(&sha1_function); |
| 90 | } |
| 91 | |
| 92 | static int load_module(void) |
| 93 | { |
| 94 | return ast_custom_function_register(&sha1_function); |
| 95 | } |
| 96 | |
| 97 | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SHA-1 computation dialplan function"); |