2018-05-30 15:19:13 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
* libretroshare/src/util: rsprint.cc *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
|
|
|
* Copyright 2008-2008 Robert Fernie <retroshare@lunamutt.com> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program 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 Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2008-01-25 01:45:12 -05:00
|
|
|
|
|
|
|
#include "util/rsprint.h"
|
2012-04-15 10:37:44 -04:00
|
|
|
#include "util/rsstring.h"
|
2008-01-25 01:45:12 -05:00
|
|
|
#include <iomanip>
|
2015-12-25 22:37:06 -05:00
|
|
|
#include <sstream>
|
2008-01-25 01:45:12 -05:00
|
|
|
#include <openssl/sha.h>
|
2012-04-15 10:37:44 -04:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS
|
2018-10-06 19:34:05 -04:00
|
|
|
#include "util/rstime.h"
|
2012-04-15 10:37:44 -04:00
|
|
|
#include <sys/timeb.h>
|
|
|
|
#endif
|
2008-01-25 01:45:12 -05:00
|
|
|
|
2016-12-21 18:14:16 -05:00
|
|
|
std::string RsUtil::NumberToString(uint64_t n,bool hex)
|
2015-12-25 22:37:06 -05:00
|
|
|
{
|
|
|
|
std::ostringstream os ;
|
2016-12-21 18:14:16 -05:00
|
|
|
|
|
|
|
if(hex)
|
|
|
|
os << std::hex ;
|
|
|
|
|
2015-12-25 22:37:06 -05:00
|
|
|
os << n ;
|
|
|
|
os.flush() ;
|
|
|
|
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2012-04-15 10:37:44 -04:00
|
|
|
std::string RsUtil::BinToHex(const std::string &bin)
|
2008-01-25 01:45:12 -05:00
|
|
|
{
|
|
|
|
return BinToHex(bin.c_str(), bin.length());
|
|
|
|
}
|
|
|
|
|
2017-07-15 16:15:51 -04:00
|
|
|
std::string RsUtil::BinToHex(const unsigned char *arr, const uint32_t len,uint32_t max_len)
|
2015-11-22 11:36:14 -05:00
|
|
|
{
|
2017-07-15 16:15:51 -04:00
|
|
|
if(max_len > 0)
|
|
|
|
return BinToHex((char*)arr,std::min(max_len,len)) + ((len > max_len)?"...":"") ;
|
|
|
|
else
|
|
|
|
return BinToHex((char*)arr,len) ;
|
2015-11-22 11:36:14 -05:00
|
|
|
}
|
2008-01-25 01:45:12 -05:00
|
|
|
std::string RsUtil::BinToHex(const char *arr, const uint32_t len)
|
|
|
|
{
|
2012-04-15 10:37:44 -04:00
|
|
|
std::string out;
|
2008-01-25 01:45:12 -05:00
|
|
|
|
2012-04-15 10:37:44 -04:00
|
|
|
for(uint32_t j = 0; j < len; j++)
|
|
|
|
{
|
|
|
|
rs_sprintf_append(out, "%02x", (int32_t) (((const uint8_t *) arr)[j]));
|
|
|
|
}
|
2008-01-25 01:45:12 -05:00
|
|
|
|
2012-04-15 10:37:44 -04:00
|
|
|
return out;
|
|
|
|
}
|
2008-01-25 01:45:12 -05:00
|
|
|
|
2012-04-15 10:37:44 -04:00
|
|
|
std::string RsUtil::HashId(const std::string &id, bool reverse)
|
2008-01-25 01:45:12 -05:00
|
|
|
{
|
|
|
|
std::string hash;
|
|
|
|
std::string tohash;
|
|
|
|
if (reverse)
|
|
|
|
{
|
2012-04-15 10:37:44 -04:00
|
|
|
std::string::const_reverse_iterator rit;
|
2014-10-24 18:07:26 -04:00
|
|
|
for(rit = id.rbegin(); rit != id.rend(); ++rit)
|
2008-01-25 01:45:12 -05:00
|
|
|
{
|
|
|
|
tohash += (*rit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tohash = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
SHA_CTX *sha_ctx = new SHA_CTX;
|
|
|
|
uint8_t sha_hash[SHA_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
SHA1_Init(sha_ctx);
|
|
|
|
SHA1_Update(sha_ctx, tohash.c_str(), tohash.length());
|
|
|
|
SHA1_Final(sha_hash, sha_ctx);
|
|
|
|
|
|
|
|
for(uint16_t i = 0; i < SHA_DIGEST_LENGTH; i++)
|
|
|
|
{
|
|
|
|
hash += sha_hash[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
delete sha_ctx;
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
2010-11-11 15:51:17 -05:00
|
|
|
|
2018-10-05 10:54:36 -04:00
|
|
|
static int toHalfByte(char u,bool& ok)
|
|
|
|
{
|
|
|
|
if(u >= 'a' && u <= 'f') return u-'a' + 0xa;
|
|
|
|
if(u >= 'A' && u <= 'F') return u-'A' + 0xa;
|
|
|
|
if(u >= '0' && u <= '9') return u-'0' + 0x0;
|
|
|
|
|
|
|
|
ok = false ;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RsUtil::HexToBin(const std::string& input,unsigned char *data, const uint32_t len)
|
|
|
|
{
|
|
|
|
if(input.size() & 1)
|
|
|
|
return false ;
|
|
|
|
|
|
|
|
if(len != input.size()/2)
|
|
|
|
return false ;
|
|
|
|
|
|
|
|
bool ok = true ;
|
|
|
|
|
|
|
|
for(uint32_t i=0;(i<len) && ok;++i)
|
|
|
|
data[i] = (toHalfByte(input[2*i],ok) << 4) + (toHalfByte(input[2*i+1],ok));
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2012-04-15 10:37:44 -04:00
|
|
|
//static double getCurrentTS()
|
|
|
|
//{
|
|
|
|
//#ifndef WINDOWS_SYS
|
|
|
|
// struct timeval cts_tmp;
|
|
|
|
// gettimeofday(&cts_tmp, NULL);
|
|
|
|
// double cts = (cts_tmp.tv_sec) + ((double) cts_tmp.tv_usec) / 1000000.0;
|
|
|
|
//#else
|
|
|
|
// struct _timeb timebuf;
|
|
|
|
// _ftime( &timebuf);
|
|
|
|
// double cts = (timebuf.time) + ((double) timebuf.millitm) / 1000.0;
|
|
|
|
//#endif
|
|
|
|
// return cts;
|
|
|
|
//}
|
2010-11-11 15:51:17 -05:00
|
|
|
|
|
|
|
// Little fn to get current timestamp in an independent manner.
|
2012-04-15 10:37:44 -04:00
|
|
|
//std::string RsUtil::AccurateTimeString()
|
|
|
|
//{
|
|
|
|
// std::ostringstream out; // please do not use std::stringstream
|
|
|
|
// out << std::setprecision(15) << getCurrentTS();
|
|
|
|
// return out.str();
|
|
|
|
//}
|
2016-10-09 08:32:52 -04:00
|
|
|
|
|
|
|
std::vector<uint8_t> RsUtil::BinToSha256(const std::vector<uint8_t> &in)
|
|
|
|
{
|
|
|
|
std::vector<uint8_t> out;
|
|
|
|
|
|
|
|
SHA256_CTX *sha_ctx = new SHA256_CTX;
|
|
|
|
uint8_t sha_hash[SHA256_DIGEST_LENGTH] = {0};
|
|
|
|
|
|
|
|
SHA256_Init(sha_ctx);
|
|
|
|
SHA256_Update(sha_ctx, in.data(), in.size());
|
|
|
|
SHA256_Final(sha_hash, sha_ctx);
|
|
|
|
|
|
|
|
for(uint16_t i = 0; i < SHA256_DIGEST_LENGTH; i++)
|
|
|
|
{
|
|
|
|
out.push_back(sha_hash[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
delete sha_ctx;
|
|
|
|
return out;
|
|
|
|
}
|