static void
append_fingerprint (GcrCertificateDetailsWidget *self, const guchar *data, 
                    gsize n_data, const gchar *name, GChecksumType type)
{
	GChecksum *checksum;
	guint8 *buffer;
	gsize n_buffer;
	gchar *display;
	
	checksum = g_checksum_new (type);
	g_return_if_fail (checksum);
	g_checksum_update (checksum, data, n_data);
	
	n_buffer = g_checksum_type_get_length (type);
	g_return_if_fail (n_buffer);
	buffer = g_malloc0 (n_buffer);
	
	g_checksum_get_digest (checksum, buffer, &n_buffer);
	g_checksum_free (checksum);
	
	display = egg_hex_encode_full (buffer, n_buffer, TRUE, ' ', 1);
	append_field_and_value (self, name, display, TRUE);
	g_free (display);
	
	g_free (buffer);		
}
示例#2
0
文件: ppp_auth.c 项目: AndriusA/ofono
static void chap_process_challenge(struct ppp_chap *chap, const guint8 *packet)
{
	const struct chap_header *header = (const struct chap_header *) packet;
	struct chap_header *response;
	GChecksum *checksum;
	const char *secret = g_at_ppp_get_password(chap->ppp);
	const char *username = g_at_ppp_get_username(chap->ppp);
	guint16 response_length;
	struct ppp_header *ppp_packet;
	gsize digest_len;

	/* create a checksum over id, secret, and challenge */
	checksum = g_checksum_new(chap->method);
	if (checksum == NULL)
		return;

	g_checksum_update(checksum, &header->identifier, 1);

	if (secret)
		g_checksum_update(checksum, (guchar *) secret, strlen(secret));

	g_checksum_update(checksum, &header->data[1], header->data[0]);

	/* transmit a response packet */
	/*
	 * allocate space for the header, the checksum, and the ppp header,
	 * and the value size byte
	 */
	digest_len = g_checksum_type_get_length(chap->method);
	response_length = digest_len + sizeof(*header) + 1;

	if (username != NULL)
		response_length += strlen(username);

	ppp_packet = ppp_packet_new(response_length, CHAP_PROTOCOL);
	if (ppp_packet == NULL)
		goto challenge_out;

	response = (struct chap_header *) &ppp_packet->info;
	if (response) {
		response->code = RESPONSE;
		response->identifier = header->identifier;
		response->length = htons(response_length);
		g_checksum_get_digest(checksum, response->data + 1,
							&digest_len);
		response->data[0] = digest_len;
		/* leave the name empty? */
	}

	if (username != NULL)
		memcpy(response->data + digest_len + 1, username,
				strlen(username));

	/* transmit the packet */
	ppp_transmit(chap->ppp, (guint8 *) ppp_packet, response_length);
	g_free(ppp_packet);

challenge_out:
	g_checksum_free(checksum);
}
示例#3
0
文件: tizen.c 项目: aklein53/neard
static uint8_t *digest_cert(const char *cert, int length)
{
	GChecksum *checksum;
	uint8_t *hash;
	gsize digest_len;

	if (cert == NULL || length < 0)
		return NULL;

	digest_len = g_checksum_type_get_length(G_CHECKSUM_SHA1);
	hash = g_try_malloc(digest_len);
	if (hash == NULL)
		return NULL;

	checksum = g_checksum_new(G_CHECKSUM_SHA1);
	if (checksum == NULL) {
		g_free(hash);
		return NULL;
	}

	g_checksum_update(checksum, cert, length);

	g_checksum_get_digest(checksum, hash, &digest_len);

	DBG("Digest is: ");
	__seel_apdu_dump(hash, digest_len);

	g_checksum_free(checksum);

	return hash;
}
示例#4
0
/**
 * gcr_fingerprint_from_subject_public_key_info:
 * @key_info: (array length=n_key_info): DER encoded subjectPublicKeyInfo structure
 * @n_key_info: length of DER encoded structure
 * @checksum_type: the type of fingerprint to create
 * @n_fingerprint: the length of fingerprint returned
 *
 * Create a key fingerprint for a DER encoded subjectPublicKeyInfo.
 *
 * Returns: (transfer full) (allow-none) (array length=n_fingerprint): the
 *          fingerprint or %NULL if the input was invalid.
 */
