bool check_bcrypt(const std::string& pass, const std::string& hash) { if(hash.size() != 60 || hash[0] != '$' || hash[1] != '2' || hash[2] != 'a' || hash[3] != '$' || hash[6] != '$') { return false; } const uint16_t workfactor = to_uint16(hash.substr(4, 2)); const std::vector<uint8_t> salt = bcrypt_base64_decode(hash.substr(7, 22)); if(salt.size() != 16) return false; const std::string compare = make_bcrypt(pass, salt, workfactor); return same_mem(hash.data(), compare.data(), compare.size()); }
bit mb_processframe() { uint8_t err; uint16_t val; uint16_t dataaddr = mb_getdataaddr(); uint16_t dataval = mb_getdataval(); switch (mb_getfunc()) { case MB_FNCODE_RD_COIL: err = proto_get_rw_byte(dataaddr, dataval, (uint8_t*)&val); goto mb_check; case MB_FNCODE_RD_DISCRETE_INPUT: err = proto_get_ro_byte(dataaddr, dataval, (uint8_t*)&val); goto mb_check; case MB_FNCODE_RD_HOLDING_REGISTER: err = proto_get_rw_word(dataaddr, dataval, &val); goto mb_check; case MB_FNCODE_RD_INPUT_REGISTER: err = proto_get_ro_word(dataaddr, dataval, &val); goto mb_check; case MB_FNCODE_WR_SINGLE_COIL: { switch (dataval) { default: err = MB_EVAL; goto mb_check; case 0x0000: val = false; break; case 0xFF00: val = true; break; } err = proto_set_rw_byte(dataaddr, val); } goto mb_check; case MB_FNCODE_WR_SINGLE_REGISTER: { val = dataval; err = proto_set_rw_word(dataaddr, val); } goto mb_check; default: err = MB_EFUNC; break; } err = MB_EFUNC; mb_check: if (mb_isbroadcast()) { return false; } mb_frame[0] = mb_addr; if (err != MB_EOK) { mb_frame[1] = mb_getfunc() + 0x80; mb_frame[2] = err; mb_framelen = 3; } else { mb_frame[1] = mb_getfunc(); switch (mb_getfunc()) { default: err = MB_EFUNC; goto mb_check; case MB_FNCODE_RD_COIL: case MB_FNCODE_RD_DISCRETE_INPUT: mb_frame[2] = (uint8_t)dataval; mb_frame[3] = (uint8_t)val; mb_framelen = 4; break; case MB_FNCODE_RD_HOLDING_REGISTER: case MB_FNCODE_RD_INPUT_REGISTER: mb_frame[2] = (uint8_t)(2 * dataval); to_uint16(&mb_frame[3]) = htons(val); mb_framelen = 5; break; case MB_FNCODE_WR_SINGLE_COIL: to_uint16(&mb_frame[2]) = htons(dataaddr); to_uint16(&mb_frame[4]) = htons(dataval); mb_framelen = 6; break; case MB_FNCODE_WR_SINGLE_REGISTER: to_uint16(&mb_frame[2]) = htons(dataaddr); to_uint16(&mb_frame[4]) = htons(dataval); mb_framelen = 6; break; } } to_uint16(&mb_frame[mb_framelen]) = crc16(mb_frame, mb_framelen); mb_framelen += 2; return true; }
static inline boost::uint16_t to_uint16(const Byte *buf) { return to_uint16(buf[0], buf[1]); }
static inline boost::uint16_t to_uint16(T1 high, T2 low) { return to_uint16(static_cast<unsigned char>(high), static_cast<unsigned char>(low)); }