本文整理汇总了C++中bytesConstRef::size方法的典型用法代码示例。如果您正苦于以下问题:C++ bytesConstRef::size方法的具体用法?C++ bytesConstRef::size怎么用?C++ bytesConstRef::size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bytesConstRef
的用法示例。
在下文中一共展示了bytesConstRef::size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: hexPrefixEncode
std::string hexPrefixEncode(bytesConstRef _d1, unsigned _o1, bytesConstRef _d2, unsigned _o2, bool _leaf)
{
unsigned begin1 = _o1;
unsigned end1 = _d1.size() * 2;
unsigned begin2 = _o2;
unsigned end2 = _d2.size() * 2;
bool odd = (end1 - begin1 + end2 - begin2) & 1;
std::string ret(1, ((_leaf ? 2 : 0) | (odd ? 1 : 0)) * 16);
ret.reserve((end1 - begin1 + end2 - begin2) / 2 + 1);
unsigned d = odd ? 1 : 2;
for (auto i = begin1; i < end1; ++i, ++d)
{
byte n = nibble(_d1, i);
if (d & 1) // odd
ret.back() |= n; // or the nibble onto the back
else
ret.push_back(n << 4); // push the nibble on to the back << 4
}
for (auto i = begin2; i < end2; ++i, ++d)
{
byte n = nibble(_d2, i);
if (d & 1) // odd
ret.back() |= n; // or the nibble onto the back
else
ret.push_back(n << 4); // push the nibble on to the back << 4
}
return ret;
}
示例2: checkPacket
bool Session::checkPacket(bytesConstRef _msg)
{
if (_msg[0] > 0x7f || _msg.size() < 2)
return false;
if (RLP(_msg.cropped(1)).actualSize() + 1 != _msg.size())
return false;
return true;
}
示例3: hashedBytes
unique_ptr<DiscoveryDatagram> DiscoveryDatagram::interpretUDP(bi::udp::endpoint const& _from, bytesConstRef _packet)
{
unique_ptr<DiscoveryDatagram> decoded;
// h256 + Signature + type + RLP (smallest possible packet is empty neighbours packet which is 3 bytes)
if (_packet.size() < h256::size + Signature::size + 1 + 3)
{
LOG(g_discoveryWarnLogger::get()) << "Invalid packet (too small) from "
<< _from.address().to_string() << ":" << _from.port();
return decoded;
}
bytesConstRef hashedBytes(_packet.cropped(h256::size, _packet.size() - h256::size));
bytesConstRef signedBytes(hashedBytes.cropped(Signature::size, hashedBytes.size() - Signature::size));
bytesConstRef signatureBytes(_packet.cropped(h256::size, Signature::size));
bytesConstRef bodyBytes(_packet.cropped(h256::size + Signature::size + 1));
h256 echo(sha3(hashedBytes));
if (!_packet.cropped(0, h256::size).contentsEqual(echo.asBytes()))
{
LOG(g_discoveryWarnLogger::get()) << "Invalid packet (bad hash) from "
<< _from.address().to_string() << ":" << _from.port();
return decoded;
}
Public sourceid(dev::recover(*(Signature const*)signatureBytes.data(), sha3(signedBytes)));
if (!sourceid)
{
LOG(g_discoveryWarnLogger::get()) << "Invalid packet (bad signature) from "
<< _from.address().to_string() << ":" << _from.port();
return decoded;
}
switch (signedBytes[0])
{
case PingNode::type:
decoded.reset(new PingNode(_from, sourceid, echo));
break;
case Pong::type:
decoded.reset(new Pong(_from, sourceid, echo));
break;
case FindNode::type:
decoded.reset(new FindNode(_from, sourceid, echo));
break;
case Neighbours::type:
decoded.reset(new Neighbours(_from, sourceid, echo));
break;
default:
LOG(g_discoveryWarnLogger::get()) << "Invalid packet (unknown packet type) from "
<< _from.address().to_string() << ":" << _from.port();
return decoded;
}
decoded->interpretRLP(bodyBytes);
return decoded;
}
示例4: mKey
void Secp256k1PP::encryptECIES(Public const& _k, bytesConstRef _sharedMacData, bytes& io_cipher)
{
// interop w/go ecies implementation
auto r = KeyPair::create();
Secret z;
ecdh::agree(r.sec(), _k, z);
auto key = eciesKDF(z, bytes(), 32);
bytesConstRef eKey = bytesConstRef(&key).cropped(0, 16);
bytesRef mKeyMaterial = bytesRef(&key).cropped(16, 16);
CryptoPP::SHA256 ctx;
ctx.Update(mKeyMaterial.data(), mKeyMaterial.size());
bytes mKey(32);
ctx.Final(mKey.data());
bytes cipherText = encryptSymNoAuth(SecureFixedHash<16>(eKey), h128(), bytesConstRef(&io_cipher));
if (cipherText.empty())
return;
bytes msg(1 + Public::size + h128::size + cipherText.size() + 32);
msg[0] = 0x04;
r.pub().ref().copyTo(bytesRef(&msg).cropped(1, Public::size));
bytesRef msgCipherRef = bytesRef(&msg).cropped(1 + Public::size + h128::size, cipherText.size());
bytesConstRef(&cipherText).copyTo(msgCipherRef);
// tag message
CryptoPP::HMAC<SHA256> hmacctx(mKey.data(), mKey.size());
bytesConstRef cipherWithIV = bytesRef(&msg).cropped(1 + Public::size, h128::size + cipherText.size());
hmacctx.Update(cipherWithIV.data(), cipherWithIV.size());
hmacctx.Update(_sharedMacData.data(), _sharedMacData.size());
hmacctx.Final(msg.data() + 1 + Public::size + cipherWithIV.size());
io_cipher.resize(msg.size());
io_cipher.swap(msg);
}
示例5:
void eth::sha3(bytesConstRef _input, bytesRef _output)
{
CryptoPP::SHA3_256 ctx;
ctx.Update((byte*)_input.data(), _input.size());
assert(_output.size() >= 32);
ctx.Final(_output.data());
}
示例6: writeFile
void dev::writeFile(std::string const& _file, bytesConstRef _data, bool _writeDeleteRename)
{
namespace fs = boost::filesystem;
if (_writeDeleteRename)
{
fs::path tempPath = fs::unique_path(_file + "-%%%%%%");
writeFile(tempPath.string(), _data, false);
// will delete _file if it exists
fs::rename(tempPath, _file);
}
else
{
// create directory if not existent
fs::path p(_file);
if (!fs::exists(p.parent_path()))
{
fs::create_directories(p.parent_path());
DEV_IGNORE_EXCEPTIONS(fs::permissions(p.parent_path(), fs::owner_all));
}
ofstream s(_file, ios::trunc | ios::binary);
s.write(reinterpret_cast<char const*>(_data.data()), _data.size());
if (!s)
BOOST_THROW_EXCEPTION(FileError() << errinfo_comment("Could not write to file: " + _file));
DEV_IGNORE_EXCEPTIONS(fs::permissions(_file, fs::owner_read|fs::owner_write));
}
}
示例7: aesDecrypt
bytes dev::aesDecrypt(bytesConstRef _ivCipher, std::string const& _password, unsigned _rounds, bytesConstRef _salt)
{
bytes pw = asBytes(_password);
if (!_salt.size())
_salt = &pw;
bytes target(64);
CryptoPP::PKCS5_PBKDF2_HMAC<CryptoPP::SHA256>().DeriveKey(target.data(), target.size(), 0, pw.data(), pw.size(), _salt.data(), _salt.size(), _rounds);
try
{
CryptoPP::AES::Decryption aesDecryption(target.data(), 16);
auto cipher = _ivCipher.cropped(16);
auto iv = _ivCipher.cropped(0, 16);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv.data());
std::string decrypted;
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decrypted));
stfDecryptor.Put(cipher.data(), cipher.size());
stfDecryptor.MessageEnd();
return asBytes(decrypted);
}
catch (exception const& e)
{
cerr << e.what() << endl;
return bytes();
}
}
示例8:
RLP::RLP(bytesConstRef _d, Strictness _s):
m_data(_d)
{
if ((_s & FailIfTooBig) && actualSize() < _d.size())
{
if (_s & ThrowOnFail)
BOOST_THROW_EXCEPTION(OversizeRLP());
else
m_data.reset();
}
if ((_s & FailIfTooSmall) && actualSize() > _d.size())
{
if (_s & ThrowOnFail)
BOOST_THROW_EXCEPTION(UndersizeRLP());
else
m_data.reset();
}
}
示例9: randomWord
static bytes randomWord(bytesConstRef _alphabet, unsigned _min, unsigned _diff, h256* _seed)
{
assert(_min + _diff < 33);
*_seed = sha3(*_seed);
unsigned l = _min + (*_seed)[31] % (_diff + 1);
bytes ret;
for (unsigned i = 0; i < l; ++i)
ret.push_back(_alphabet[(*_seed)[i] % _alphabet.size()]);
return ret;
}
示例10: mKey
bool Secp256k1PP::decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, bytes& io_text)
{
// interop w/go ecies implementation
// io_cipher[0] must be 2, 3, or 4, else invalidpublickey
if (io_text.empty() || io_text[0] < 2 || io_text[0] > 4)
// invalid message: publickey
return false;
if (io_text.size() < (1 + Public::size + h128::size + 1 + h256::size))
// invalid message: length
return false;
Secret z;
if (!ecdh::agree(_k, *(Public*)(io_text.data() + 1), z))
return false; // Invalid pubkey or seckey.
auto key = ecies::kdf(z, bytes(), 64);
bytesConstRef eKey = bytesConstRef(&key).cropped(0, 16);
bytesRef mKeyMaterial = bytesRef(&key).cropped(16, 16);
bytes mKey(32);
CryptoPP::SHA256 ctx;
ctx.Update(mKeyMaterial.data(), mKeyMaterial.size());
ctx.Final(mKey.data());
bytes plain;
size_t cipherLen = io_text.size() - 1 - Public::size - h128::size - h256::size;
bytesConstRef cipherWithIV(io_text.data() + 1 + Public::size, h128::size + cipherLen);
bytesConstRef cipherIV = cipherWithIV.cropped(0, h128::size);
bytesConstRef cipherNoIV = cipherWithIV.cropped(h128::size, cipherLen);
bytesConstRef msgMac(cipherNoIV.data() + cipherLen, h256::size);
h128 iv(cipherIV.toBytes());
// verify tag
CryptoPP::HMAC<CryptoPP::SHA256> hmacctx(mKey.data(), mKey.size());
hmacctx.Update(cipherWithIV.data(), cipherWithIV.size());
hmacctx.Update(_sharedMacData.data(), _sharedMacData.size());
h256 mac;
hmacctx.Final(mac.data());
for (unsigned i = 0; i < h256::size; i++)
if (mac[i] != msgMac[i])
return false;
plain = decryptSymNoAuth(SecureFixedHash<16>(eKey), iv, cipherNoIV).makeInsecure();
io_text.resize(plain.size());
io_text.swap(plain);
return true;
}
示例11: appendFile
//追加内容到文件
void dev::appendFile(std::string const& _file, bytesConstRef _data)
{
namespace fs = boost::filesystem;
// create directory if not existent
fs::path p(_file);
if (!fs::exists(p.parent_path()))
{
fs::create_directories(p.parent_path());
DEV_IGNORE_EXCEPTIONS(fs::permissions(p.parent_path(), fs::owner_all));
}
ofstream s(_file, ios::app | ios::binary);
s.write(reinterpret_cast<char const*>(_data.data()), _data.size());
if (!s)
BOOST_THROW_EXCEPTION(FileError() << errinfo_comment("Could not append write to file: " + _file));
DEV_IGNORE_EXCEPTIONS(fs::permissions(_file, fs::owner_read|fs::owner_write));
}
示例12:
string dev::toBase58(bytesConstRef _d, string const& _alphabet)
{
auto begin = _d.data();
auto end = _d.data() + _d.size();
// Skip & count leading zeroes.
int zeroes = 0;
for (; begin != end && !*begin; begin++, zeroes++) {}
// Allocate enough space in big-endian base58 representation.
// log(256) / log(58), rounded up.
std::vector<unsigned char> b58((end - begin) * 138 / 100 + 1);
// Process the bytes.
while (begin != end)
{
int carry = *begin;
// Apply "b58 = b58 * 256 + ch".
for (auto it = b58.rbegin(); it != b58.rend(); it++)
{
carry += 256 * (*it);
*it = carry % 58;
carry /= 58;
}
assert(!carry);
begin++;
}
// Skip leading zeroes in base58 result.
auto it = b58.begin();
while (it != b58.end() && !*it)
it++;
// Translate the result into a string.
std::string ret;
ret.reserve(zeroes + (b58.end() - it));
ret.assign(zeroes, '1');
while (it != b58.end())
ret += _alphabet[*(it++)];
return ret;
}
示例13: bytesSec
bytesSec dev::decryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _cipher)
{
if (_k.size() != 16 && _k.size() != 24 && _k.size() != 32)
return bytesSec();
SecByteBlock key(_k.data(), _k.size());
try
{
CTR_Mode<AES>::Decryption d;
d.SetKeyWithIV(key, key.size(), _iv.data());
bytesSec ret(_cipher.size());
d.ProcessData(ret.writable().data(), _cipher.data(), _cipher.size());
return ret;
}
catch (CryptoPP::Exception& _e)
{
cerr << _e.what() << endl;
return bytesSec();
}
}
示例14: bytes
bytes dev::encryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _plain)
{
if (_k.size() != 16 && _k.size() != 24 && _k.size() != 32)
return bytes();
SecByteBlock key(_k.data(), _k.size());
try
{
CTR_Mode<AES>::Encryption e;
e.SetKeyWithIV(key, key.size(), _iv.data());
bytes ret(_plain.size());
e.ProcessData(ret.data(), _plain.data(), _plain.size());
return ret;
}
catch (CryptoPP::Exception& _e)
{
cerr << _e.what() << endl;
return bytes();
}
}
示例15: contains
bool contains(bytesConstRef _key) const { return _key.size() >= m_ext.size() && !memcmp(_key.data(), m_ext.data(), m_ext.size()); }