diff options
author | Andreas Schneider <mail@cynapses.org> | 2009-03-30 13:11:47 +0000 |
---|---|---|
committer | Andreas Schneider <mail@cynapses.org> | 2009-03-30 13:11:47 +0000 |
commit | ae4ef84702cbecd4ca004349bcb4ff6cae6153fe (patch) | |
tree | 783a51217c96f5c3cb245ea1145bda425c2f819e /libssh | |
parent | 1fd7a875beca36597e987729bd76928a9593cfd1 (diff) | |
download | libssh-ae4ef84702cbecd4ca004349bcb4ff6cae6153fe.tar.gz libssh-ae4ef84702cbecd4ca004349bcb4ff6cae6153fe.tar.xz libssh-ae4ef84702cbecd4ca004349bcb4ff6cae6153fe.zip |
Create a ssh_get_hexa function.
This function converts a buffer into a colon separated hex string.
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@308 7dcaeef0-15fb-0310-b436-a5af3365683c
Diffstat (limited to 'libssh')
-rw-r--r-- | libssh/dh.c | 57 | ||||
-rw-r--r-- | libssh/libssh.vers | 2 |
2 files changed, 48 insertions, 11 deletions
diff --git a/libssh/dh.c b/libssh/dh.c index 924c5755..377cfe34 100644 --- a/libssh/dh.c +++ b/libssh/dh.c @@ -141,17 +141,54 @@ void ssh_print_bignum(char *which,bignum num){ free(hex); } -void ssh_print_hexa(char *descr, const unsigned char *what, int len){ - int i; - printf("%s : ",descr); - if(len>16) - printf ("\n "); - for(i=0;i<len-1;i++){ - printf("%.2hhx:",what[i]); - if((i+1) % 16 ==0) - printf("\n "); +/** + * @brief Convert a buffer into a colon separated hex string. + * The caller has to free the memory. + * + * @param what What should be converted to a hex string. + * + * @param len Length of the buffer to convert. + * + * @return The hex string or NULL on error. + */ +char *ssh_get_hexa(const unsigned char *what, size_t len) { + char *hexa = NULL; + size_t i; + + hexa = malloc(len * 3 + 1); + if (hexa == NULL) { + return NULL; + } + + ZERO_STRUCTP(hexa); + + for (i = 0; i < len; i++) { + char hex[4]; + snprintf(hex, sizeof(hex), "%02x:", what[i]); + strcat(hexa, hex); + } + + hexa[(len * 3) - 1] = '\0'; + + return hexa; +} + +/** + * @brief Print a buffer as colon separated hex string. + * + * @param descr Description printed infront of the hex string. + * + * @param what What should be converted to a hex string. + * + * @param len Length of the buffer to convert. + */ +void ssh_print_hexa(const char *descr, const unsigned char *what, size_t len) { + char *hexa = ssh_get_hexa(what, len); + + if (hexa == NULL) { + return; } - printf("%.2hhx\n",what[i]); + printf("%s: %s\n", descr, hexa); } void dh_generate_x(SSH_SESSION *session){ diff --git a/libssh/libssh.vers b/libssh/libssh.vers index b28fe633..ae883990 100644 --- a/libssh/libssh.vers +++ b/libssh/libssh.vers @@ -8,7 +8,7 @@ SSH_0.3 { string_from_char; string_len; string_new; string_fill; string_to_char; string_copy; string_burn; string_data; ssh_crypto_init; - ssh_print_hexa; ssh_get_random; + ssh_get_hexa; ssh_print_hexa; ssh_get_random; ssh_get_pubkey_hash; ssh_get_pubkey; ssh_fd_poll; ssh_select; publickey_free; privatekey_from_file; publickey_to_string; publickey_from_privatekey; |