int create_account(DB *database, unsigned char *username, unsigned char *password) { int user_hash = SearchHash(username); if (!strcmp(username, database->db[user_hash].username)) { printf("Sorry, that username is already taken. Please try again.\n\n"); return 0; } if (!check_password(password)) { printf("Sorry, your password must be 8-20 characters in length and contain "); printf("either a number or one of these special characters: !@#$%%*.\n\n"); return 0; } // copy username to db location strcpy(database->db[user_hash].username, username); // create new password with salt int new_length = strlen(password) + strlen(salt); char *salt_password = malloc(sizeof(char) * new_length + 1); strcpy(salt_password, password); strcat(salt_password, salt); database->db[user_hash].password = CryptoHash(salt_password); return 1; }
int main(int argc, const char* argv[]) { int i; int* addr = (int *) malloc(sizeof(int)); HashTable H; InitHashTable(&H); for (i = 0; i < 10; i++) { InsertHash(&H, i * 2); } int t = SearchHash(H, 12, addr); printf("%d\n", t); }
Status InsertHash(HashTable &H, HElemType e) { // 算法9.18 // 查找不成功时插入数据元素e到开放定址哈希表H中,并返回OK; // 若冲突次数过大,则重建哈希表 int c = 0; int p = 0; if (SearchHash(H, e.key, p, c) == SUCCESS ) return DUPLICATE; // 表中已有与e有相同关键字的元素 else if (c < H.cursize) { // 冲突次数c未达到上限,(阀值c可调) H.elem[p] = e; ++H.count; return SUCCESS; // 插入e } else { RecreateHashTable(H); // 重建哈希表 return UNSUCCESS; } } // InsertHash
//insertHash Status InsertHash(HashTable &H,Elemtype e) { c=0; if(SearchHash(H,e.key,p,c)) return DUPLICATE; else if(c<Hahsize[H.sizeindex]/2) { H.elem[p]=e; ++H.count; return OK; } else { ReceateHashTable(H); return UNSUCCESS; } }
int login(DB *database, unsigned char *username, unsigned char *password) { unsigned long user_hash = SearchHash(username); if (!strcmp(username, database->db[user_hash].username)) { int new_length = strlen(password) + strlen(salt); char *salt_password = malloc(sizeof(char) * new_length + 1); strcpy(salt_password, password); strcat(salt_password, salt); if (CryptoHash(salt_password) == database->db[user_hash].password) return 1; else return 0; } return 0; }
/* * @description:哈希表插入元素 */ Status InsertHash(HashTable *H,KeyType key) { int c,p; //用于计数,也用作步长 c = 0; if(SearchHash(*H,key,&p,&c)) return DUPLICATE; /* 产生冲突但是冲突次数没有达到上限 这里的上限是可以调整,根据实际情况来设定 */ else if(c < hashsize[(*H).sizeindex] / 2) { //插入 (*H).elem[p].key = key; (*H).count++; return SUCCESS; } //冲突次数超过了上限,故需要重建哈希表即进行扩容 else { RecreateHash(H); return UNSUCCESS; } }
void parse_msr( void ) { while ( 1 ) { int f; char *p = gettoken( &f ); //char a; if ( !p ) break; //a = *p; if ( f & TT_LABEL ) { int n = strlen(p); if ( p[n-1] == ':' ) p[n-1] = 0; RegLabel( p ); } else if ( f & TT_NUMBER ) { cgout1( getnum( p ) ); } else if ( f & TT_STRING ) { int i, n = strlen( p ); if ( n > 2 && p[0] == p[n-1] ) n--; for ( i = 1; i < n; i++ ) cgout1( p[i] ); } else if ( f & TT_OTHER ) { HASHDATA *phd = SearchHash( p ); if ( phd ) { if ( phd->type == TYPE_RESERVED ) switch ( phd->value ) { case RSV_ADRS: adrs(); break; case RSV_ADRSM: adrsm(); break; case RSV_ADRSD: adrsd(); break; case RSV_INCLUDE: include(); break; case RSV_GOTO: _goto(); break; case RSV_TEMPO: tempo(); break; case RSV_PARTN: partn(); break; case RSV_PHRASE: phrase(); break; } else if ( phd->type & TYPE_DIRECTCODE ) { cgout1( phd->value ); } } else { error( "Unkown commands '%s'.", p ); } } else if ( f & TT_MML ) { parse_mml_line( -1, p ); } } }