Exemple #1
0
static TaggedString *insert_s (char *str, long l, stringtable *tb)
{
  TaggedString *ts;
  unsigned long h = hash_s(str, l);
  int size = tb->size;
  int i;
  int j = -1;
  if ((long)tb->nuse*3 >= (long)size*2) {
    grow(tb);
    size = tb->size;
  }
  for (i = h%size; (ts = tb->hash[i]) != NULL; ) {
    if (ts == &EMPTY)
      j = i;
    else if (ts->constindex >= 0 &&
             ts->u.s.len == l &&
             (memcmp(str, ts->str, l) == 0))
      return ts;
    if (++i == size) i=0;
  }
  /* not found */
  if (j != -1)  /* is there an EMPTY space? */
    i = j;
  else
    tb->nuse++;
  ts = tb->hash[i] = newone_s(str, l, h);
  return ts;
}
Exemple #2
0
 bool isAnagram(string s, string t) {
     vector<int> hash_s (26,0);
     vector<int> hash_t (26,0);
     hash_s = updateHashTable(s, hash_s);
     hash_t = updateHashTable(t, hash_t);
     int i;
     for(i=0;i<26;i++){
         if(hash_s[i]!=hash_t[i])
             return false;
     }
     return true;
 }
Exemple #3
0
int main(int argc, char *argv[])
{
    char hashBuffer[32 + 1];
    char saltBuffer[SALT_LEN + 1];
    char saltHexBuffer[SALT_LEN * 2 +1];
    FILE *f = NULL;
    int ch;
    int result = EXIT_FAILURE;
    char *password = NULL;
    bool easyMode = false;

    while ((ch = getopt(argc, argv, "ehp:")) != -1) {
        switch(ch) {
         case 'e':
            easyMode = true;
            break;
         case 'h':
            help(argv[0]); 
            result = EXIT_SUCCESS;
            goto free_params;
        case 'p':
            password = strdup(optarg);
            break;
        case '?':
        default:
            result = EXIT_FAILURE;
            goto free_params;
        }
    }

    /* Sanity checks */
    if (password == NULL) {
        fprintf(stderr, "[-] Must provide the password\n");
        result = EXIT_FAILURE;
        goto free_params;
    }

    if(!easyMode) {
        printf("[*] Password = %s\n", password);
    }

    /* Generate random salt */
    f = fopen(RANDOM_DEV, "r");
    if(f == NULL) {
        perror("[-] Unable to open " RANDOM_DEV "\n");
        result = EXIT_FAILURE;
        goto free_params;
    }
    fread(saltBuffer, sizeof(char), SALT_LEN, f);
    saltBuffer[SALT_LEN] = '\0';
    hexify((unsigned char *) saltBuffer, SALT_LEN, saltHexBuffer);
    saltHexBuffer[SALT_LEN * 2] = '\0';

    printf((easyMode)? "%s:" : "[+] Salt = %s\n",
        saltHexBuffer);

    fclose(f);
    f = NULL;

    if(hash_s(saltHexBuffer, password, hashBuffer) == -1) {
        perror("[-] Unable to generate the hash\n");
        result = EXIT_FAILURE;
        goto free_params;
    }
    
    printf((easyMode)? "%s\n" : "[+] Hash = %s\n",
        hashBuffer);

    result = EXIT_SUCCESS;

free_params:
    if (password != NULL) {
        memset_s(password, 0, strlen(password));
        free(password);
    }

    return result;
}