Exemplo n.º 1
0
// http://doc.qt.nokia.com/4.6/datastreamformat.html
// A QString has the length in the first 4 bytes, then the string in UTF-16 encoding
// Has to be stored as big endian!
static gchar* char2qstring(const gchar* in, gsize* size)
{
  glong read, written;
  GError* error = NULL;
  gunichar2* out = g_utf8_to_utf16(in, -1, &read, &written, &error);

  if(error)
  {
    dt_print(DT_DEBUG_PWSTORAGE,"[pwstorage_kwallet] ERROR: error converting string: %s\n", error->message);
    g_error_free(error);
    return NULL;
  }

  glong i;
  for(i=0; i<written; ++i)
  {
    out[i] = g_htons(out[i]);
  }

  guint bytes = sizeof(gunichar2)*written;
  guint BE_bytes = GUINT_TO_BE(bytes);
  *size = sizeof(guint)+bytes;
  gchar* result = g_malloc(*size);

  memcpy(result, &BE_bytes, sizeof(guint));
  memcpy(result+sizeof(guint), out, bytes);

  return result;
}
Exemplo n.º 2
0
void SHA256_Final(unsigned char out[SHA256_DIGEST_LENGTH], GChecksum *ctx)
{
  // Add padding as described in RFC 3174 (it describes SHA-1 but
  // the same padding style is used for SHA-256 too).
  size_t pos = ctx->sha256.size & 0x3F;
  ctx->buffer.u8[pos++] = 0x80;

  while (pos != 64 - 8) {
    if (pos == 64) {
      process(ctx);
      pos = 0;
    }

    ctx->buffer.u8[pos++] = 0x00;
  }

  // Convert the message size from bytes to bits.
  ctx->sha256.size *= 8;

  ctx->buffer.u64[(64 - 8) / 8] = GUINT64_TO_BE(ctx->sha256.size);

  process(ctx);

  for (size_t i = 0; i < 8; ++i)
    ctx->buffer.u32[i] = GUINT_TO_BE(ctx->sha256.state[i]);

  memcpy(out, ctx->buffer.u8, SHA256_DIGEST_LENGTH);
}