mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-08-03 11:54:30 -04:00
added hashstream class to hash content of RsIdentityUsage and fix duplication of identity usage statistics
This commit is contained in:
parent
1a2def70b5
commit
d631758e8c
10 changed files with 152 additions and 112 deletions
79
libretroshare/src/crypto/hashstream.cc
Normal file
79
libretroshare/src/crypto/hashstream.cc
Normal file
|
@ -0,0 +1,79 @@
|
|||
#include "hashstream.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <retroshare/rsids.h>
|
||||
|
||||
namespace librs
|
||||
{
|
||||
namespace crypto
|
||||
{
|
||||
HashStream::HashStream(HashType t)
|
||||
{
|
||||
assert(t == SHA1) ;
|
||||
|
||||
mdctx = EVP_MD_CTX_create();
|
||||
EVP_DigestInit_ex(mdctx,EVP_sha1(),NULL);
|
||||
}
|
||||
HashStream::~HashStream()
|
||||
{
|
||||
if(mdctx)
|
||||
EVP_MD_CTX_destroy(mdctx) ;
|
||||
}
|
||||
|
||||
Sha1CheckSum HashStream::hash()
|
||||
{
|
||||
uint8_t h[EVP_MAX_MD_SIZE] ;
|
||||
unsigned int len;
|
||||
EVP_DigestFinal_ex(mdctx,h,&len) ;
|
||||
|
||||
EVP_MD_CTX_destroy(mdctx) ;
|
||||
mdctx=NULL ;
|
||||
|
||||
return Sha1CheckSum(h);
|
||||
}
|
||||
|
||||
template<>
|
||||
HashStream& operator<<(HashStream& u,const std::string& s)
|
||||
{
|
||||
EVP_DigestUpdate(u.mdctx,s.c_str(),s.length()) ;
|
||||
return u;
|
||||
}
|
||||
template<>
|
||||
HashStream& operator<<(HashStream& u,const uint64_t& n)
|
||||
{
|
||||
unsigned char mem[8] ;
|
||||
uint64_t s(n);
|
||||
for(int i=0;i<8;++i)
|
||||
{
|
||||
mem[i] = (uint8_t)s;
|
||||
s <<= 8 ;
|
||||
}
|
||||
|
||||
EVP_DigestUpdate(u.mdctx,mem,8);
|
||||
return u;
|
||||
}
|
||||
template<>
|
||||
HashStream& operator<<(HashStream& u,const uint32_t& n)
|
||||
{
|
||||
unsigned char mem[4] ;
|
||||
uint64_t s(n);
|
||||
for(int i=0;i<4;++i)
|
||||
{
|
||||
mem[i] = (uint8_t)s;
|
||||
s <<= 8 ;
|
||||
}
|
||||
|
||||
EVP_DigestUpdate(u.mdctx,mem,4);
|
||||
return u;
|
||||
}
|
||||
template<>
|
||||
HashStream& operator<<(HashStream& u,const uint8_t& n)
|
||||
{
|
||||
EVP_DigestUpdate(u.mdctx,&n,1);
|
||||
return u;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
34
libretroshare/src/crypto/hashstream.h
Normal file
34
libretroshare/src/crypto/hashstream.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
#pragma once
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <util/rsdir.h>
|
||||
|
||||
namespace librs
|
||||
{
|
||||
namespace crypto
|
||||
{
|
||||
class HashStream
|
||||
{
|
||||
public:
|
||||
enum HashType { UNKNOWN = 0x00,
|
||||
SHA1 = 0x01
|
||||
};
|
||||
|
||||
HashStream(HashType t);
|
||||
~HashStream();
|
||||
|
||||
Sha1CheckSum hash() ;
|
||||
|
||||
template<class T> friend HashStream& operator<<(HashStream& u, const T&) ;
|
||||
|
||||
template<uint32_t ID_SIZE_IN_BYTES,bool UPPER_CASE,uint32_t UNIQUE_IDENTIFIER>
|
||||
friend HashStream& operator<<(HashStream& u,const t_RsGenericIdType<ID_SIZE_IN_BYTES,UPPER_CASE,UNIQUE_IDENTIFIER>& r)
|
||||
{
|
||||
EVP_DigestUpdate(u.mdctx,r.toByteArray(),ID_SIZE_IN_BYTES);
|
||||
return u;
|
||||
}
|
||||
private:
|
||||
EVP_MD_CTX *mdctx ;
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue