2012-03-15 19:55:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* "$Id: dirtest.cc,v 1.1 2007-02-19 20:08:30 rmf24 Exp $"
|
|
|
|
*
|
|
|
|
* RetroShare C++ Interface.
|
|
|
|
*
|
|
|
|
* Copyright 2012-2012 by Cyril Soler
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "util/rsdir.h"
|
2012-12-19 23:05:38 +00:00
|
|
|
#include "../common/argstream.h"
|
2012-03-15 19:55:43 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
void printHelp(int argc,char *argv[])
|
|
|
|
{
|
|
|
|
std::cerr << argv[0] << ": test RS sha1sum class implementation." << std::endl;
|
|
|
|
std::cerr << "Usage: " << argv[0] << " [test file]" << std::endl ;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc,char *argv[])
|
|
|
|
{
|
2012-12-19 23:05:38 +00:00
|
|
|
std::string inputfile ;
|
|
|
|
argstream as(argc,argv) ;
|
|
|
|
|
|
|
|
as >> parameter('i',"input",inputfile,"input file name to hash",false)
|
|
|
|
>> help() ;
|
2012-03-15 19:55:43 +00:00
|
|
|
|
2012-12-19 23:05:38 +00:00
|
|
|
as.defaultErrorHandling() ;
|
|
|
|
|
|
|
|
if(inputfile.empty())
|
|
|
|
inputfile = argv[0] ;
|
|
|
|
|
|
|
|
FILE *f = RsDirUtil::rs_fopen(inputfile.c_str(),"r") ;
|
2012-03-15 19:55:43 +00:00
|
|
|
|
|
|
|
if(f == NULL)
|
|
|
|
{
|
2012-12-19 23:05:38 +00:00
|
|
|
std::cerr << "Cannot open file " << inputfile << " for read !" << std::endl;
|
2012-03-15 19:55:43 +00:00
|
|
|
return -1 ;
|
|
|
|
}
|
|
|
|
|
2012-12-19 23:05:38 +00:00
|
|
|
std::cerr << "Testing sha1" << std::endl;
|
2012-03-15 19:55:43 +00:00
|
|
|
uint32_t SIZE = 1024*1024 ;
|
|
|
|
unsigned char *buf = new unsigned char[SIZE] ;
|
|
|
|
int len = fread(buf,1,SIZE,f) ;
|
|
|
|
|
2012-12-19 23:05:38 +00:00
|
|
|
std::cerr << "Read " << len << " bytes" << std::endl;
|
2012-03-15 19:55:43 +00:00
|
|
|
|
|
|
|
Sha1CheckSum sum = RsDirUtil::sha1sum(buf,len) ;
|
2012-12-19 23:05:38 +00:00
|
|
|
{
|
|
|
|
std::cerr << std::hex << sum.fourbytes[0] << std::endl;
|
|
|
|
std::cerr << "New method : " << sum.toStdString() << std::endl;
|
|
|
|
}
|
2012-03-15 19:55:43 +00:00
|
|
|
|
|
|
|
std::string hash ;
|
|
|
|
uint64_t size ;
|
2012-12-19 23:05:38 +00:00
|
|
|
RsDirUtil::getFileHash(inputfile.c_str(),hash,size) ;
|
2012-03-15 19:55:43 +00:00
|
|
|
|
2012-12-19 23:05:38 +00:00
|
|
|
std::cerr << "Old method : " << hash << std::endl;
|
|
|
|
if(hash != sum.toStdString())
|
|
|
|
return -1 ;
|
2012-03-15 19:55:43 +00:00
|
|
|
|
|
|
|
Sha1CheckSum H(hash) ;
|
2012-12-19 23:05:38 +00:00
|
|
|
std::cerr << "Hashed transformed: " << H.toStdString() << std::endl;
|
|
|
|
if(hash != H.toStdString())
|
|
|
|
return -1 ;
|
2012-03-15 19:55:43 +00:00
|
|
|
|
2012-12-19 23:05:38 +00:00
|
|
|
std::cerr << "Computing all chunk hashes:" << std::endl;
|
2012-03-17 14:08:27 +00:00
|
|
|
|
|
|
|
fseek(f,0,SEEK_SET) ;
|
|
|
|
int n=0 ;
|
|
|
|
|
|
|
|
while(len = fread(buf,1,SIZE,f))
|
|
|
|
{
|
|
|
|
Sha1CheckSum sum = RsDirUtil::sha1sum(buf,len) ;
|
2012-12-19 23:05:38 +00:00
|
|
|
std::cerr << "Chunk " << n << ": " << sum.toStdString() << std::endl;
|
2012-03-17 14:08:27 +00:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f) ;
|
2012-03-15 19:55:43 +00:00
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|