IPAddress IPAddress::Parse(RCString ipString) { byte buf[16]; if (CCheck(::inet_pton(AF_INET, ipString, buf))) return IPAddress(ConstBuf(buf, 4)); if (CCheck(::inet_pton(AF_INET6, ipString, buf))) return IPAddress(ConstBuf(buf, 16)); #if UCFG_USE_REGEX # if UCFG_WIN32 if (regex_search(ipString, *s_reDomainName)) { # else if (regex_search(ipString.c_str(), s_reDomainName)) { # endif IPAddress r; r.AddressFamily = Ext::AddressFamily(AF_DOMAIN_NAME); r.m_domainname = ipString; return r; } #endif Throw(HRESULT_FROM_WIN32(DNS_ERROR_INVALID_IP_ADDRESS)); } bool IPAddress::TryParse(RCString s, IPAddress& ip) { byte buf[16]; if (CCheck(::inet_pton(AF_INET, s, buf))) { ip = IPAddress(ConstBuf(buf, 4)); return true; } if (CCheck(::inet_pton(AF_INET6, s, buf))) { ip = IPAddress(ConstBuf(buf, 16)); return true; } return false; }
Blob ConvertFromBase58ShaSquare(RCString s) { BigInteger bi = 0; for (const char *p=s; *p; ++p) { if (const char *q = strchr(s_pszBase58, *p)) { bi = bi*58 + BigInteger(q-s_pszBase58); } else Throw(E_INVALIDARG); } vector<byte> v((bi.Length+7)/8); bi.ToBytes(&v[0], v.size()); if (v.size()>=2 && v.end()[-1]==0 && v.end()[-1]>=0x80) v.resize(v.size()-1); vector<byte> r; for (const char *p=s; *p==s_pszBase58[0]; ++p) r.push_back(0); r.resize(r.size()+v.size()); std::reverse_copy(v.begin(), v.end(), r.end()-v.size()); if (r.size() < 4) Throw(E_FAIL); SHA256 sha; HashValue hash = HashValue(sha.ComputeHash(sha.ComputeHash(ConstBuf(&r[0], r.size()-4)))); if (memcmp(hash.data(), &r.end()[-4], 4)) Throw(HRESULT_FROM_WIN32(ERROR_CRC)); return Blob(&r[0], r.size()-4); }
Blob ConvertFromBase58(RCString s, bool bCheckHash) { BigInteger bi = 0; for (const char *p=s; *p; ++p) { if (const char *q = strchr(s_pszBase58, *p)) { bi = bi*58 + BigInteger(q-s_pszBase58); } else Throw(errc::invalid_argument); } vector<byte> v((bi.Length+7)/8); bi.ToBytes(&v[0], v.size()); if (v.size()>=2 && v.end()[-1]==0 && v.end()[-1]>=0x80) v.resize(v.size()-1); vector<byte> r; for (const char *p=s; *p==s_pszBase58[0]; ++p) r.push_back(0); r.resize(r.size()+v.size()); std::reverse_copy(v.begin(), v.end(), r.end()-v.size()); if (r.size() < 4) Throw(E_FAIL); if (bCheckHash) { HashValue hash = HasherEng::GetCurrent()->HashForAddress(ConstBuf(&r[0], r.size()-4)); if (memcmp(hash.data(), &r.end()[-4], 4)) Throw(HRESULT_FROM_WIN32(ERROR_CRC)); } return Blob(&r[0], r.size()-4); }
hashval CalcPbkdf2Hash(const UInt32 *pIhash, const ConstBuf& data, int idx) { SHA256 sha; size_t size = 16*4+data.Size+4; UInt32 *buf = (UInt32*)alloca(size); memset(buf, 0x36, 16*4); for (int i=0; i<8; ++i) buf[i] ^= pIhash[i]; memcpy(buf+16, data.P, data.Size); buf[16+data.Size/4] = htobe32(idx); hashval hv = sha.ComputeHash(ConstBuf(buf, size)); memset(buf, 0x5C, 16*4); for (int i=0; i<8; ++i) buf[i] ^= pIhash[i]; memcpy(buf+16, hv.constData(), hv.size()); return sha.ComputeHash(ConstBuf(buf, 64+hv.size())); }
void DbTable::Open(DbTransaction& tx, bool bCreate) { CKVStorageKeeper keeper(&tx.Storage); DbCursor cM(tx, DbTable::Main); ConstBuf k(Name.c_str(), strlen(Name.c_str())); if (!cM.SeekToKey(k)) { if (!bCreate) Throw(E_FAIL); TableData td; td.RootPgNo = 0; td.KeySize = KeySize; DbTable::Main.Put(tx, k, ConstBuf(&td, sizeof td)); // cM.SeekToKey(k); //!!!? } }
Blob PBKDF2(PseudoRandomFunction& prf, const ConstBuf& password, const ConstBuf& salt, uint32_t c, size_t dkLen) { size_t hlen = prf.HashSize(); if (dkLen > uint64_t(hlen) * uint32_t(0xFFFFFFFF)) Throw(ExtErr::DerivedKeyTooLong); uint32_t iBe = 0; Blob salt_i = salt + ConstBuf(&iBe, 4); Blob r(nullptr, dkLen); for (uint32_t i=1, n=uint32_t((dkLen + hlen - 1) / hlen); i<=n; ++i) { memcpy(salt_i.data()+salt.Size, &(iBe = htobe(i)), 4); hashval u = prf(password, salt_i), rh = u; for (uint32_t j=1; j<c; ++j) VectorXor(rh.data(), (u = prf(password, u)).data(), hlen); memcpy(r.data() + (i-1)*hlen, rh.data(), (min)(hlen, r.Size - (i-1)*hlen)); } return r; }
size_t IPAddress::GetHashCode() const { size_t r = std::hash<uint16_t>()((uint16_t)get_AddressFamily()); switch ((int)get_AddressFamily()) { case AF_DOMAIN_NAME: r += std::hash<String>()(m_domainname); break; case AF_INET: r += std::hash<uint32_t>()(*(uint32_t*)&m_sin.sin_addr); break; case AF_INET6: r += hash_value(ConstBuf(&m_sin6.sin6_addr, 16)); break; default: Throw(ExtErr::UnknownHostAddressType); } return r; }
CArray8UInt32 CalcSCryptHash(const ConstBuf& password) { ASSERT(password.Size == 80); SHA256 sha; hashval ihash = sha.ComputeHash(password); const UInt32 *pIhash = (UInt32*)ihash.constData(); UInt32 x[32]; for (int i=0; i<4; ++i) { hashval hv = CalcPbkdf2Hash(pIhash, password, i+1); memcpy(x+8*i, hv.constData(), 32); } AlignedMem am((1025*32)*sizeof(UInt32), 128); ScryptCore(x, (UInt32*)am.get()); hashval hv = CalcPbkdf2Hash(pIhash, ConstBuf(x, sizeof x), 1); const UInt32 *p = (UInt32*)hv.constData(); CArray8UInt32 r; for (int i=0; i<8; ++i) r[i] = p[i]; return r; }
static bool fieldAnnouncements(NWK_DataInd_t *ind) { char *data = (char*)ind->data; // be safe if (!ind->options & NWK_IND_OPT_MULTICAST) { return true; } if (hqVerboseOutput) { Serial.print(F("multicast in ")); Serial.println(ind->dstAddr); } if (Scout.isLeadScout()) { leadAnnouncementSend(ind->dstAddr, ind->srcAddr, ConstBuf(data, ind->size-1)); // no null } if (!ind->dstAddr || ind->dstAddr == 0xBEEF || strlen(data) < 3 || data[0] != '[') { return false; } int keys[10]; keyLoad((char*)ind->data, keys, millis()); // run the Bitlash callback function, if defined StringBuffer callback(20); callback.appendSprintf("on.message.group", ind->dstAddr); if (find_user_function(const_cast<char*>(callback.c_str())) || findscript(const_cast<char*>(callback.c_str()))) { StringBuffer buf(64, 16); buf.appendSprintf("on.message.group(%d,%d", ind->dstAddr, ind->srcAddr); for (int i=2; i<=keys[0]; i++) { buf.appendSprintf(",%d", keys[i]); } buf += ")"; doCommand(const_cast<char*>(buf.c_str())); } return true; }
String AFXAPI HostToStr(DWORD host) { char buf[20]; const byte *ar = (const byte*)&host; sprintf(buf, "%d.%d.%d.%d", ar[3], ar[2], ar[1], ar[0]); return buf; } static const in6_addr s_loopback6 = IN6ADDR_LOOPBACK_INIT, s_any6 = IN6ADDR_ANY_INIT, s_none6 = { 0 }; const IPAddress IPAddress::Any(Fast_htonl(INADDR_ANY)), // [Win] don't use standard ntohl to prevent loading WS2_32.dll IPAddress::Broadcast(Fast_htonl(INADDR_BROADCAST)), IPAddress::Loopback(Fast_htonl(INADDR_LOOPBACK)), IPAddress::None(Fast_htonl(INADDR_NONE)), IPAddress::IPv6Any(ConstBuf(&s_any6, 16)), IPAddress::IPv6Loopback(ConstBuf(&s_loopback6, 16)), IPAddress::IPv6None(ConstBuf(&s_none6, 16)); IPAddress::IPAddress() : m_domainname(nullptr) { CreateCommon(); } IPAddress::IPAddress(uint32_t nboIp4) : m_domainname(nullptr) { CreateCommon(); m_sin.sin_addr.s_addr = nboIp4;
HashValue CalcHash(const ConstBuf& cbuf) override { array<UInt32, 8> res = CalcSCryptHash(cbuf); return HashValue(ConstBuf(res.data(), 32)); }
HashValue ScryptHash(const ConstBuf& mb) { CArray8UInt32 ar = CalcSCryptHash(mb); return HashValue(ConstBuf(ar.data(), 32)); }
void SetDestination(MacAddress mac) { SetChunk(0, ConstBuf(&mac.m_n64, 6)); }
HashValue GroestlHash(const ConstBuf& mb) { Groestl512Hash hf; return HashValue(ConstBuf(hf.ComputeHash(hf.ComputeHash(mb)).data(), 32)); }
HashValue NeoSCryptHash(const ConstBuf& mb, int profile) { return HashValue(ConstBuf(CalcNeoSCryptHash(mb, profile).data(), 32)); }
hashval CalcPbkdf2Hash(const uint32_t *pIhash, const ConstBuf& data, int idx) { SHA256 sha; byte *text = (byte*)alloca(data.Size + 4); *(uint32_t*)((byte*)memcpy(text, data.P, data.Size) + data.Size) = htobe32(idx); return HMAC(sha, ConstBuf(pIhash, 32), ConstBuf(text, data.Size + 4)); }
HashValue SHA256_SHA256(const ConstBuf& cbuf) { SHA256 sha; return ConstBuf(sha.ComputeHash(sha.ComputeHash(cbuf))); }
void MineNparNonces(BitcoinMiner& miner, BitcoinWorkData& wd, UInt32 *buf, UInt32 nonce) override { for (int i=0; i<UCFG_BITCOIN_NPAR; i+=3, nonce+=3) { SetNonce(buf, nonce); array<array<UInt32, 8>, 3> res3 = CalcSCryptHash_80_3way(buf); for (int j=0; j<3; ++j) { if (HashValue(ConstBuf(res3[j].data(), 32)) <= wd.HashTarget) { if (!miner.TestAndSubmit(&wd, htobe(nonce+j))) *miner.m_pTraceStream << "Found NONCE not accepted by Target" << endl; } } } }