comparison mcabber/mcabber/utils.c @ 2203:170597f5365b

Use more generic routines to convert fingerprints to/from hexadecimal This is a first step towards non-MD5 fingerprints; now we need Loudmouth support...
author Mikael Berthe <mikael@lilotux.net>
date Mon, 12 Oct 2015 17:19:19 +0200
parents 1591518a33b9
children 377b6a52b25f
comparison
equal deleted inserted replaced
2202:ff18feb75a6d 2203:170597f5365b
153 return g_strdup_printf("%s%s", homedir, fname+1); 153 return g_strdup_printf("%s%s", homedir, fname+1);
154 } 154 }
155 return g_strdup(fname); 155 return g_strdup(fname);
156 } 156 }
157 157
158 void fingerprint_to_hex(const char *fprstr, char hex[48]) 158 // fingerprint_to_hex(fprstr, hex, fpr_len)
159 { 159 // Convert the binary fingerprint fprstr (which is fpr_len bytes long)
160 int i; 160 // to a NULL-terminated hexadecimal string hex.
161 // The destination array hex should have been preallocated by the caller,
162 // and should be big enough (i.e. >= 3*fpr_len bytes).
163 void fingerprint_to_hex(const char *fprstr, char *hex, size_t fpr_len)
164 {
165 unsigned int i;
161 const unsigned char *fpr = (const unsigned char *)fprstr; 166 const unsigned char *fpr = (const unsigned char *)fprstr;
162 char *p; 167 char *p;
163 168
164 hex[0] = 0; 169 hex[0] = 0;
165 if (!fpr) return; 170 if (!fpr || fpr_len < 16) return;
166 171
167 for (p = hex, i = 0; i < 15; i++, p+=3) 172 for (p = hex, i = 0; i < fpr_len - 1; i++, p+=3)
168 g_snprintf(p, 4, "%02X:", fpr[i]); 173 g_snprintf(p, 4, "%02X:", fpr[i]);
169 g_snprintf(p, 3, "%02X", fpr[i]); 174 g_snprintf(p, 3, "%02X", fpr[i]);
170 } 175 }
171 176
172 gboolean hex_to_fingerprint(const char *hex, char fpr[17]) 177 // hex_to_fingerprint(hex, fpr, fpr_len)
173 { 178 // Convert the hexadecimal fingerprint hex to a byte array fpr[].
174 int i; 179 // The fpr array should have been preallocated with a size >= fpr_len.
180 gboolean hex_to_fingerprint(const char *hex, char *fpr, size_t fpr_len)
181 {
182 unsigned int i;
175 const char *p; 183 const char *p;
176 184
185 if (fpr_len < 16) return FALSE;
186
177 fpr[0] = 0; 187 fpr[0] = 0;
178 if (strlen(hex) != 47) 188
189 if (strlen(hex) != fpr_len*3 - 1)
179 return FALSE; 190 return FALSE;
180 for (i = 0, p = hex; *p && *(p+1); i++, p += 3) { 191
181 if (*(p+2) && (*(p+2) != ':')) { 192 for (i = 0, p = hex; i < fpr_len && *p && *(p+1); i++, p += 3) {
182 fpr[i] = 0; 193 // Check we have two hex digits followed by a colon (or end of string)
194 if (!isxdigit(*p) || !isxdigit(*(p+1)))
183 return FALSE; 195 return FALSE;
184 } 196 if (*(p+2) && (*(p+2) != ':'))
197 return FALSE;
185 fpr[i] = (char)g_ascii_strtoull(p, NULL, 16); 198 fpr[i] = (char)g_ascii_strtoull(p, NULL, 16);
186 } 199 }
187 fpr[i] = 0;
188 return TRUE; 200 return TRUE;
189 } 201 }
190 202
191 static gboolean tracelog_create(void) 203 static gboolean tracelog_create(void)
192 { 204 {