Replace std::vector<unsigned char> with Blob

This commit is contained in:
Vinnie Falco
2013-06-07 15:14:10 -07:00
parent a6fdabc68a
commit b51edb58fc
72 changed files with 352 additions and 334 deletions

View File

@@ -58,7 +58,7 @@ std::string Base58::encode (const unsigned char* pbegin, const unsigned char* pe
// Convert big endian data to little endian
// Extra zero at the end make sure bignum will interpret as a positive number
std::vector<unsigned char> vchTmp(pend-pbegin+1, 0);
Blob vchTmp(pend-pbegin+1, 0);
std::reverse_copy(pbegin, pend, vchTmp.begin());
// Convert little endian data to bignum
@@ -89,21 +89,21 @@ std::string Base58::encode (const unsigned char* pbegin, const unsigned char* pe
return str;
}
std::string Base58::encode (const std::vector<unsigned char>& vch)
std::string Base58::encode (Blob const& vch)
{
return encode (&vch[0], &vch[0] + vch.size());
}
std::string Base58::encodeWithCheck (const std::vector<unsigned char>& vchIn)
std::string Base58::encodeWithCheck (Blob const& vchIn)
{
// add 4-byte hash check to the end
std::vector<unsigned char> vch(vchIn);
Blob vch(vchIn);
uint256 hash = SHA256Hash(vch.begin(), vch.end());
vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
return encode (vch);
}
bool Base58::decode (const char* psz, std::vector<unsigned char>& vchRet, const char* pAlpha)
bool Base58::decode (const char* psz, Blob & vchRet, const char* pAlpha)
{
assert (pAlpha != 0);
@@ -134,7 +134,7 @@ bool Base58::decode (const char* psz, std::vector<unsigned char>& vchRet, const
}
// Get bignum as little endian data
std::vector<unsigned char> vchTmp = bn.getvch();
Blob vchTmp = bn.getvch();
// Trim off sign byte if present
if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80)
@@ -151,12 +151,12 @@ bool Base58::decode (const char* psz, std::vector<unsigned char>& vchRet, const
return true;
}
bool Base58::decode (const std::string& str, std::vector<unsigned char>& vchRet)
bool Base58::decode (const std::string& str, Blob & vchRet)
{
return decode (str.c_str(), vchRet);
}
bool Base58::decodeWithCheck (const char* psz, std::vector<unsigned char>& vchRet, const char* pAlphabet)
bool Base58::decodeWithCheck (const char* psz, Blob & vchRet, const char* pAlphabet)
{
assert (pAlphabet != NULL);
@@ -177,7 +177,7 @@ bool Base58::decodeWithCheck (const char* psz, std::vector<unsigned char>& vchRe
return true;
}
bool Base58::decodeWithCheck (const std::string& str, std::vector<unsigned char>& vchRet, const char* pAlphabet)
bool Base58::decodeWithCheck (const std::string& str, Blob & vchRet, const char* pAlphabet)
{
return decodeWithCheck (str.c_str(), vchRet, pAlphabet);
}

View File

@@ -45,13 +45,13 @@ public:
static char const* getTestnetAlphabet ();
static std::string encode (const unsigned char* pbegin, const unsigned char* pend);
static std::string encode (const std::vector<unsigned char>& vch);
static std::string encodeWithCheck (const std::vector<unsigned char>& vchIn);
static std::string encode (Blob const& vch);
static std::string encodeWithCheck (Blob const& vchIn);
static bool decode (const char* psz, std::vector<unsigned char>& vchRet, const char* pAlphabet=getCurrentAlphabet ());
static bool decode (const std::string& str, std::vector<unsigned char>& vchRet);
static bool decodeWithCheck (const char* psz, std::vector<unsigned char>& vchRet, const char* pAlphabet=getCurrentAlphabet ());
static bool decodeWithCheck (const std::string& str, std::vector<unsigned char>& vchRet, const char* pAlphabet);
static bool decode (const char* psz, Blob & vchRet, const char* pAlphabet=getCurrentAlphabet ());
static bool decode (const std::string& str, Blob & vchRet);
static bool decodeWithCheck (const char* psz, Blob & vchRet, const char* pAlphabet=getCurrentAlphabet ());
static bool decodeWithCheck (const std::string& str, Blob & vchRet, const char* pAlphabet);
private:
static char const* s_currentAlphabet;

View File

@@ -40,7 +40,7 @@ CBase58Data::~CBase58Data()
memset(&vchData[0], 0, vchData.size());
}
void CBase58Data::SetData(int nVersionIn, const std::vector<unsigned char>& vchDataIn)
void CBase58Data::SetData(int nVersionIn, Blob const& vchDataIn)
{
nVersion = nVersionIn;
vchData = vchDataIn;
@@ -61,7 +61,7 @@ void CBase58Data::SetData(int nVersionIn, const unsigned char *pbegin, const uns
bool CBase58Data::SetString(const char* psz, unsigned char version, const char* pAlphabet)
{
std::vector<unsigned char> vchTemp;
Blob vchTemp;
Base58::decodeWithCheck (psz, vchTemp, pAlphabet);
if (vchTemp.empty() || vchTemp[0] != version)
{
@@ -84,7 +84,7 @@ bool CBase58Data::SetString(const std::string& str, unsigned char version)
std::string CBase58Data::ToString() const
{
std::vector<unsigned char> vch(1, nVersion);
Blob vch(1, nVersion);
vch.insert(vch.end(), vchData.begin(), vchData.end());

View File

@@ -35,12 +35,12 @@ class CBase58Data
{
protected:
unsigned char nVersion;
std::vector<unsigned char> vchData;
Blob vchData;
CBase58Data();
~CBase58Data();
void SetData(int nVersionIn, const std::vector<unsigned char>& vchDataIn);
void SetData(int nVersionIn, Blob const& vchDataIn);
void SetData(int nVersionIn, const void* pdata, size_t nSize);
void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend);

View File

@@ -41,7 +41,7 @@ CBigNum::CBigNum(unsigned int n) { BN_init(this); setulong(n); }
CBigNum::CBigNum(uint64 n) { BN_init(this); setuint64(n); }
CBigNum::CBigNum(uint256 n) { BN_init(this); setuint256(n); }
CBigNum::CBigNum(const std::vector<unsigned char>& vch)
CBigNum::CBigNum(Blob const& vch)
{
BN_init(this);
setvch(vch);
@@ -154,9 +154,9 @@ uint256 CBigNum::getuint256()
return ret;
}
void CBigNum::setvch(const std::vector<unsigned char>& vch)
void CBigNum::setvch(Blob const& vch)
{
std::vector<unsigned char> vch2(vch.size() + 4);
Blob vch2(vch.size() + 4);
unsigned int nSize = vch.size();
// BIGNUM's byte stream format expects 4 bytes of
// big endian size data info at the front
@@ -169,12 +169,12 @@ void CBigNum::setvch(const std::vector<unsigned char>& vch)
BN_mpi2bn(&vch2[0], vch2.size(), this);
}
std::vector<unsigned char> CBigNum::getvch() const
Blob CBigNum::getvch() const
{
unsigned int nSize = BN_bn2mpi(this, NULL);
if (nSize < 4)
return std::vector<unsigned char>();
std::vector<unsigned char> vch(nSize);
return Blob ();
Blob vch(nSize);
BN_bn2mpi(this, &vch[0]);
vch.erase(vch.begin(), vch.begin() + 4);
reverse(vch.begin(), vch.end());
@@ -184,7 +184,7 @@ std::vector<unsigned char> CBigNum::getvch() const
CBigNum& CBigNum::SetCompact(unsigned int nCompact)
{
unsigned int nSize = nCompact >> 24;
std::vector<unsigned char> vch(4 + nSize);
Blob vch(4 + nSize);
vch[3] = nSize;
if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
@@ -196,7 +196,7 @@ CBigNum& CBigNum::SetCompact(unsigned int nCompact)
unsigned int CBigNum::GetCompact() const
{
unsigned int nSize = BN_bn2mpi(this, NULL);
std::vector<unsigned char> vch(nSize);
Blob vch(nSize);
nSize -= 4;
BN_bn2mpi(this, &vch[0]);
unsigned int nCompact = nSize << 24;

View File

@@ -70,7 +70,7 @@ public:
CBigNum(unsigned int n);
CBigNum(uint64 n);
explicit CBigNum(uint256 n);
explicit CBigNum(const std::vector<unsigned char>& vch);
explicit CBigNum(Blob const& vch);
~CBigNum();
void setuint(unsigned int n);
@@ -81,8 +81,8 @@ public:
void setuint64(uint64 n);
void setuint256(const uint256& n);
uint256 getuint256();
void setvch(const std::vector<unsigned char>& vch);
std::vector<unsigned char> getvch() const;
void setvch(Blob const& vch);
Blob getvch() const;
CBigNum& SetCompact(unsigned int nCompact);
unsigned int GetCompact() const;
void SetHex(const std::string& str);

View File

@@ -224,7 +224,7 @@ public:
return true;
}
bool SetPubKey(const std::vector<unsigned char>& vchPubKey)
bool SetPubKey(Blob const& vchPubKey)
{
return SetPubKey(&vchPubKey[0], vchPubKey.size());
}
@@ -234,13 +234,13 @@ public:
return SetPubKey(pubKey.data(), pubKey.size());
}
std::vector<unsigned char> GetPubKey() const
Blob GetPubKey() const
{
unsigned int nSize = i2o_ECPublicKey(pkey, NULL);
assert(nSize<=33);
if (!nSize)
throw key_error("CKey::GetPubKey() : i2o_ECPublicKey failed");
std::vector<unsigned char> vchPubKey(33, 0);
Blob vchPubKey(33, 0);
unsigned char* pbegin = &vchPubKey[0];
if (i2o_ECPublicKey(pkey, &pbegin) != nSize)
throw key_error("CKey::GetPubKey() : i2o_ECPublicKey returned unexpected size");
@@ -248,7 +248,7 @@ public:
return vchPubKey;
}
bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig)
bool Sign(const uint256& hash, Blob & vchSig)
{
unsigned char pchSig[10000];
unsigned int nSize = 0;
@@ -272,7 +272,7 @@ public:
return true;
}
bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const
bool Verify(const uint256& hash, Blob const& vchSig) const
{
return Verify(hash, &vchSig[0], vchSig.size());
}
@@ -289,8 +289,8 @@ public:
// encrypt/decrypt functions with integrity checking.
// Note that the other side must somehow know what keys to use
std::vector<unsigned char> encryptECIES(CKey& otherKey, const std::vector<unsigned char>& plaintext);
std::vector<unsigned char> decryptECIES(CKey& otherKey, const std::vector<unsigned char>& ciphertext);
Blob encryptECIES(CKey& otherKey, Blob const& plaintext);
Blob decryptECIES(CKey& otherKey, Blob const& ciphertext);
};
#endif

View File

@@ -71,7 +71,7 @@ void CKey::getECIESSecret(CKey& otherKey, ECIES_ENC_KEY_TYPE& enc_key, ECIES_HMA
memset(hbuf, 0, ECIES_KEY_LENGTH);
}
static ECIES_HMAC_TYPE makeHMAC(const ECIES_HMAC_KEY_TYPE& secret, const std::vector<unsigned char>& data)
static ECIES_HMAC_TYPE makeHMAC(const ECIES_HMAC_KEY_TYPE& secret, Blob const& data)
{
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
@@ -101,7 +101,7 @@ static ECIES_HMAC_TYPE makeHMAC(const ECIES_HMAC_KEY_TYPE& secret, const std::ve
return ret;
}
std::vector<unsigned char> CKey::encryptECIES(CKey& otherKey, const std::vector<unsigned char>& plaintext)
Blob CKey::encryptECIES(CKey& otherKey, Blob const& plaintext)
{
ECIES_ENC_IV_TYPE iv;
@@ -125,7 +125,7 @@ std::vector<unsigned char> CKey::encryptECIES(CKey& otherKey, const std::vector<
}
secret.zero();
std::vector<unsigned char> out(plaintext.size() + ECIES_HMAC_SIZE + ECIES_ENC_KEY_SIZE + ECIES_ENC_BLK_SIZE, 0);
Blob out(plaintext.size() + ECIES_HMAC_SIZE + ECIES_ENC_KEY_SIZE + ECIES_ENC_BLK_SIZE, 0);
int len = 0, bytesWritten;
// output IV
@@ -169,7 +169,7 @@ std::vector<unsigned char> CKey::encryptECIES(CKey& otherKey, const std::vector<
return out;
}
std::vector<unsigned char> CKey::decryptECIES(CKey& otherKey, const std::vector<unsigned char>& ciphertext)
Blob CKey::decryptECIES(CKey& otherKey, Blob const& ciphertext)
{
// minimum ciphertext = IV + HMAC + 1 block
if (ciphertext.size() < ((2 * ECIES_ENC_BLK_SIZE) + ECIES_HMAC_SIZE) )
@@ -208,7 +208,7 @@ std::vector<unsigned char> CKey::decryptECIES(CKey& otherKey, const std::vector<
}
// decrypt plaintext (after IV and encrypted mac)
std::vector<unsigned char> plaintext(ciphertext.size() - ECIES_HMAC_SIZE - ECIES_ENC_BLK_SIZE);
Blob plaintext(ciphertext.size() - ECIES_HMAC_SIZE - ECIES_ENC_BLK_SIZE);
outlen = plaintext.size();
if (EVP_DecryptUpdate(&ctx, &(plaintext.front()), &outlen,
&(ciphertext.front()) + ECIES_ENC_BLK_SIZE + ECIES_HMAC_SIZE + 1,
@@ -265,17 +265,17 @@ bool checkECIES(void)
}
// generate message
std::vector<unsigned char> message(4096);
Blob message(4096);
int msglen = i%3000;
RandomNumbers::getInstance ().fillBytes (&message.front(), msglen);
message.resize(msglen);
// encrypt message with sender's private key and recipient's public key
std::vector<unsigned char> ciphertext = senderPriv.encryptECIES(recipientPub, message);
Blob ciphertext = senderPriv.encryptECIES(recipientPub, message);
// decrypt message with recipient's private key and sender's public key
std::vector<unsigned char> decrypt = recipientPriv.decryptECIES(senderPub, ciphertext);
Blob decrypt = recipientPriv.decryptECIES(senderPub, ciphertext);
if (decrypt != message)
{