guchar *
gcr_fingerprint_from_subject_public_key_info (const guchar *key_info,
                                              gsize n_key_info,
                                              GChecksumType checksum_type,
                                              gsize *n_fingerprint)
{
	GChecksum *check;
	guint8 *fingerprint;

	g_return_val_if_fail (key_info, NULL);
	g_return_val_if_fail (n_key_info, NULL);
	g_return_val_if_fail (n_fingerprint, NULL);

	check = g_checksum_new (checksum_type);
	g_return_val_if_fail (check, NULL);

	g_checksum_update (check, key_info, n_key_info);

	*n_fingerprint = g_checksum_type_get_length (checksum_type);
	fingerprint = g_malloc (*n_fingerprint);
	g_checksum_get_digest (check, fingerprint, n_fingerprint);

	g_checksum_free (check);
	return fingerprint;
}
示例#5
0
sc_bool sc_link_calculate_checksum(const sc_stream *stream, sc_check_sum *check_sum)
{
    sc_char buffer[1024];
    sc_uint32 data_read;
    const gchar *result = 0;
    GChecksum *checksum = g_checksum_new(SC_DEFAULT_CHECKSUM);

    g_assert(stream != 0);
    g_assert(check_sum != 0);
    g_checksum_reset(checksum);

    sc_stream_seek(stream, SC_STREAM_SEEK_SET, 0);

    while (sc_stream_eof(stream) == SC_FALSE)
    {
        if (sc_stream_read_data(stream, buffer, 1024, &data_read) == SC_RESULT_ERROR)
        {
            g_checksum_free(checksum);
            return SC_FALSE;
        }

        g_checksum_update(checksum, (guchar*)buffer, data_read);
    }

    // store results
    check_sum->len = g_checksum_type_get_length(SC_DEFAULT_CHECKSUM);
    result = g_checksum_get_string(checksum);
    memcpy(&(check_sum->data[0]), result, check_sum->len);

    g_checksum_free(checksum);

    sc_stream_seek(stream, SC_STREAM_SEEK_SET, 0);

    return SC_TRUE;
}
示例#6
0
/**
 * CacheUtilHashPassword:
 * @algorithm: Hash algorithm.
 * @salt: Random data, usually stored with the hash.
 * @password: Secret value to hash.
 *
 * Returns: (allow-none): Hash value of concatenated @salt and @password, or
 * #NULL if an argument was invalid.
 */
GBytes *CacheUtilHashPassword(GChecksumType algorithm, GBytes *salt,
                               const char *password) {
  GChecksum *checksum = NULL;
  gssize checksum_len = -1;
  guint8 *result = NULL;
  gsize result_len = 0;

  if (!salt || !password)
    return NULL;

  checksum_len = g_checksum_type_get_length(algorithm);
  if (checksum_len <= 0)
    return NULL;

  checksum = g_checksum_new(algorithm);
  if (!checksum)
    return NULL;

  g_checksum_update(
      checksum, g_bytes_get_data(salt, NULL), g_bytes_get_size(salt));
  g_checksum_update(checksum, (guint8 *) password, strlen(password));

  result = g_malloc(checksum_len);
  result_len = checksum_len;

  g_checksum_get_digest(checksum, result, &result_len);
  g_assert(checksum_len == result_len);

  g_checksum_free(checksum);
  return g_bytes_new_take(result, result_len);
}
示例#7
0
void
_gcr_display_view_append_fingerprint (GcrDisplayView *self, GcrRenderer *renderer, const guchar *data,
                                      gsize n_data, const gchar *name, GChecksumType type)
{
	GChecksum *checksum;
	guint8 *buffer;
	gsize n_buffer;
	gchar *display;

	g_return_if_fail (GCR_IS_DISPLAY_VIEW (self));

	checksum = g_checksum_new (type);
	g_return_if_fail (checksum);
	g_checksum_update (checksum, data, n_data);

	n_buffer = g_checksum_type_get_length (type);
	g_return_if_fail (n_buffer);
	buffer = g_malloc0 (n_buffer);

	g_checksum_get_digest (checksum, buffer, &n_buffer);
	g_checksum_free (checksum);

	display = egg_hex_encode_full (buffer, n_buffer, TRUE, ' ', 1);
	_gcr_display_view_append_value (self, renderer, name, display, TRUE);
	g_free (display);

	g_free (buffer);
}
示例#8
0
gchar *
kms_utils_generate_fingerprint_from_pem (const gchar * pem)
{
  guint i;
  gchar *line;
  guchar *der, *tmp;
  gchar **lines;
  gint state = 0;
  guint save = 0;
  gsize der_length = 0;
  GChecksum *checksum;
  guint8 *digest;
  gsize digest_length;
  GString *fingerprint;
  gchar *ret;

  if (pem == NULL) {
    GST_ERROR ("Pem certificate is null");
    return NULL;
  }
  der = tmp = g_new0 (guchar, (strlen (pem) / 4) * 3 + 3);
  lines = g_strsplit (pem, "\n", 0);

  for (i = 0, line = lines[i]; line; line = lines[++i]) {
    if (line[0] && g_str_has_prefix (line, BEGIN_CERTIFICATE)) {
      i++;
      break;
    }
  }

  for (line = lines[i]; line; line = lines[++i]) {
    if (line[0] && g_str_has_prefix (line, END_CERTIFICATE)) {
      break;
    }
    tmp += g_base64_decode_step (line, strlen (line), tmp, &state, &save);
  }
  der_length = tmp - der;
  checksum = g_checksum_new (G_CHECKSUM_SHA256);
  digest_length = g_checksum_type_get_length (G_CHECKSUM_SHA256);
  digest = g_new (guint8, digest_length);
  g_checksum_update (checksum, der, der_length);
  g_checksum_get_digest (checksum, digest, &digest_length);
  fingerprint = g_string_new (NULL);
  for (i = 0; i < digest_length; i++) {
    if (i)
      g_string_append (fingerprint, ":");
    g_string_append_printf (fingerprint, "%02X", digest[i]);
  }
  ret = g_string_free (fingerprint, FALSE);

  g_free (digest);
  g_checksum_free (checksum);
  g_free (der);
  g_strfreev (lines);

  return ret;
}
示例#9
0
static uint8_t *hash (const void *buffer, size_t size, uint8_t *out, GChecksumType type) {
  GChecksum *s = g_checksum_new(type);
  gsize digest_size = g_checksum_type_get_length(type);

  g_checksum_update(s, buffer, size);
  g_checksum_get_digest(s, out, &digest_size);
  g_checksum_free(s);

  return out;
}
示例#10
0
文件: ghmac.c 项目: GNOME/glib
/**
 * g_hmac_get_digest:
 * @hmac: a #GHmac
 * @buffer: (array length=digest_len): output buffer
 * @digest_len: (inout): an inout parameter. The caller initializes it to the
 *   size of @buffer. After the call it contains the length of the digest
 *
 * Gets the digest from @checksum as a raw binary array and places it
 * into @buffer. The size of the digest depends on the type of checksum.
 *
 * Once this function has been called, the #GHmac is closed and can
 * no longer be updated with g_checksum_update().
 *
 * Since: 2.30
 */
