Example #1
0
  /* Directly sum an entire stream. The stream is read in increments,
     to avoid loading large files into memory. You can specify the
     buffer size as the second parameter, the default is 16 Kb.

     The algorithm does not depend on size() for non-pointer streams.

     Pointer streams are hashed in one go, and do not advance the file
     pointer.

     Since the position of the file pointer after this operation
     depends on the type of stream given, it is considered undefined.
   */
  static Hash sum(Mangle::Stream::Stream &str, int bufsize = DEFSIZE)
  {
    assert(str.isReadable);

    // Is this a memory stream?
    if(str.hasPtr)
      {
        assert(str.hasSize);

        // Pointer streams makes life easy
        return Hash(str.getPtr(), str.size());
      }

    // No pointers today. Create a buffer and hash in increments.
    char *buf = new char[bufsize];

    Hash result;

    // We do not depend on the stream size, only on eof().
    while(!str.eof())
      {
        size_t num = str.read(buf, bufsize);

        // If we read less than expected, we should be at the
        // end of the stream
        assert(num == bufsize || (num < bufsize && str.eof()));

        // Hash the data
        result.update(buf, num);
      }

    // Clean up and return
    delete[] buf;
    return result.finish();
  }
Example #2
0
		void		finish (unsigned char* out, size_t out_len =LENGTH)
		{
			unsigned char	digest[Hash::LENGTH];
			hash.finish(digest);

			unsigned char	k_opad[Hash::BLOCK_LENGTH];
			std::memset(k_opad, 0, Hash::BLOCK_LENGTH);
			std::memcpy(k_opad, key, key_len);
			for (size_t i = 0; i < Hash::BLOCK_LENGTH; ++i) {
				k_opad[i] ^= 0x5c;
			}

			Hash		final_hash;
			final_hash.update(k_opad, Hash::BLOCK_LENGTH);
			final_hash.update(digest, Hash::LENGTH);
			final_hash.finish(out, out_len);

			explicit_memzero(k_opad, Hash::BLOCK_LENGTH);
		}
Example #3
0
  /* Finish and reset the hash summing sequence. The result is
     returned, as well as stored in the .hash member.

     Any read/write operations after finish() restarts the summing
     process, as per update()/finish() in the Hash struct.
   */
  Hash finish()
  {
    return hash.finish();
  }