예제 #1
0
파일: sha256.c 프로젝트: 0-wiz-0/RHash
/**
 * Calculate message hash.
 * Can be called repeatedly with chunks of the message to be hashed.
 *
 * @param ctx the algorithm context containing current hashing state
 * @param msg message chunk
 * @param size length of the message chunk
 */
void rhash_sha256_update(sha256_ctx *ctx, const unsigned char *msg, size_t size)
{
	size_t index = (size_t)ctx->length & 63;
	ctx->length += size;

	/* fill partial block */
	if(index) {
		size_t left = sha256_block_size - index;
		memcpy((char*)ctx->message + index, msg, (size < left ? size : left));
		if(size < left) return;

		/* process partial block */
		rhash_sha256_process_block(ctx->hash, (unsigned*)ctx->message);
		msg  += left;
		size -= left;
	}
	while(size >= sha256_block_size) {
		unsigned* aligned_message_block;
		if(IS_ALIGNED_32(msg)) {
			/* the most common case is processing of an already aligned message
			without copying it */
			aligned_message_block = (unsigned*)msg;
		} else {
			memcpy(ctx->message, msg, sha256_block_size);
			aligned_message_block = (unsigned*)ctx->message;
		}

		rhash_sha256_process_block(ctx->hash, aligned_message_block);
		msg  += sha256_block_size;
		size -= sha256_block_size;
	}
	if(size) {
		memcpy(ctx->message, msg, size); /* save leftovers */
	}
}
예제 #2
0
파일: md5.c 프로젝트: 0-wiz-0/RHash
/**
 * Calculate message hash.
 * Can be called repeatedly with chunks of the message to be hashed.
 *
 * @param ctx the algorithm context containing current hashing state
 * @param msg message chunk
 * @param size length of the message chunk
 */
void rhash_md5_update(md5_ctx *ctx, const unsigned char* msg, size_t size)
{
	unsigned index = (unsigned)ctx->length & 63;
	ctx->length += size;

	/* fill partial block */
	if(index) {
		unsigned left = md5_block_size - index;
		le32_copy((char*)ctx->message, index, msg, (size < left ? size : left));
		if(size < left) return;

		/* process partial block */
		rhash_md5_process_block(ctx->hash, ctx->message);
		msg  += left;
		size -= left;
	}
	while(size >= md5_block_size) {
		unsigned* aligned_message_block;
		if(IS_LITTLE_ENDIAN && IS_ALIGNED_32(msg)) {
			/* the most common case is processing a 32-bit aligned message
			on a little-endian CPU without copying it */
			aligned_message_block = (unsigned*)msg;
		} else {
			le32_copy(ctx->message, 0, msg, md5_block_size);
			aligned_message_block = ctx->message;
		}

		rhash_md5_process_block(ctx->hash, aligned_message_block);
		msg  += md5_block_size;
		size -= md5_block_size;
	}
	if(size) {
		/* save leftovers */
		le32_copy(ctx->message, 0, msg, size);
	}
}
예제 #3
0
/**
 * Calculate message hash.
 * Can be called repeatedly with chunks of the message to be hashed.
 *
 * @param ctx the algorithm context containing current hashing state
 * @param msg message chunk
 * @param size length of the message chunk
 */
void has160_update(has160_ctx *ctx, const unsigned char* msg, size_t size)
{
  unsigned index = (unsigned)ctx->length & 63;
  ctx->length += size;

  /* fill partial block */
  if(index) {
    unsigned left = has160_block_size - index;
    memcpy((char*)ctx->message + index, msg, (size < left ? size : left));
    if(size < left) return;

    /* process partial block */
    has160_process_block(ctx->hash, ctx->message);
    msg  += left;
    size -= left;
  }
  while(size >= has160_block_size) {
    unsigned* aligned_message_block;
    if( IS_ALIGNED_32(msg) ) {
      /* the most common case is processing a 32-bit aligned message 
         without copying it */
      aligned_message_block = (unsigned*)msg;
    } else {
      memcpy(ctx->message, msg, has160_block_size);
      aligned_message_block = ctx->message;
    }

    has160_process_block(ctx->hash, aligned_message_block);
    msg  += has160_block_size;
    size -= has160_block_size;
  }
  if(size) {
    /* save leftovers */
    memcpy(ctx->message, msg, size);
  }
}