Browse Source

use GOST R 34.11 in hash serializer

pull/5/head
orignal 8 years ago
parent
commit
ec36620d6a
  1. 2
      i2pd
  2. 17
      src/hash.h

2
i2pd

@ -1 +1 @@
Subproject commit 249bc426673a3d8088a674b5661b84db6bcb16fc Subproject commit d9b79f47c81c72dfe937136eacb9ad8ed73f6e92

17
src/hash.h

@ -12,6 +12,7 @@
#include <openssl/sha.h> #include <openssl/sha.h>
#include <openssl/ripemd.h> #include <openssl/ripemd.h>
#include <vector> #include <vector>
#include <sstream>
template<typename T1> template<typename T1>
inline uint256 Hash(const T1 pbegin, const T1 pend) inline uint256 Hash(const T1 pbegin, const T1 pend)
@ -21,21 +22,21 @@ inline uint256 Hash(const T1 pbegin, const T1 pend)
uint8_t hash1[64]; uint8_t hash1[64];
i2p::crypto::GOSTR3411_2012_512 ((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), hash1); i2p::crypto::GOSTR3411_2012_512 ((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), hash1);
uint256 hash2; uint256 hash2;
i2p::crypto::GOSTR3411_2012_256 ((unsigned char*)&hash1, 61, hash2.begin ()); i2p::crypto::GOSTR3411_2012_256 (hash1, 61, hash2.begin ());
return hash2; return hash2;
} }
class CHashWriter class CHashWriter
{ {
private: private:
SHA256_CTX ctx; std::stringstream ctx;
public: public:
int nType; int nType;
int nVersion; int nVersion;
void Init() { void Init() {
SHA256_Init(&ctx); ctx.str("");
} }
CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) { CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {
@ -43,16 +44,16 @@ public:
} }
CHashWriter& write(const char *pch, size_t size) { CHashWriter& write(const char *pch, size_t size) {
SHA256_Update(&ctx, pch, size); ctx.write (pch, size);
return (*this); return (*this);
} }
// invalidates the object // invalidates the object
uint256 GetHash() { uint256 GetHash() {
uint256 hash1; uint8_t hash1[64];
SHA256_Final((unsigned char*)&hash1, &ctx); i2p::crypto::GOSTR3411_2012_512 ((uint8_t *)ctx.str ().c_str (), ctx.str ().length (), hash1);
uint256 hash2; uint256 hash2;
SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2); i2p::crypto::GOSTR3411_2012_256 (hash1, 64, (unsigned char*)&hash2);
return hash2; return hash2;
} }
@ -112,7 +113,7 @@ inline uint160 Hash160(const T1 pbegin, const T1 pend)
{ {
static unsigned char pblank[1]; static unsigned char pblank[1];
uint256 hash1; uint256 hash1;
SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1); i2p::crypto::GOSTR3411_2012_256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
uint160 hash2; uint160 hash2;
RIPEMD160((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2); RIPEMD160((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
return hash2; return hash2;

Loading…
Cancel
Save