/** * Takes a bundle and finds the custom key binding for the bundle. * Returns the hash of the two key presses for the bundle. */ void Accelerators::ParseBundleForHash(const tmAction* x, int& outChordHash, int& outFinalHash, wxString& outChordString) { outChordHash = -1; outFinalHash = -1; int actionHash = (x->key.modifiers << 24) | x->key.keyCode; // now check if there is a custom shortcut std::map<wxString, wxString>::iterator iterator; iterator = m_customBindings.find(normalize(x->name)); wxString customAccel; if(iterator != m_customBindings.end()) { customAccel = iterator->second.Trim(); } if(customAccel.empty()) { outFinalHash = actionHash; return; } int pos = customAccel.Find(wxT(" ")); if(pos != wxNOT_FOUND) { // Custom shortcut is a chord // First, find or create the chord object wxString chordAccel = customAccel.Left(pos); wxString finalAccel = customAccel.Mid(pos+1); outChordHash = makeHash(chordAccel); outFinalHash = makeHash(finalAccel); outChordString = chordAccel; } else { outFinalHash = makeHash(customAccel); } }
bool SecurityManager::isCredentialsOK(QString entered_user_id, QString entered_password) { qDebug() << "XXXX isCredentialsOK(" << entered_user_id << "," << entered_password << ")"; int result = SB_SUCCESS; DataManager* data_mgr = DataManager::getInstance(); QString current_user_id_hash_as_hex = data_mgr->getUserid(); QString current_password_hash_as_hex = data_mgr->getPassword(); bool password_ready = false; // if DataManager was not able to provide the stored password or a default then it will have returned an empty string if (current_password_hash_as_hex.length() > 0) { password_ready = true; } // if we were unable to either // a) restore password from QSettings or // b) create a default password using the SB API then // we have a system error and it's game over if (!password_ready) { qDebug() << "XXXX SYSTEM ERROR: password not ready"; return false; } // hash the user ID entered by the user and compare it with the user_id hash restored from QSettings unsigned char message_digest_user_id[SB_SHA256_DIGEST_LEN]; QString entered_user_id_hash = ""; result = makeHash(entered_user_id, message_digest_user_id); if (result == SB_SUCCESS) { QByteArray entered_user_id_data = QByteArray::fromRawData(reinterpret_cast<const char *>(message_digest_user_id), SB_SHA256_DIGEST_LEN); QString entered_user_id_hash_as_hex = QString::fromAscii(entered_user_id_data.toHex()); qDebug() << "XXXX entered user_id hash:" << entered_user_id_hash_as_hex; qDebug() << "XXXX current user_id hash:" << current_user_id_hash_as_hex; if (current_user_id_hash_as_hex.compare(entered_user_id_hash_as_hex) != 0) { qDebug() << "XXXX incorrect credentials entered #1"; return false; } } else { qDebug() << "XXXX SYSTEM ERROR: failed to calculate hash of entered user_id:" << result; return false; } // hash the password entered by the user and compare it with the password hash restored from QSettings unsigned char message_digest_password[SB_SHA256_DIGEST_LEN]; QString entered_password_hash = ""; result = makeHash(entered_password, message_digest_password); if (result == SB_SUCCESS) { QByteArray entered_password_data = QByteArray::fromRawData(reinterpret_cast<const char *>(message_digest_password), SB_SHA256_DIGEST_LEN); QString entered_password_hash_as_hex = QString::fromAscii(entered_password_data.toHex()); qDebug() << "XXXX entered password hash:" << entered_password_hash_as_hex; qDebug() << "XXXX current password hash:" << current_password_hash_as_hex; if (current_password_hash_as_hex.compare(entered_password_hash_as_hex) != 0) { qDebug() << "XXXX incorrect credentials entered #2"; return false; } else { return true; } } else { qDebug() << "XXXX SYSTEM ERROR: failed to calculate hash of entered password:" << result; return false; } }
OovString ModelStatement::makeOverloadKeyFromOperUSR(OovStringRef operStr) { OovString sym; #define DEBUG_KEY 0 #if(DEBUG_KEY) sym = operStr; // Remove symbols that conflict with either the CompoundValue class or // ModelStatement name separator characters. sym.replaceStrs("@", "-"); sym.replaceStrs("#", "-"); sym.replaceStrs(":", "-"); sym.appendInt(makeHash(operStr), 16); #else sym.appendInt(makeHash(operStr), 16); #endif return sym; }
int MapGet(Map *map, char *key) { MapEntry *cur; int pos = makeHash(key) % map->cap; for (cur = map->slots[pos]; cur != NULL; cur = cur->next) { if (strcmp(cur->key, key) == 0) return cur->val; } return -1; }
bool Account::createAccount(const std::string& accountName, const int& accountNum, const std::string& pin, const std::string& bankSalt) { if(accountName == "") { return false; } this->accountName = accountName; if(accountNum <= 0) { return false; } this->accountNum = accountNum; this->salt = makeHash(randomString(128)); std::string card = makeHash(to_string(this->accountNum) + salt); this->card = card; std::ofstream outfile; std::string cardFile = "cards/" + this->accountName + ".card"; std::ofstream file_out(cardFile.c_str()); // write values to cards if(file_out.is_open()) { file_out << card; } file_out.close(); //If oin sucessfully set then the account can be unlocked for use. if(setPIN(pin, bankSalt)) { islocked = false; } else { islocked = true; } return true; }
static int updateMapEntry(MapEntry **slots, int cap, MapEntry *me) { MapEntry *cur; int pos = makeHash(me->key) % cap; for (cur = slots[pos]; cur != NULL; cur = cur->next) { if (strcmp(me->key, cur->key) == 0) { cur->val = me->val; return 0; } } me->next = slots[pos]; slots[pos] = me; return 1; }
void Accelerators::InsertBinding(KeyBinding* binding) { wxString& accel = binding->accel; // now parse the accel for a chord int pos = accel.Find(wxT(" ")); if(pos != wxNOT_FOUND) { wxString chordAccel = accel.Left(pos); int chordHash = makeHash(chordAccel); binding->finalKey = accel.Mid(pos+1).Trim(); std::map<int, KeyChord*>::iterator iterator; iterator = m_chords.find(chordHash); KeyChord* chord; if(iterator != m_chords.end()) { chord = iterator->second; } else { chord = new KeyChord(chordAccel); m_chords.insert(pair<int, KeyChord*>(chordHash, chord)); } // Prevent a memory leak if there are duplicate bindings int hash = makeHash(binding->finalKey); std::map<int, KeyBinding*>::iterator bindingIterator = chord->bindings.find(hash); if(bindingIterator != chord->bindings.end()) { delete bindingIterator->second; } chord->bindings[hash] = binding; } else { // Prevent a memory leak if there are duplicate bindings int hash = makeHash(accel); std::map<int, KeyBinding*>::iterator bindingIterator = m_bindings.find(hash); if(bindingIterator != m_bindings.end()) { delete bindingIterator->second; } m_bindings[hash] = binding; } }
void MapRemove(Map *map, char *key) { MapEntry *prev = NULL, *cur; int pos = makeHash(key) % map->cap; for (cur = map->slots[pos]; cur != NULL; cur = cur->next) { if (strcmp(cur->key, key) == 0) { if (prev == NULL) map->slots[pos] = cur->next; else prev->next = cur->next; free(cur); return; } prev = cur; } }
bool Account::tryLogin(const std::string& pin, const std::string& bankSalt) { if(this->islocked || this->inUse) { return false; } std::string attemptedHash = makeHash(this->card + pin + bankSalt); if(this->hash == attemptedHash) { return true; } else { return false; } }
bool Account::setPIN(const std::string& pin, const std::string& bankSalt) { //require pin length of at least 6 but no more than 32 if(pin.length() < 6 || pin.length() > 32) { return false; } std::string hash = makeHash(this->card + pin + bankSalt); //verify that hash was created if(hash.length() > 0) { this->hash = hash; return true; } else { return false; } }
bool BankSession::sendP(long int &csock, void* packet, std::string command) { if(!this->key) { return false; } bankNonce = makeHash(randomString(128)); command = command + "," + atmNonce + "," + bankNonce; if(command.size() >= 460) { return false; } buildPacket((char*)packet, command); if(!encryptPacket((char*)packet,this->key)) { return false; } return sendPacket(csock, packet); }
// For a single word, returns the start of the result Docnode list. // Makes an exact copy of the docnode from index DocNode *getResultsForWord(char *word, INVERTED_INDEX *index) { // check for reserved words first if (!strncmp(word, "AND", 3) || !strncmp(word, "OR", 2)) { printf("AND and OR are reserved words. Please enter a different query.\n"); return NULL; } NormalizeWord(word); int h = makeHash(word); int word_not_found = 1; // 1 for true, 0 for false WordNode *cluster_end = NULL; // collisionAction is like getDatawith key for Dictionary.. cluster_end = collisionAction(index, h, word, &word_not_found); if (word_not_found) return NULL; else { DocNode *d = cluster_end->data; // page from the wordnode DocNode *dcopy = initDocNode(d->doc_id, d->page_word_frequency); // see if more documents exist for (d=d->next; d!=NULL; d=d->next) updateDocNode(dcopy, d->doc_id, d->page_word_frequency); // This also works but commented because ORhelper has to free // the incoming doc list... // since we are making exact copy of the docnode from index, // we need to add the docs that are not already in our copy. // So this is like an OR operation. // if (d->next != NULL) // ORHelper(&dcopy, d->next); return dcopy; } }
HttpMethod HttpMethod::fromString(const QString& methodString) { static QHash<QString, Method> hash = makeHash(); return HttpMethod(hash.value(methodString.toUpper(), INVALID)); }
unsigned int City::getCellSeed(int x_, int z_) { return makeHash(x_^makeHash(z_^_seed)); }
void* client_thread(void* arg) { BankSocketThread* bankSocketThread = (BankSocketThread*) arg; Bank* bank = bankSocketThread->bank; BankSession* bankSession = new BankSession(); bankSession->state = 0; bankSession->bank = bank; bankSession->key = 0; long int csock = (long int)*(bankSocketThread->csock); printf("[bank] client ID #%ld connected\n", csock); //input loop int length; char packet[1024]; bool fatalError = false; std::vector<std::string> tokens; while(1) { fatalError = false; tokens.clear(); if(!listenPacket(csock, packet)) { printf("[bank] fail to read packet\n"); break; } if(!bankSession->key) { if(bankSession->state != 0) { printf("[error] Unexpected state\n"); break; } for(unsigned int i = 0; i < bank->keys.size(); ++i) { if(bank->keysInUse[i]) { continue; } if(decryptPacket((char*)packet,bank->keys[i]) && std::string(packet).substr(0,9) == "handshake") { bankSession->key = bank->keys[i]; bank->keysInUse[i] = true; break; } } if(!bankSession->key) { printf("[error] Key not found.\n"); break; } } else { if(!decryptPacket((char*)packet, bankSession->key)) { printf("[error] Invalid key\n"); break; } } //Parse the packet //std::string strPacket = packet; split(std::string(packet),',', tokens); //We should get something, if not ignore this packet if(tokens.size() < 1) { continue; } if(tokens[0] == "logout") { bankSession->endSession(); break; } //Now we're compare what we go to what state we expect to be in switch(bankSession->state) { case 0: case 1: if(tokens.size() == 2 && tokens[0] == "handshake" && tokens[1].size() == 128) { bankSession->atmNonce = tokens[1]; bankSession->bankNonce = makeHash(randomString(128)); if(bankSession->bankNonce.size() == 0) { printf("Unexpected error\n"); fatalError = true; break; } buildPacket(packet, "handshakeResponse," + bankSession->atmNonce + "," + bankSession->bankNonce); if(!encryptPacket((char*)packet,bankSession->key)) { printf("Unexpected error\n"); fatalError = true; break; } if(!sendPacket(csock, packet)) { printf("Unexpected error\n"); fatalError = true; break; } bankSession->state = 2; } break; //Expecting a login case 2: if(!bankSession->validateNonce(std::string(packet))) { printf("Unexpected error\n"); fatalError = true; break; } if(tokens.size() == 5 && tokens[0] == "login" && tokens[1].size() == 128) { //Now we'll try to find the account //bankSession->account = bank->tryLoginHash(tokens[1]); bankSession->account = bank->getAccountByName(tokens[2]); if(!bankSession->account || !bankSession->account->tryHash(tokens[1])) { //Failed login //TODO Blacklist hash bankSession->error = true; //printf("[notice] Failed login!\n"); } bankSession->account->inUse = true; bankSession->state = 5; if(!bankSession->sendP(csock,packet, "ack")) { printf("Unexpected error!\n"); fatalError = true; break; } } break; case 5: bool returnBalance = false; bankSession->state = 4; if(bankSession->error) { returnBalance = false; } else if(tokens.size() == 3 && tokens[0] == "balance") { returnBalance = true; } else if(tokens.size() == 4 && tokens[0] == "withdraw" && isDouble(tokens[1])) { double amount = atof(tokens[1].c_str()); if(!bankSession->account->Withdraw(amount)) { printf("[error] Failed withdraw\n"); returnBalance = false; bankSession->error = true; } returnBalance = true; } else if(tokens.size() == 5 && tokens[0] == "transfer" && !isDouble(tokens[1]) && isDouble(tokens[2])) { Account* accountTo = bank->getAccountByName(tokens[1]); double amount = atof(tokens[2].c_str()); if(!bankSession->account->Transfer(amount, accountTo)) { printf("[error] Failed transfer\n"); returnBalance = false; bankSession->error = true; } returnBalance = true; } if(bankSession->error) { bankSession->sendP(csock, packet, "denied"); } else if(returnBalance) { char moneyStr[256]; sprintf(moneyStr,"%.2Lf",bankSession->account->getBalance()); bankSession->sendP(csock, packet, std::string(moneyStr)); } //Reset back to initial state bankSession->endSession(); break; } if(fatalError) { bankSession->endSession(); break; } } bankSession->endSession(); printf("[bank] client ID #%ld disconnected\n", csock); close(csock); delete bankSession; return NULL; }
static void insertMapEntry(MapEntry **slots, int cap, MapEntry *me) { int pos = makeHash(me->key) % cap; me->next = slots[pos]; slots[pos] = me; }
void heterozygote (unsigned r, unsigned c, COUNTTYPE * R) { if(tableCount < 0) return; // aborted because of time limit COUNTTYPE *res, *resn; int lower, upper; unsigned ntables; unsigned i, arc, ar1, ar2, a32, a31; COUNTTYPE * Rnew = R + nAlleles; double countsSoFar; unsigned long long hash; // NSNumber * n; res = R-1; // to make res a 1-based version of R resn = Rnew-1; // so resn is 1-based for Rnew lower = res[r]; for (i = 1; i < c; i++) lower -= res[i]; lower = fmax(0, lower); upper = fmin(res[r], res[c]); if(c > 2) for (arc = lower; arc <= upper; arc++) { memcpy(Rnew, R, Rbytes); // Put a fresh set of residuals from R into Rnew // decrement residuals for the current value of arc. resn[r] -= arc; resn[c] -= arc; heterozygote(r, c-1, Rnew); } if(c==2){ if(r > 3) for (ar2= lower; ar2 <= upper; ar2++) { memcpy(Rnew, R, Rbytes); // Put a fresh set of residuals from R into Rnew // decrement residuals for the current value of arc. resn[r] -= ar2; resn[c] -= ar2; // The value of ar1 is now fixed, so no need for any more calls to heterozygote in this row ar1 = fmin(resn[r], resn[1]); resn[1] -= ar1; resn[r] -= ar1; // Before calling homozygote, see if we have visited this node before by comparing its hash tag. hash = makeHash(r-1, Rnew); i = 0; // Search list of old nodes while (hash != nodez[i].hash && i < nextNode) i++; if(i < nextNode) { // old node was found, no need to go any further. tableCount += nodez[i].count; } else { // new node countsSoFar = tableCount; homozygote(r-1, Rnew); if (nextNode < MAXNODE) { // Make a new node nodez[i].hash = hash; nodez[i].count = tableCount - countsSoFar; nextNode++; } } // new node } if(r==3) // and c = 2, then we can handle a series of two-allele cases with no deeper recursion { for(a32 = lower; a32 <= upper; a32++) { a31 = fmin(res[1], res[3]-a32); //Value of a31 is now fixed for each a32 ntables = (fmin(res[1] - a31, res[2]-a32))/2 + 1; tableCount += ntables; } } // if r == 3 } // if c == 2 } // heterozygote