Ejemplo n.º 1
0
void HPRC4LikeCipher::Init(const uint8 *key, uint32 size)
{
    // align key to 32 bits
    std::vector<uint32> keycopy((size + sizeof(uint32) - 1) / sizeof(uint32));
    DEBUG(ASSERT(keycopy.size() * sizeof(uint32) >= size));
    // the last 3 bytes may be incomplete, null those before copying
    keycopy[keycopy.size() - 1] = 0;
    memcpy(&keycopy[0], key, size);

#if IS_BIG_ENDIAN
    for(uint32 i = 0; i < keycopy.size(); ++i)
        ToLittleEndian(keycopy[i]);
#endif

    MTRand mt;
    mt.seed((uint32*)&keycopy[0], keycopy.size());

    for(uint32 i = 0; i < 256; ++i)
        _sbox[i] = i | (mt.randInt() << 8); // lowest bit is always the exchange index, like in original RC4

    uint32 a = 0, b = 0;

    for(uint32 i = 0; i < std::max<uint32>(256, size); ++i)
    {
        b += key[a] + _sbox[i & 0xFF];
        iswap(_sbox[i & 0xFF], _sbox[b & 0xFF]);
        ++a;
        a %= size;
    }
}
bool check_scrabble_found(const std::string& searchkey, 
						  const std::string& match, int jokers)
{
	const char* mc = match.data();
	int len = match.size();
	std::string keycopy(searchkey);
	while(len--) {
		size_t pos = keycopy.find(*mc);
		if (pos == std::string::npos) {
			if (--jokers < 0) {
				std::cout << "SCRABBLE(" << jokers << "): " << match << " not found with " << searchkey << "\n";
				return false;
			}
		}
		else 
			keycopy.erase(pos, 1);
		++mc;
	}
	return true;
}