| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2012-2013 Richard Hughes <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the GNU General Public License Version 2 |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <glib.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include "ch-hash.h" |
| 28 |
|
| 29 |
/** |
| 30 |
* ch_sha1_to_string: |
| 31 |
* @sha1: A %ChSha1 |
| 32 |
* |
| 33 |
* Gets a string representation of the SHA1 hash. |
| 34 |
* |
| 35 |
* Return value: A string, free with g_free(). |
| 36 |
* |
| 37 |
* Since: 0.1.29 |
| 38 |
**/ |
| 39 |
gchar * |
| 40 |
ch_sha1_to_string (const ChSha1 *sha1) |
| 41 |
{ |
| 42 |
GString *string = NULL; |
| 43 |
guint i; |
| 44 |
|
| 45 |
g_return_val_if_fail (sha1 != NULL, NULL); |
| 46 |
|
| 47 |
/* read each byte and convert to hex */ |
| 48 |
string = g_string_new (""); |
| 49 |
for (i = 0; i < 20; i++) { |
| 50 |
g_string_append_printf (string, "%02x", |
| 51 |
sha1->bytes[i]); |
| 52 |
} |
| 53 |
return g_string_free (string, FALSE); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* ch_sha1_parse: |
| 58 |
* @value: A string representation of the SHA1 hash |
| 59 |
* @sha1: A %ChSha1 |
| 60 |
* @error: A %GError, or %NULL |
| 61 |
* |
| 62 |
* Parses a SHA1 hash from a string value. |
| 63 |
* |
| 64 |
* Return value: %TRUE for success |
| 65 |
* |
| 66 |
* Since: 0.1.29 |
| 67 |
**/ |
| 68 |
gboolean |
| 69 |
ch_sha1_parse (const gchar *value, ChSha1 *sha1, GError **error) |
| 70 |
{ |
| 71 |
gchar tmp[3] = { '\0', '\0', '\0'}; |
| 72 |
guint i; |
| 73 |
guint len; |
| 74 |
|
| 75 |
g_return_val_if_fail (value != NULL, FALSE); |
| 76 |
g_return_val_if_fail (sha1 != NULL, FALSE); |
| 77 |
|
| 78 |
/* not a SHA1 hash */ |
| 79 |
len = strlen (value); |
| 80 |
if (len != 40) { |
| 81 |
g_set_error (error, 1, 0, |
| 82 |
"Invalid SHA1 hash '%s'", |
| 83 |
value); |
| 84 |
return FALSE; |
| 85 |
} |
| 86 |
|
| 87 |
/* parse */ |
| 88 |
for (i = 0; i < len; i += 2) { |
| 89 |
tmp[0] = value[i+0]; |
| 90 |
tmp[1] = value[i+1]; |
| 91 |
sha1->bytes[i/2] = g_ascii_strtoull (tmp, NULL, 16); |
| 92 |
} |
| 93 |
return TRUE; |
| 94 |
} |