void
g_hmac_get_digest (GHmac  *hmac,
                   guint8 *buffer,
                   gsize  *digest_len)
{
  gsize len;

  g_return_if_fail (hmac != NULL);

  len = g_checksum_type_get_length (hmac->digest_type);
  g_return_if_fail (*digest_len >= len);

  /* Use the same buffer, because we can :) */
  g_checksum_get_digest (hmac->digesti, buffer, &len);
  g_checksum_update (hmac->digesto, buffer, len);
  g_checksum_get_digest (hmac->digesto, buffer, digest_len);
}
示例#11
0
static guint8 *
get_md5_digest (const guchar *str)
{
	guint8 *digest;
	gsize length;
	GChecksum *checksum;

	length = g_checksum_type_get_length (G_CHECKSUM_MD5);
	digest = g_malloc0 (length);

	checksum = g_checksum_new (G_CHECKSUM_MD5);
	g_checksum_update (checksum, str, -1);
	g_checksum_get_digest (checksum, digest, &length);
	g_checksum_free (checksum);

	return digest;
}
示例#12
0
static GBytes *
hash_sha1 (GBytes *data)
{
	GChecksum *checksum;
	guchar *hash;
	gsize n_hash;

	n_hash = g_checksum_type_get_length (G_CHECKSUM_SHA1);
	hash = g_malloc (n_hash);

	checksum = g_checksum_new (G_CHECKSUM_SHA1);
	g_checksum_update (checksum, g_bytes_get_data (data, NULL), g_bytes_get_size (data));
	g_checksum_get_digest (checksum, hash, &n_hash);
	g_checksum_free (checksum);

	return g_bytes_new_take (hash, n_hash);
}
示例#13
0
文件: ghmac.c 项目: GNOME/glib
/**
 * g_hmac_get_string:
 * @hmac: a #GHmac
 *
 * Gets the HMAC as an hexadecimal string.
 *
 * Once this function has been called the #GHmac can no longer be
 * updated with g_hmac_update().
 *
 * The hexadecimal characters will be lower case.
 *
 * Returns: the hexadecimal representation of the HMAC. The
 *   returned string is owned by the HMAC and should not be modified
 *   or freed.
 *
 * Since: 2.30
 */
