+ return silc_strdup(buf);
+}
+
+/* Creates fingerprint from data, usually used with SHA1 digests */
+
+char *silc_fingerprint(const unsigned char *data, SilcUInt32 data_len)
+{
+ unsigned char *fingerprint, *cp;
+ unsigned int len, blocks, i;
+
+ if (!data || !data_len) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ return NULL;
+ }
+
+ if (data_len >= 256)
+ data_len = 255;
+
+ /* Align and calculate total length */
+ len = ((data_len + 19) / 20) * 20;
+ blocks = (len / 10);
+ len = (len * 2) + ((blocks - 1) * 2) + (4 * blocks) + 2 + 1;
+
+ cp = fingerprint = silc_calloc(len, sizeof(*fingerprint));
+ if (!cp)
+ return NULL;
+
+ for (i = 0; i < data_len; i++) {
+ silc_snprintf(cp, len, "%02X", data[i]);
+ cp += 2;
+ len -= 2;
+
+ if ((i + 1) % 2 == 0)
+ silc_snprintf(cp++, len--, " ");
+ if ((i + 1) % 10 == 0)
+ silc_snprintf(cp++, len--, " ");
+ }
+ i--;
+ if ((i + 1) % 10 == 0)
+ *(--cp) = '\0';
+ if ((i + 1) % 2 == 0)
+ *(--cp) = '\0';
+
+ return fingerprint;
+}
+
+/* Return TRUE if the `data' is ASCII string. */
+
+SilcBool silc_string_is_ascii(const unsigned char *data, SilcUInt32 data_len)
+{
+ int i;
+
+ for (i = 0; i < data_len; i++) {
+ if (!isascii(data[i]))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/* Displays input prompt on command line and takes input data from user */
+
+char *silc_get_input(const char *prompt, SilcBool echo_off)
+{
+#ifdef SILC_UNIX
+ int fd;
+ char input[2048];
+
+ if (echo_off) {
+ char *ret = NULL;
+#ifdef HAVE_TERMIOS_H
+ struct termios to;
+ struct termios to_old;
+
+ fd = open("/dev/tty", O_RDONLY);
+ if (fd < 0) {
+ silc_set_errno_posix(errno);
+ return NULL;
+ }
+
+ signal(SIGINT, SIG_IGN);
+
+ /* Get terminal info */
+ tcgetattr(fd, &to);
+ to_old = to;
+
+ /* Echo OFF, and assure we can prompt and get input */
+ to.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
+ to.c_lflag |= ICANON;
+ to.c_cc[VMIN] = 255;
+ tcsetattr(fd, TCSANOW, &to);
+
+ memset(input, 0, sizeof(input));
+
+ printf("%s", prompt);
+ fflush(stdout);
+
+ if ((read(fd, input, sizeof(input))) < 0) {
+ silc_set_errno_posix(errno);
+ tcsetattr(fd, TCSANOW, &to_old);
+ return NULL;
+ }
+
+ if (strlen(input) <= 1) {
+ tcsetattr(fd, TCSANOW, &to_old);
+ silc_set_errno(SILC_ERR_EOF);
+ return NULL;
+ }
+
+ if (strchr(input, '\n'))
+ *strchr(input, '\n') = '\0';
+
+ /* Restore old terminfo */
+ tcsetattr(fd, TCSANOW, &to_old);
+ signal(SIGINT, SIG_DFL);
+
+ ret = silc_memdup(input, strlen(input));
+ memset(input, 0, sizeof(input));
+#endif /* HAVE_TERMIOS_H */
+ return ret;
+ } else {
+ fd = open("/dev/tty", O_RDONLY);
+ if (fd < 0) {
+ silc_set_errno_posix(errno);
+ return NULL;
+ }
+
+ memset(input, 0, sizeof(input));
+
+ printf("%s", prompt);
+ fflush(stdout);
+
+ if ((read(fd, input, sizeof(input))) < 0) {
+ silc_set_errno_posix(errno);
+ return NULL;
+ }
+
+ if (strlen(input) <= 1) {
+ silc_set_errno(SILC_ERR_EOF);
+ return NULL;
+ }
+
+ if (strchr(input, '\n'))
+ *strchr(input, '\n') = '\0';
+
+ return silc_strdup(input);
+ }
+#else
+ return NULL;
+#endif /* SILC_UNIX */
+}
+
+/* Hexdump */
+
+void silc_hexdump(const unsigned char *data, SilcUInt32 data_len,
+ FILE *output)
+{
+ int i, k;
+ int off, pos, count;
+ int len = data_len;
+
+ k = 0;
+ pos = 0;
+ count = 16;
+ off = len % 16;
+ while (1) {
+ if (off) {
+ if ((len - pos) < 16 && (len - pos <= len - off))
+ count = off;
+ } else {
+ if (pos == len)
+ count = 0;
+ }
+ if (off == len)
+ count = len;
+
+ if (count)
+ fprintf(output, "%08X ", k++ * 16);
+
+ for (i = 0; i < count; i++) {
+ fprintf(output, "%02X ", data[pos + i]);
+
+ if ((i + 1) % 4 == 0)
+ fprintf(output, " ");
+ }
+
+ if (count && count < 16) {
+ int j;
+
+ for (j = 0; j < 16 - count; j++) {
+ fprintf(output, " ");
+
+ if ((j + count + 1) % 4 == 0)
+ fprintf(output, " ");
+ }
+ }
+
+ for (i = 0; i < count; i++) {
+ char ch;
+
+ if (data[pos] < 32 || data[pos] >= 127)
+ ch = '.';
+ else
+ ch = data[pos];
+
+ fprintf(output, "%c", ch);
+ pos++;
+ }
+
+ if (count)
+ fprintf(output, "\n");
+
+ if (count < 16)
+ break;
+ }
+}
+
+/* Convert hex string to data. Each hex number must have two characters. */
+
+SilcBool silc_hex2data(const char *hex, unsigned char *data,
+ SilcUInt32 data_size, SilcUInt32 *ret_data_len)
+{
+ char *cp = (char *)hex;
+ unsigned char l, h;
+ int i;
+
+ if (data_size < strlen(hex) / 2) {
+ silc_set_errno(SILC_ERR_OVERFLOW);
+ return FALSE;
+ }
+
+ for (i = 0; i < strlen(hex) / 2; i++) {
+ h = *cp++;
+ l = *cp++;
+
+ h -= h < 'A' ? '0' : 'A' - 10;
+ l -= l < 'A' ? '0' : 'A' - 10;
+
+ data[i] = (h << 4) | (l & 0xf);
+ }
+
+ if (ret_data_len)
+ *ret_data_len = i;
+
+ return TRUE;
+}
+
+/* Converts binary data to HEX string */
+
+SilcBool silc_data2hex(const unsigned char *data, SilcUInt32 data_len,
+ char *hex, SilcUInt32 hex_size)
+{
+ unsigned char l, h;
+ char *cp = hex;
+ int i;
+
+ if (hex_size - 1 < data_len * 2) {
+ silc_set_errno(SILC_ERR_OVERFLOW);
+ return FALSE;
+ }
+
+ memset(hex, 0, hex_size);
+
+ for (i = 0; i < data_len; i++) {
+ l = data[i];
+ h = l >> 4;
+ l &= 0xf;
+
+ *cp++ = h + (h > 9 ? 'A' - 10 : '0');
+ *cp++ = l + (l > 9 ? 'A' - 10 : '0');
+ }
+
+ return TRUE;