| 1 /* |
|
| 2 * purple |
|
| 3 * |
|
| 4 * Purple is the legal property of its developers, whose names are too numerous |
|
| 5 * to list here. Please refer to the COPYRIGHT file distributed with this |
|
| 6 * source distribution. |
|
| 7 * |
|
| 8 * This program is free software; you can redistribute it and/or modify |
|
| 9 * it under the terms of the GNU General Public License as published by |
|
| 10 * the Free Software Foundation; either version 2 of the License, or |
|
| 11 * (at your option) any later version. |
|
| 12 * |
|
| 13 * This program is distributed in the hope that it will be useful, |
|
| 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 16 * GNU General Public License for more details. |
|
| 17 * |
|
| 18 * You should have received a copy of the GNU General Public License |
|
| 19 * along with this program; if not, write to the Free Software |
|
| 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|
| 21 */ |
|
| 22 #include "internal.h" |
|
| 23 #include "sha256hash.h" |
|
| 24 |
|
| 25 /******************************************************************************* |
|
| 26 * Structs |
|
| 27 ******************************************************************************/ |
|
| 28 #define PURPLE_SHA256_HASH_GET_PRIVATE(obj) \ |
|
| 29 (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_SHA256_HASH, PurpleSHA256HashPrivate)) |
|
| 30 |
|
| 31 typedef struct { |
|
| 32 GChecksum *checksum; |
|
| 33 } PurpleSHA256HashPrivate; |
|
| 34 |
|
| 35 /****************************************************************************** |
|
| 36 * Globals |
|
| 37 *****************************************************************************/ |
|
| 38 static GObjectClass *parent_class = NULL; |
|
| 39 |
|
| 40 /****************************************************************************** |
|
| 41 * Hash Stuff |
|
| 42 *****************************************************************************/ |
|
| 43 |
|
| 44 static void |
|
| 45 purple_sha256_hash_reset(PurpleHash *hash) |
|
| 46 { |
|
| 47 PurpleSHA256Hash *sha256_hash = PURPLE_SHA256_HASH(hash); |
|
| 48 PurpleSHA256HashPrivate *priv = PURPLE_SHA256_HASH_GET_PRIVATE(sha256_hash); |
|
| 49 |
|
| 50 g_return_if_fail(priv != NULL); |
|
| 51 g_return_if_fail(priv->checksum != NULL); |
|
| 52 |
|
| 53 g_checksum_reset(priv->checksum); |
|
| 54 } |
|
| 55 |
|
| 56 static void |
|
| 57 purple_sha256_hash_append(PurpleHash *hash, const guchar *data, |
|
| 58 gsize len) |
|
| 59 { |
|
| 60 PurpleSHA256Hash *sha256_hash = PURPLE_SHA256_HASH(hash); |
|
| 61 PurpleSHA256HashPrivate *priv = PURPLE_SHA256_HASH_GET_PRIVATE(sha256_hash); |
|
| 62 |
|
| 63 g_return_if_fail(priv != NULL); |
|
| 64 g_return_if_fail(priv->checksum != NULL); |
|
| 65 |
|
| 66 while (len >= G_MAXSSIZE) { |
|
| 67 g_checksum_update(priv->checksum, data, G_MAXSSIZE); |
|
| 68 len -= G_MAXSSIZE; |
|
| 69 data += G_MAXSSIZE; |
|
| 70 } |
|
| 71 |
|
| 72 if (len) |
|
| 73 g_checksum_update(priv->checksum, data, len); |
|
| 74 } |
|
| 75 |
|
| 76 static gboolean |
|
| 77 purple_sha256_hash_digest(PurpleHash *hash, guchar *digest, size_t buff_len) |
|
| 78 { |
|
| 79 PurpleSHA256Hash *sha256_hash = PURPLE_SHA256_HASH(hash); |
|
| 80 PurpleSHA256HashPrivate *priv = PURPLE_SHA256_HASH_GET_PRIVATE(sha256_hash); |
|
| 81 |
|
| 82 const gssize required_len = g_checksum_type_get_length(G_CHECKSUM_SHA256); |
|
| 83 gsize digest_len = buff_len; |
|
| 84 |
|
| 85 g_return_val_if_fail(priv != NULL, FALSE); |
|
| 86 g_return_val_if_fail(priv->checksum != NULL, FALSE); |
|
| 87 |
|
| 88 g_return_val_if_fail(required_len >= 0, FALSE); |
|
| 89 g_return_val_if_fail(buff_len >= (gsize)required_len, FALSE); |
|
| 90 |
|
| 91 g_checksum_get_digest(priv->checksum, digest, &digest_len); |
|
| 92 |
|
| 93 if (digest_len != (gsize)required_len) |
|
| 94 return FALSE; |
|
| 95 |
|
| 96 purple_sha256_hash_reset(hash); |
|
| 97 |
|
| 98 return TRUE; |
|
| 99 } |
|
| 100 |
|
| 101 static size_t |
|
| 102 purple_sha256_hash_get_block_size(PurpleHash *hash) |
|
| 103 { |
|
| 104 return 64; |
|
| 105 } |
|
| 106 |
|
| 107 static size_t |
|
| 108 purple_sha256_hash_get_digest_size(PurpleHash *hash) |
|
| 109 { |
|
| 110 return g_checksum_type_get_length(G_CHECKSUM_SHA256); |
|
| 111 } |
|
| 112 |
|
| 113 /****************************************************************************** |
|
| 114 * Object Stuff |
|
| 115 *****************************************************************************/ |
|
| 116 |
|
| 117 static void |
|
| 118 purple_sha256_hash_finalize(GObject *obj) |
|
| 119 { |
|
| 120 PurpleSHA256Hash *sha256_hash = PURPLE_SHA256_HASH(obj); |
|
| 121 PurpleSHA256HashPrivate *priv = PURPLE_SHA256_HASH_GET_PRIVATE(sha256_hash); |
|
| 122 |
|
| 123 if (priv->checksum) |
|
| 124 g_checksum_free(priv->checksum); |
|
| 125 |
|
| 126 parent_class->finalize(obj); |
|
| 127 } |
|
| 128 |
|
| 129 static void |
|
| 130 purple_sha256_hash_class_init(PurpleSHA256HashClass *klass) { |
|
| 131 GObjectClass *obj_class = G_OBJECT_CLASS(klass); |
|
| 132 PurpleHashClass *hash_class = PURPLE_HASH_CLASS(klass); |
|
| 133 |
|
| 134 parent_class = g_type_class_peek_parent(klass); |
|
| 135 |
|
| 136 obj_class->finalize = purple_sha256_hash_finalize; |
|
| 137 |
|
| 138 hash_class->reset = purple_sha256_hash_reset; |
|
| 139 hash_class->reset_state = purple_sha256_hash_reset; |
|
| 140 hash_class->append = purple_sha256_hash_append; |
|
| 141 hash_class->digest = purple_sha256_hash_digest; |
|
| 142 hash_class->get_digest_size = purple_sha256_hash_get_digest_size; |
|
| 143 hash_class->get_block_size = purple_sha256_hash_get_block_size; |
|
| 144 |
|
| 145 g_type_class_add_private(klass, sizeof(PurpleSHA256HashPrivate)); |
|
| 146 } |
|
| 147 |
|
| 148 static void |
|
| 149 purple_sha256_hash_init(PurpleHash *hash) |
|
| 150 { |
|
| 151 PurpleSHA256Hash *sha256_hash = PURPLE_SHA256_HASH(hash); |
|
| 152 PurpleSHA256HashPrivate *priv = PURPLE_SHA256_HASH_GET_PRIVATE(sha256_hash); |
|
| 153 |
|
| 154 priv->checksum = g_checksum_new(G_CHECKSUM_SHA256); |
|
| 155 |
|
| 156 purple_sha256_hash_reset(hash); |
|
| 157 } |
|
| 158 |
|
| 159 /****************************************************************************** |
|
| 160 * API |
|
| 161 *****************************************************************************/ |
|
| 162 GType |
|
| 163 purple_sha256_hash_get_type(void) { |
|
| 164 static GType type = 0; |
|
| 165 |
|
| 166 if(type == 0) { |
|
| 167 static const GTypeInfo info = { |
|
| 168 sizeof(PurpleSHA256HashClass), |
|
| 169 NULL, |
|
| 170 NULL, |
|
| 171 (GClassInitFunc)purple_sha256_hash_class_init, |
|
| 172 NULL, |
|
| 173 NULL, |
|
| 174 sizeof(PurpleSHA256Hash), |
|
| 175 0, |
|
| 176 (GInstanceInitFunc)purple_sha256_hash_init, |
|
| 177 NULL, |
|
| 178 }; |
|
| 179 |
|
| 180 type = g_type_register_static(PURPLE_TYPE_HASH, |
|
| 181 "PurpleSHA256Hash", |
|
| 182 &info, 0); |
|
| 183 } |
|
| 184 |
|
| 185 return type; |
|
| 186 } |
|
| 187 |
|
| 188 PurpleHash * |
|
| 189 purple_sha256_hash_new(void) { |
|
| 190 return g_object_new(PURPLE_TYPE_SHA256_HASH, NULL); |
|
| 191 } |
|