const gchar *
g_hmac_get_string (GHmac *hmac)
{
  guint8 *buffer;
  gsize digest_len;

  g_return_val_if_fail (hmac != NULL, NULL);

  digest_len = g_checksum_type_get_length (hmac->digest_type);
  buffer = g_alloca (digest_len);

  /* This is only called for its side-effect of updating hmac->digesto... */
  g_hmac_get_digest (hmac, buffer, &digest_len);
  /* ... because we get the string from the checksum rather than
   * stringifying buffer ourselves
   */
  return g_checksum_get_string (hmac->digesto);
}
示例#14
0
static GError *
_checksum_file (const char *path, struct file_info_s *fi)
{
	GError *err = NULL;
	gchar *file_content = NULL;
	g_file_get_contents (path, &file_content, &fi->fs, &err);
	if (err) return err;

	fi->hs = g_checksum_type_get_length (G_CHECKSUM_SHA256);

	GChecksum *checksum = g_checksum_new (G_CHECKSUM_SHA256);
	g_checksum_update (checksum, (guint8*)file_content, fi->fs);
	g_checksum_get_digest (checksum, fi->h, &fi->hs);
	g_checksum_free (checksum);
	g_free (file_content);

	return NULL;
}
示例#15
0
static gchar *
generate_fingerprint_from_pem (const gchar * pem)
{
  guint i;
  gchar *line;
  guchar *der, *tmp;
  gchar **lines;
  gint state = 0;
  guint save = 0;
  gsize der_length = 0;
  GChecksum *checksum;
  guint8 *digest;
  gsize digest_length;
  GString *fingerprint;
  gchar *ret;

  der = tmp = g_new0 (guchar, (strlen (pem) / 4) * 3 + 3);
  lines = g_strsplit (pem, "\n", 0);
  for (i = 0, line = lines[i]; line; line = lines[++i]) {
    if (line[0] && !g_str_has_prefix (line, "-----"))
      tmp += g_base64_decode_step (line, strlen (line), tmp, &state, &save);
  }
  der_length = tmp - der;
  checksum = g_checksum_new (G_CHECKSUM_SHA256);
  digest_length = g_checksum_type_get_length (G_CHECKSUM_SHA256);
  digest = g_new (guint8, digest_length);
  g_checksum_update (checksum, der, der_length);
  g_checksum_get_digest (checksum, digest, &digest_length);
  fingerprint = g_string_new (NULL);
  for (i = 0; i < digest_length; i++) {
    if (i)
      g_string_append (fingerprint, ":");
    g_string_append_printf (fingerprint, "%02X", digest[i]);
  }
  ret = g_string_free (fingerprint, FALSE);

  g_free (digest);
  g_checksum_free (checksum);
  g_free (der);
  g_strfreev (lines);

  return ret;
}
示例#16
0
文件: webrtc.c 项目: saljam/webcam
void got_dtls_certificate(GObject *media_session, GParamSpec *pspec, gpointer user_data)
{
	guint i;
	gchar *pem, *line;
	guchar *der, *tmp;
	gchar **lines;
	gint state = 0;
	guint save = 0;
	gsize der_length = 0;
	GChecksum *checksum;
	guint8 *digest;
	gsize digest_length;
	GString *fingerprint;

	g_object_get(media_session, "dtls-certificate", &pem, NULL);
	der = tmp = g_new0(guchar, (strlen(pem) / 4) * 3 + 3);
	lines = g_strsplit(pem, "\n", 0);
	for (i = 0, line = lines[i]; line; line = lines[++i]) {
		if (line[0] && !g_str_has_prefix(line, "-----"))
			tmp += g_base64_decode_step(line, strlen(line), tmp, &state, &save);
	}
	der_length = tmp - der;
	checksum = g_checksum_new(G_CHECKSUM_SHA256);
	digest_length = g_checksum_type_get_length(G_CHECKSUM_SHA256);
	digest = g_new(guint8, digest_length);
	g_checksum_update(checksum, der, der_length);
	g_checksum_get_digest(checksum, digest, &digest_length);
	fingerprint = g_string_new(NULL);
	for (i = 0; i < digest_length; i++) {
		if (i)
			g_string_append(fingerprint, ":");
		g_string_append_printf(fingerprint, "%02X", digest[i]);
	}
	gchar *fprint = g_string_free(fingerprint, FALSE);
	got_dtls_certificate_go(OWR_SESSION(media_session), fprint);

	g_free(fprint);
	g_free(digest);
	g_checksum_free(checksum);
	g_free(der);
	g_strfreev(lines);
}
static void
multipart_set_boundary (CamelMultipart *multipart,
                        const gchar *boundary)
{
	CamelDataWrapper *cdw = CAMEL_DATA_WRAPPER (multipart);
	gchar *bgen, bbuf[27], *p;
	guint8 *digest;
	gsize length;
	gint state, save;

	g_return_if_fail (cdw->mime_type != NULL);

	length = g_checksum_type_get_length (G_CHECKSUM_MD5);
	digest = g_alloca (length);

	if (!boundary) {
		GChecksum *checksum;

		/* Generate a fairly random boundary string. */
		bgen = g_strdup_printf ("%p:%lu:%lu", (gpointer) multipart,
					(gulong) getpid (),
					(gulong) time (NULL));

		checksum = g_checksum_new (G_CHECKSUM_MD5);
		g_checksum_update (checksum, (guchar *) bgen, -1);
		g_checksum_get_digest (checksum, digest, &length);
		g_checksum_free (checksum);

		g_free (bgen);
		strcpy (bbuf, "=-");
		p = bbuf + 2;
		state = save = 0;
		p += g_base64_encode_step (
			(guchar *) digest, length, FALSE, p, &state, &save);
		*p = '\0';

		boundary = bbuf;
	}

	camel_content_type_set_param (cdw->mime_type, "boundary", boundary);
}
示例#18
0
/**
 * gcr_certificate_get_fingerprint:
 * @self: a #GcrCertificate
 * @type: the type of algorithm for the fingerprint.
 * @n_length: The length of the resulting fingerprint.
 * 
 * Calculate the fingerprint for this certificate.
 * 
 * You can pass G_CHECKSUM_SHA1 or G_CHECKSUM_MD5 as the @type
 * parameter.
 * 
 * The caller should free the returned data using g_free() when
 * it is no longer required.
 * 
 * Returns: the raw binary fingerprint.  
 **/
guchar*
gcr_certificate_get_fingerprint (GcrCertificate *self, GChecksumType type, gsize *n_length)
{
	GChecksum *sum;
	guchar *digest;
	gssize length;
	
	g_return_val_if_fail (GCR_IS_CERTIFICATE (self), NULL);
	g_return_val_if_fail (n_length, NULL);
	
	sum = digest_certificate (self, type);
	g_return_val_if_fail (sum, NULL);
	length = g_checksum_type_get_length (type);
	g_return_val_if_fail (length > 0, NULL);
	digest = g_malloc (length);
	*n_length = length;
	g_checksum_get_digest (sum, digest, n_length);
	g_checksum_free (sum);
	
	return digest;
}
示例#19
0
/**
 * e_dbhash_add:
 * @edbh: an #EDbHash
 * @key: a database key
 * @data: a database object for @key
 *
 * Adds a database object for @key.
 **/
