Opened 10 years ago

Closed 10 years ago

#7128 closed Bugs (fixed)

boost::uuids::detail::sha1 computes incorrect result

Reported by: anonymous Owned by: Andy Tompkins
Milestone: To Be Determined Component: uuid
Version: Boost 1.50.0 Severity: Problem
Keywords: sha1 Cc: jack.wgm@…

Description

I found a problem when hash a large file in linux. my file size is: 1,545,746,148bytes boost :: uuids :: detail :: sha1 very serious problem, hope can fix this problem.

Change History (5)

comment:1 by Andy Tompkins, 10 years ago

Release commit 76405 updated the sh1 algorithm to handle messages as long as the specification (264 bits). See ticket #5325https://svn.boost.org/trac/boost/ticket/5325. It should handle your file.

Can you show your program?

comment:2 by jack.wgm@…, 10 years ago

#include <iostream>
#include <string>
#include <boost/shared_array.hpp>
#include <boost/uuid/sha1.hpp>
#include <boost/smart_ptr/scoped_ptr.hpp>
#include <boost/iostreams/device/file.hpp>

std::string sha1_to_string(const char *hash)
{
	char str[128] = { 0 };
	char *ptr = str;
	std::string ret;

	for (int i = 0; i < 20; i++)
	{
		sprintf(ptr, "%02X", (unsigned char)*hash);
		ptr += 2;
		hash++;
	}
	ret = str;

	return ret;
}

int main(int argc, char **argv)
{
	if (argc != 2)
	{
		return -1;
	}

	boost::uuids::detail::sha1 hasher;
	boost::shared_array<unsigned int> digest;
	boost::shared_array<char> temp_buf(new char[5242880]);

	boost::scoped_ptr<boost::iostreams::file> file_ptr(
		new boost::iostreams::file(argv[1], BOOST_IOS::out|BOOST_IOS::in|BOOST_IOS::binary));
	if (!file_ptr->is_open())
		return -1;
	
	while (true)
	{
		std::streamsize readbytes = file_ptr->read(temp_buf.get(), 5242880);
		if (readbytes <= 0)
			break;
		hasher.process_bytes(temp_buf.get(), readbytes);
	}
	file_ptr.reset();
	
	digest.reset(new unsigned int [5]);
	char bin[20];

	hasher.get_digest(reinterpret_cast<boost::uuids::detail::sha1::digest_type>(*digest.get()));
	for(int i = 0; i < 5; ++i)
	{
		const char* tmp = reinterpret_cast<char*>(digest.get());
		bin[i * 4    ] = tmp[i * 4 + 3];
		bin[i * 4 + 1] = tmp[i * 4 + 2];
		bin[i * 4 + 2] = tmp[i * 4 + 1];
		bin[i * 4 + 3] = tmp[i * 4    ];
	}

	std::string hash_hex = sha1_to_string(bin);

	// output hex digest
	std::cout << hash_hex.c_str() << std::endl;

	return 0;
}

run program:

[root@media-coder test]# g++ test.cpp
[root@coder test]# ./a.out dump.rmvb 
3A383A31E5B447A0614361D7E6931FE29BA9519E
[root@coder test]# sha1sum dump.rmvb 
ad74ded3e3cbea7f02997d51e79e887e596ed787  dump.rmvb

comment:3 by Andy Tompkins, 10 years ago

fixed in trunk - changeset #80850

in reply to:  3 comment:4 by jack.wgm@…, 10 years ago

Replying to atompkins:

fixed in trunk - changeset #80850

Passed the test, thank!

comment:5 by Andy Tompkins, 10 years ago

Resolution: fixed
Status: newclosed

fixed in release - changeset #80954

Note: See TracTickets for help on using tickets.