void
e_dbhash_add (EDbHash *edbh,
              const gchar *key,
              const gchar *data)
{
	DB *db;
	DBT dkey;
	DBT ddata;
	GChecksum *checksum;
	guint8 *digest;
	gsize length;

	g_return_if_fail (edbh != NULL);
	g_return_if_fail (edbh->priv != NULL);
	g_return_if_fail (edbh->priv->db != NULL);
	g_return_if_fail (key != NULL);
	g_return_if_fail (data != NULL);

	length = g_checksum_type_get_length (G_CHECKSUM_MD5);
	digest = g_alloca (length);

	db = edbh->priv->db;

	/* Key dbt */
	string_to_dbt (key, &dkey);

	/* Compute MD5 checksum */
	checksum = g_checksum_new (G_CHECKSUM_MD5);
	g_checksum_update (checksum, (guchar *) data, -1);
	g_checksum_get_digest (checksum, digest, &length);
	g_checksum_free (checksum);

	/* Data dbt */
	md5_to_dbt (digest, &ddata);

	/* Add to database */
	db->put (db, NULL, &dkey, &ddata, 0);
}
示例#20
0
/**
 * gcr_certificate_get_fingerprint_hex:
 * @self: a #GcrCertificate
 * @type: the type of algorithm for the fingerprint.
 * 
 * Calculate the fingerprint for this certificate, and return it 
 * as a hex string.
 * 
 * You can pass G_CHECKSUM_SHA1 or G_CHECKSUM_MD5 as the @type
 * parameter.
 * 
 * The caller should free the returned data using g_free() when
 * it is no longer required.
 * 
 * Returns: an allocated hex string which contains the fingerprint.  
 */
gchar*
gcr_certificate_get_fingerprint_hex (GcrCertificate *self, GChecksumType type)
{
	GChecksum *sum;
	guchar *digest;
	gsize n_digest;
	gssize length;
	gchar *hex;
	
	g_return_val_if_fail (GCR_IS_CERTIFICATE (self), NULL);
	
	sum = digest_certificate (self, type);
	g_return_val_if_fail (sum, NULL);
	length = g_checksum_type_get_length (type);
	g_return_val_if_fail (length > 0, NULL);
	digest = g_malloc (length);
	n_digest = length;
	g_checksum_get_digest (sum, digest, &n_digest);
	hex = egg_hex_encode_full (digest, n_digest, TRUE, ' ', 1);
	g_checksum_free (sum);
	g_free (digest);
	return hex;
}
示例#21
0
文件: hmac-glib.c 项目: CTU-IIG/qemu
static int
qcrypto_glib_hmac_bytesv(QCryptoHmac *hmac,
                         const struct iovec *iov,
                         size_t niov,
                         uint8_t **result,
                         size_t *resultlen,
                         Error **errp)
{
    QCryptoHmacGlib *ctx;
    int i, ret;

    ctx = hmac->opaque;

    for (i = 0; i < niov; i++) {
        g_hmac_update(ctx->ghmac, iov[i].iov_base, iov[i].iov_len);
    }

    ret = g_checksum_type_get_length(qcrypto_hmac_alg_map[hmac->alg]);
    if (ret < 0) {
        error_setg(errp, "Unable to get hmac length");
        return -1;
    }

    if (*resultlen == 0) {
        *resultlen = ret;
        *result = g_new0(uint8_t, *resultlen);
    } else if (*resultlen != ret) {
        error_setg(errp, "Result buffer size %zu is smaller than hmac %d",
                   *resultlen, ret);
        return -1;
    }

    g_hmac_get_digest(ctx->ghmac, *result, resultlen);

    return 0;
}
示例#22
0
static GBytes *
hash_sha1_pkcs1 (GBytes *data)
{
	const guchar SHA1_ASN[15] = /* Object ID is 1.3.14.3.2.26 */
		{ 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
		  0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };

	GChecksum *checksum;
	guchar *hash;
	gsize n_hash;
	gsize n_digest;

	n_digest = g_checksum_type_get_length (G_CHECKSUM_SHA1);
	n_hash = n_digest + sizeof (SHA1_ASN);
	hash = g_malloc (n_hash);
	memcpy (hash, SHA1_ASN, sizeof (SHA1_ASN));

	checksum = g_checksum_new (G_CHECKSUM_SHA1);
	g_checksum_update (checksum, g_bytes_get_data (data, NULL), g_bytes_get_size (data));
	g_checksum_get_digest (checksum, hash + sizeof (SHA1_ASN), &n_digest);
	g_checksum_free (checksum);

	return g_bytes_new_take (hash, n_hash);
}
示例#23
0
static gboolean
purple_sha1_hash_digest(PurpleHash *hash, guchar *digest, size_t buff_len)
{
	PurpleSHA1Hash *sha1_hash = PURPLE_SHA1_HASH(hash);
	PurpleSHA1HashPrivate *priv = PURPLE_SHA1_HASH_GET_PRIVATE(sha1_hash);

	const gssize required_len = g_checksum_type_get_length(G_CHECKSUM_SHA1);
	gsize digest_len = buff_len;

	g_return_val_if_fail(priv != NULL, FALSE);
	g_return_val_if_fail(priv->checksum != NULL, FALSE);

	g_return_val_if_fail(required_len >= 0, FALSE);
	g_return_val_if_fail(buff_len >= (gsize)required_len, FALSE);

	g_checksum_get_digest(priv->checksum, digest, &digest_len);

	if (digest_len != (gsize)required_len)
		return FALSE;

	purple_sha1_hash_reset(hash);

	return TRUE;
}
示例#24
0
static void _tgentransfer_readChecksum(TGenTransfer* transfer) {
    TGEN_ASSERT(transfer);

    if(_tgentransfer_getLine(transfer)) {
        /* transfer is done */
        _tgentransfer_changeState(transfer, TGEN_XFER_SUCCESS);
        transfer->time.checksum = g_get_monotonic_time();

        /* we have read the entire checksum from the other end */
        gssize sha1Length = g_checksum_type_get_length(G_CHECKSUM_MD5);
        g_assert(sha1Length >= 0);
        gchar* computedSum = g_strdup(g_checksum_get_string(transfer->payloadChecksum));

        gchar* line = g_string_free(transfer->readBuffer, FALSE);
        transfer->readBuffer = NULL;

        gchar** parts = g_strsplit(line, " ", 0);
        const gchar* receivedSum = parts[1];
        g_assert(receivedSum);

        /* check that the sums match */
        if(!g_ascii_strncasecmp(computedSum, receivedSum, (gsize)sha1Length)) {
            tgen_message("transport %s transfer %s MD5 checksums passed: computed=%s received=%s",
                    tgentransport_toString(transfer->transport), _tgentransfer_toString(transfer),
                    computedSum, receivedSum);
        } else {
            tgen_message("MD5 checksums failed: computed=%s received=%s", computedSum, receivedSum);
        }

        g_strfreev(parts);
        g_free(line);
        g_free(computedSum);
    } else {
        /* unable to receive entire checksum, wait for next chance to read */
    }
}
示例#25
0
/* Fd must be opened for writing, after creating file is mmapped */
static gboolean
create_cache_file (struct symbols_cache *cache,
	const gchar *filename,
	gint fd,
	rspamd_mempool_t *pool)
{
	GChecksum *cksum;
	u_char *digest;
	gsize cklen;
	GList *cur;
	struct cache_item *item;

	/* Calculate checksum */
	cksum = get_mem_cksum (cache);
	if (cksum == NULL) {
		msg_err ("cannot calculate checksum for symbols");
		close (fd);
		return FALSE;
	}

	cklen = g_checksum_type_get_length (G_CHECKSUM_SHA1);
	digest = g_malloc (cklen);

	g_checksum_get_digest (cksum, digest, &cklen);
	/* Now write data to file */
	cur = g_list_first (cache->negative_items);
	while (cur) {
		item = cur->data;
		if (write (fd, item->s, sizeof (struct saved_cache_item)) == -1) {
			msg_err ("cannot write to file %d, %s", errno, strerror (errno));
			close (fd);
			g_checksum_free (cksum);
			g_free (digest);
			return FALSE;
		}
		cur = g_list_next (cur);
	}
	cur = g_list_first (cache->static_items);
	while (cur) {
		item = cur->data;
		if (write (fd, item->s, sizeof (struct saved_cache_item)) == -1) {
			msg_err ("cannot write to file %d, %s", errno, strerror (errno));
			close (fd);
			g_checksum_free (cksum);
			g_free (digest);
			return FALSE;
		}
		cur = g_list_next (cur);
	}
	/* Write checksum */
	if (write (fd, digest, cklen) == -1) {
		msg_err ("cannot write to file %d, %s", errno, strerror (errno));
		close (fd);
		g_checksum_free (cksum);
		g_free (digest);
		return FALSE;
	}

	close (fd);
	g_checksum_free (cksum);
	g_free (digest);
	/* Reopen for reading */
	if ((fd = open (filename, O_RDWR)) == -1) {
		msg_info ("cannot open file %s, error %d, %s", errno, strerror (errno));
		return FALSE;
	}

	return mmap_cache_file (cache, fd, pool);
}
示例#26
0
static size_t
purple_sha1_hash_get_digest_size(PurpleHash *hash)
{
	return g_checksum_type_get_length(G_CHECKSUM_SHA1);
}
示例#27
0
int main(int argc, char *argv[])
{
    sc_uint item = -1;

    fflush(stdout);
    timer = g_timer_new();
    g_timer_start(timer);

    printf("MD5: %d\n", g_checksum_type_get_length(G_CHECKSUM_MD5) );
    printf("SHA1: %d\n", g_checksum_type_get_length(G_CHECKSUM_SHA1) );
    printf("SHA256: %d\n", g_checksum_type_get_length(G_CHECKSUM_SHA256) );

    sc_storage_initialize("repo");
    g_timer_stop(timer);
    printf("Segment loading speed: %f seg/sec\n", sc_storage_get_segments_count() / g_timer_elapsed(timer, 0));

    //test5();
    //test6();

    //test7();

    while (item != 0)
    {
        printf("Commands:\n"
               "0 - exit\n"
               "1 - test allocation\n"
               "2 - test sc-addr utilities\n"
               "3 - test arc deletion\n"
               "4 - test iterators\n"
               "5 - test contents\n"
               "6 - test content finding\n"
               "7 - test events\n"
               "\nCommand: ");
        scanf("%d", &item);

        printf("\n----- Test %d -----\n", item);

        switch(item)
        {
        case 1:
            test1();
            break;

        case 2:
            test2();
            break;

        case 3:
            test3();
            break;

        case 4:
            test4();
            break;

        case 5:
            test5();
            break;

        case 6:
            test6();
            break;

        case 7:
            test7();
            break;
        };

        printf("\n----- Finished -----\n");
    }

    timer = g_timer_new();
    item = sc_storage_get_segments_count();
    g_timer_reset(timer); // crash when uncomment

    sc_storage_shutdown();
    g_timer_stop(timer);
    printf("Segments save speed: %f seg/sec\n", item / g_timer_elapsed(timer, 0));
    g_timer_destroy(timer);

    return 0;
}
示例#28
0
sc_uint8 _checksum_get_size()
{
    return (sc_uint8)g_checksum_type_get_length(_checksum_type());
}
static GByteArray *
cram_md5_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
{
	GChecksum *checksum;
	guint8 *digest;
	gsize length;
	char *passwd;
	const gchar *hex;
	GByteArray *ret = NULL;
	guchar ipad[64];
	guchar opad[64];
	int i, pw_len;

	/* Need to wait for the server */
	if (!token)
		return NULL;

	g_return_val_if_fail (sasl->service->url->passwd != NULL, NULL);

	length = g_checksum_type_get_length (G_CHECKSUM_MD5);
	digest = g_alloca (length);

	memset (ipad, 0, sizeof (ipad));
	memset (opad, 0, sizeof (opad));

	passwd = sasl->service->url->passwd;
	pw_len = strlen (passwd);
	if (pw_len <= 64) {
		memcpy (ipad, passwd, pw_len);
		memcpy (opad, passwd, pw_len);
	} else {
		checksum = g_checksum_new (G_CHECKSUM_MD5);
		g_checksum_update (checksum, (guchar *) passwd, pw_len);
		g_checksum_get_digest (checksum, digest, &length);
		g_checksum_free (checksum);

		memcpy (ipad, digest, length);
		memcpy (opad, digest, length);
	}

	for (i = 0; i < 64; i++) {
		ipad[i] ^= 0x36;
		opad[i] ^= 0x5c;
	}

	checksum = g_checksum_new (G_CHECKSUM_MD5);
	g_checksum_update (checksum, (guchar *) ipad, sizeof (ipad));
	g_checksum_update (checksum, (guchar *) token->data, token->len);
	g_checksum_get_digest (checksum, digest, &length);
	g_checksum_free (checksum);

	checksum = g_checksum_new (G_CHECKSUM_MD5);
	g_checksum_update (checksum, (guchar *) opad, sizeof (opad));
	g_checksum_update (checksum, (guchar *) digest, length);

	/* String is owned by the checksum. */
	hex = g_checksum_get_string (checksum);

	ret = g_byte_array_new ();
	g_byte_array_append (ret, (guint8 *) sasl->service->url->user, strlen (sasl->service->url->user));
	g_byte_array_append (ret, (guint8 *) " ", 1);
	g_byte_array_append (ret, (guint8 *) hex, strlen (hex));

	g_checksum_free (checksum);

	sasl->authenticated = TRUE;

	return ret;
}
static RejillaBurnResult
rejilla_checksum_files_check_files (RejillaChecksumFiles *self,
				    GError **error)
{
	GValue *value;
	guint file_nb;
	guint file_num;
	gint checksum_len;
	RejillaVolSrc *vol;
	goffset start_block;
	RejillaTrack *track;
	const gchar *device;
	RejillaVolFile *file;
	RejillaDrive *drive;
	RejillaMedium *medium;
	GChecksumType gchecksum_type;
	GArray *wrong_checksums = NULL;
	RejillaDeviceHandle *dev_handle;
	RejillaChecksumFilesPrivate *priv;
	RejillaVolFileHandle *handle = NULL;
	RejillaBurnResult result = REJILLA_BURN_OK;

	priv = REJILLA_CHECKSUM_FILES_PRIVATE (self);

	/* get medium */
	rejilla_job_get_current_track (REJILLA_JOB (self), &track);
	drive = rejilla_track_disc_get_drive (REJILLA_TRACK_DISC (track));
	medium = rejilla_drive_get_medium (drive);

	/* open volume */
	if (!rejilla_medium_get_last_data_track_address (medium, NULL, &start_block))
		return REJILLA_BURN_ERR;

	device = rejilla_drive_get_device (rejilla_medium_get_drive (medium));
	dev_handle = rejilla_device_handle_open (device, FALSE, NULL);
	if (!dev_handle)
		return REJILLA_BURN_ERROR;

	vol = rejilla_volume_source_open_device_handle (dev_handle, error);

	/* open checksum file */
	file = rejilla_checksum_files_get_on_disc_checksum_type (self,
								 vol,
								 start_block);
	if (!file) {
		g_set_error (error,
			     REJILLA_BURN_ERROR,
			     REJILLA_BURN_ERROR_GENERAL,
			     _("No checksum file could be found on the disc"));

		REJILLA_JOB_LOG (self, "No checksum file");
		result = REJILLA_BURN_ERR;
		goto end;
	}

	handle = rejilla_volume_file_open (vol, file);
	if (!handle) {
		REJILLA_JOB_LOG (self, "Cannot open checksum file");
		/* FIXME: error here ? */
		result = REJILLA_BURN_ERR;
		goto end;
	}

	/* get the number of files at this time and rewind */
	file_nb = rejilla_checksum_files_get_line_num (self, handle);
	if (file_nb == 0) {
		REJILLA_JOB_LOG (self, "Empty checksum file");
		result = REJILLA_BURN_OK;
		goto end;
	}

	if (file_nb < 0) {
		/* An error here */
		REJILLA_JOB_LOG (self, "Failed to retrieve the number of lines");
		result = REJILLA_BURN_ERR;
		goto end;
	}

	/* signal we're ready to start */
	file_num = 0;
	rejilla_job_set_current_action (REJILLA_JOB (self),
				        REJILLA_BURN_ACTION_CHECKSUM,
					_("Checking file integrity"),
					TRUE);
	rejilla_job_start_progress (REJILLA_JOB (self), FALSE);

	/* Get the checksum type */
	switch (priv->checksum_type) {
	case REJILLA_CHECKSUM_MD5_FILE:
		gchecksum_type = G_CHECKSUM_MD5;
		break;
	case REJILLA_CHECKSUM_SHA1_FILE:
		gchecksum_type = G_CHECKSUM_SHA1;
		break;
	case REJILLA_CHECKSUM_SHA256_FILE:
		gchecksum_type = G_CHECKSUM_SHA256;
		break;
	default:
		gchecksum_type = G_CHECKSUM_MD5;
		break;
	}

	checksum_len = g_checksum_type_get_length (gchecksum_type) * 2;
	while (1) {
		gchar file_path [MAXPATHLEN + 1];
		gchar checksum_file [512 + 1];
		RejillaVolFile *disc_file;
		gchar *checksum_real;
		gint read_bytes;

		if (priv->cancel)
			break;

		/* first read the checksum */
		read_bytes = rejilla_volume_file_read (handle,
						       checksum_file,
						       checksum_len);
		if (read_bytes == 0)
			break;

		if (read_bytes != checksum_len) {
			/* FIXME: an error here */
			REJILLA_JOB_LOG (self, "Impossible to read the checksum from file");
			result = REJILLA_BURN_ERR;
			break;
		}
		checksum_file [checksum_len] = '\0';

		if (priv->cancel)
			break;

		/* skip spaces in between */
		while (1) {
			gchar c [2];

			read_bytes = rejilla_volume_file_read (handle, c, 1);
			if (read_bytes == 0) {
				result = REJILLA_BURN_OK;
				goto end;
			}

			if (read_bytes < 0) {
				/* FIXME: an error here */
				REJILLA_JOB_LOG (self, "Impossible to read checksum file");
				result = REJILLA_BURN_ERR;
				goto end;
			}

			if (!isspace (c [0])) {
				file_path [0] = '/';
				file_path [1] = c [0];
				break;
			}
		}

		/* get the filename */
		result = rejilla_volume_file_read_line (handle, file_path + 2, sizeof (file_path) - 2);

		/* FIXME: an error here */
		if (result == REJILLA_BURN_ERR) {
			REJILLA_JOB_LOG (self, "Impossible to read checksum file");
			break;
		}

		checksum_real = NULL;

		/* get the file handle itself */
		REJILLA_JOB_LOG (self, "Getting file %s", file_path);
		disc_file = rejilla_volume_get_file (vol,
						     file_path,
						     start_block,
						     NULL);
		if (!disc_file) {
			g_set_error (error,
				     REJILLA_BURN_ERROR,
				     REJILLA_BURN_ERROR_GENERAL,
				     _("File \"%s\" could not be opened"),
				     file_path);
			result = REJILLA_BURN_ERR;
			break;
		}

		/* we certainly don't want to checksum anything but regular file
		 * if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
		 *	rejilla_volume_file_free (disc_file);
		 *	continue;
		 * }
		 */

		/* checksum the file */
		result = rejilla_checksum_files_sum_on_disc_file (self,
								  gchecksum_type,
								  vol,
								  disc_file,
								  &checksum_real,
								  error);
		rejilla_volume_file_free (disc_file);
		if (result == REJILLA_BURN_ERR) {
			g_set_error (error,
				     REJILLA_BURN_ERROR,
				     REJILLA_BURN_ERROR_GENERAL,
				     _("File \"%s\" could not be opened"),
				     file_path);
			break;
		}

		if (result != REJILLA_BURN_OK)
			break;

		file_num++;
		rejilla_job_set_progress (REJILLA_JOB (self),
					  (gdouble) file_num /
					  (gdouble) file_nb);
		REJILLA_JOB_LOG (self,
				 "comparing checksums for file %s : %s (from md5 file) / %s (current)",
				 file_path, checksum_file, checksum_real);

		if (strcmp (checksum_file, checksum_real)) {
			gchar *string;

			REJILLA_JOB_LOG (self, "Wrong checksum");
			if (!wrong_checksums)
				wrong_checksums = g_array_new (TRUE,
							       TRUE, 
							       sizeof (gchar *));

			string = g_strdup (file_path);
			wrong_checksums = g_array_append_val (wrong_checksums, string);
		}

		g_free (checksum_real);
		if (priv->cancel)
			break;
	}

end:

	if (handle)
		rejilla_volume_file_close (handle);

	if (file)
		rejilla_volume_file_free (file);

	if (vol)
		rejilla_volume_source_close (vol);

	if (dev_handle)
		rejilla_device_handle_close (dev_handle);

	if (result != REJILLA_BURN_OK) {
		REJILLA_JOB_LOG (self, "Ended with an error");
		if (wrong_checksums) {
			g_strfreev ((gchar **) wrong_checksums->data);
			g_array_free (wrong_checksums, FALSE);
		}
		return result;
	}

	if (!wrong_checksums)
		return REJILLA_BURN_OK;

	/* add the tag */
	value = g_new0 (GValue, 1);
	g_value_init (value, G_TYPE_STRV);
	g_value_take_boxed (value, wrong_checksums->data);
	g_array_free (wrong_checksums, FALSE);

	rejilla_track_tag_add (track,
			       REJILLA_TRACK_MEDIUM_WRONG_CHECKSUM_TAG,
			       value);

	g_set_error (error,
		     REJILLA_BURN_ERROR,
		     REJILLA_BURN_ERROR_BAD_CHECKSUM,
		     _("Some files may be corrupted on the disc"));

	return REJILLA_BURN_ERR;
}