⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 haval.cpp

📁 A console-based hah calculators
💻 CPP
📖 第 1 页 / 共 3 页
字号:
            ((haval_word)*(sp+2) << 16) |  \
            ((haval_word)*(sp+3) << 24);   \
    sp += 4;                               \
  }                                        \
}

/* translate each word into four characters */
#define haval_uint2ch(word, string, wlen) {              \
  haval_word    *wp = word;                        \
  unsigned char *sp = string;                      \
  while (wp < (word) + (wlen)) {                   \
    *(sp++) = (unsigned char)( *wp        & 0xFF); \
    *(sp++) = (unsigned char)((*wp >>  8) & 0xFF); \
    *(sp++) = (unsigned char)((*wp >> 16) & 0xFF); \
    *(sp++) = (unsigned char)((*wp >> 24) & 0xFF); \
    wp++;                                          \
  }                                                \
}


/* hash a string */
void haval_string (char *string, unsigned char *fingerprint)
{
  haval_state   hstate;
  unsigned int  len = strlen (string);

  haval_start (&hstate);
  haval_hash (&hstate, (unsigned char *)string, len);
  haval_end (&hstate, fingerprint);
}

/* hash a file */
int haval_file (char *file_name, unsigned char *fingerprint)
{
  FILE          *file;
  haval_state   hstate;
  int           len;
  unsigned char buffer[1024];

  if ((file = fopen (file_name, "rb")) == NULL){
    return (1);                                    /* fail */
  } else {
    haval_start (&hstate);
    while ((len = fread (buffer, 1, 1024, file))) {
      haval_hash (&hstate, buffer, len);
    }
    fclose (file);
    haval_end (&hstate, fingerprint);
    return (0);                                    /* success */
 }
}

/* hash input from stdin */
void haval_stdin (void)
{
  haval_state   hstate;
  int           i, len;
  unsigned char buffer[32],
                fingerprint[HAVAL_FPTLEN >> 3];

  haval_start (&hstate);
  /* while (len = fread (buffer, 1, 32, stdin)) { */
  while ((len = fread (buffer, 1, 32, stdin))) {
    haval_hash (&hstate, buffer, len);
  }
  haval_end (&hstate, fingerprint);
  
  for (i = 0; i < HAVAL_FPTLEN >> 3; i++) {
    /* putchar(fingerprint[i]); */
    printf ("%02X", fingerprint[i]);
  }
  printf("\n");
}

/* initialization */
void haval_start (haval_state *hstate)
{
    hstate->count[0]       = hstate->count[1] = 0;   /* clear count */
    hstate->fingerprint[0] = 0x243F6A88L;           /* initial fingerprint */
    hstate->fingerprint[1] = 0x85A308D3L;
    hstate->fingerprint[2] = 0x13198A2EL;
    hstate->fingerprint[3] = 0x03707344L;
    hstate->fingerprint[4] = 0xA4093822L;
    hstate->fingerprint[5] = 0x299F31D0L;
    hstate->fingerprint[6] = 0x082EFA98L;
    hstate->fingerprint[7] = 0xEC4E6C89L;
}

/*
 * hash a string of specified length.
 * to be used in conjunction with haval_start and haval_end.
 */
void haval_hash (haval_state *hstate,
                 unsigned char *str, unsigned int str_len)
{
  unsigned int i,
           rmd_len,
           fill_len;

  /* calculate the number of bytes in the remainder */
  rmd_len  = (unsigned int)((hstate->count[0] >> 3) & 0x7F);
  fill_len = 128 - rmd_len;

  /* update the number of bits */
  if ((hstate->count[0] +=  (haval_word)str_len << 3)
                        < ((haval_word)str_len << 3)) {
     hstate->count[1]++;
  }
  hstate->count[1] += (haval_word)str_len >> 29;

#ifdef RH_LITTLE_ENDIAN

  /* hash as many blocks as possible */
  if (rmd_len + str_len >= 128) {
    memcpy (((unsigned char *)hstate->block)+rmd_len, str, fill_len);
    haval_hash_block (hstate);
    for (i = fill_len; i + 127 < str_len; i += 128){
      memcpy ((unsigned char *)hstate->block, str+i, 128);
      haval_hash_block (hstate);
    }
    rmd_len = 0;
  } else {
    i = 0;
  }
  memcpy (((unsigned char *)hstate->block)+rmd_len, str+i, str_len-i);

#else

  /* hash as many blocks as possible */
  if (rmd_len + str_len >= 128) {
    memcpy (&hstate->remainder[rmd_len], str, fill_len);
    haval_ch2uint(hstate->remainder, hstate->block, 128);
    haval_hash_block (hstate);
    for (i = fill_len; i + 127 < str_len; i += 128){
      memcpy (hstate->remainder, str+i, 128);
      haval_ch2uint(hstate->remainder, hstate->block, 128);
      haval_hash_block (hstate);
    }
    rmd_len = 0;
  } else {
    i = 0;
  }
  /* save the remaining input chars */
  memcpy (&hstate->remainder[rmd_len], str+i, str_len-i);

#endif
}

/* finalization */
void haval_end (haval_state *hstate, unsigned char *final_fpt)
{
  unsigned char tail[10];
  unsigned int  rmd_len, pad_len;

  /*
   * save the version number, the number of passes, the fingerprint 
   * length and the number of bits in the unpadded message.
   */
  tail[0] = (unsigned char)(((HAVAL_FPTLEN  & 0x3) << 6) |
                            ((HAVAL_PASS    & 0x7) << 3) |
                             (HAVAL_VERSION & 0x7));
  tail[1] = (unsigned char)((HAVAL_FPTLEN >> 2) & 0xFF);
  haval_uint2ch (hstate->count, &tail[2], 2);

  /* pad out to 118 mod 128 */
  rmd_len = (unsigned int)((hstate->count[0] >> 3) & 0x7f);
  pad_len = (rmd_len < 118) ? (118 - rmd_len) : (246 - rmd_len);
  haval_hash (hstate, haval_padding, pad_len);

  /*
   * append the version number, the number of passes,
   * the fingerprint length and the number of bits
   */
  haval_hash (hstate, tail, 10);

  /* tailor the last output */
  haval_tailor(hstate);

  /* translate and save the final fingerprint */
  haval_uint2ch (hstate->fingerprint, final_fpt, HAVAL_FPTLEN >> 5);

  /* clear the hstate information */
  memset ((unsigned char *)hstate, 0, sizeof (*hstate));
}

/* hash a 32-word block */
void haval_hash_block (haval_state *hstate)
{
  register haval_word t0 = hstate->fingerprint[0],    /* make use of */
                      t1 = hstate->fingerprint[1],    /* internal registers */
                      t2 = hstate->fingerprint[2],
                      t3 = hstate->fingerprint[3],
                      t4 = hstate->fingerprint[4],
                      t5 = hstate->fingerprint[5],
                      t6 = hstate->fingerprint[6],
                      t7 = hstate->fingerprint[7],
                      *w = hstate->block;

  /* Pass 1 */
  haval_FF_1(t7, t6, t5, t4, t3, t2, t1, t0, *(w   ));
  haval_FF_1(t6, t5, t4, t3, t2, t1, t0, t7, *(w+ 1));
  haval_FF_1(t5, t4, t3, t2, t1, t0, t7, t6, *(w+ 2));
  haval_FF_1(t4, t3, t2, t1, t0, t7, t6, t5, *(w+ 3));
  haval_FF_1(t3, t2, t1, t0, t7, t6, t5, t4, *(w+ 4));
  haval_FF_1(t2, t1, t0, t7, t6, t5, t4, t3, *(w+ 5));
  haval_FF_1(t1, t0, t7, t6, t5, t4, t3, t2, *(w+ 6));
  haval_FF_1(t0, t7, t6, t5, t4, t3, t2, t1, *(w+ 7));

  haval_FF_1(t7, t6, t5, t4, t3, t2, t1, t0, *(w+ 8));
  haval_FF_1(t6, t5, t4, t3, t2, t1, t0, t7, *(w+ 9));
  haval_FF_1(t5, t4, t3, t2, t1, t0, t7, t6, *(w+10));
  haval_FF_1(t4, t3, t2, t1, t0, t7, t6, t5, *(w+11));
  haval_FF_1(t3, t2, t1, t0, t7, t6, t5, t4, *(w+12));
  haval_FF_1(t2, t1, t0, t7, t6, t5, t4, t3, *(w+13));
  haval_FF_1(t1, t0, t7, t6, t5, t4, t3, t2, *(w+14));
  haval_FF_1(t0, t7, t6, t5, t4, t3, t2, t1, *(w+15));

  haval_FF_1(t7, t6, t5, t4, t3, t2, t1, t0, *(w+16));
  haval_FF_1(t6, t5, t4, t3, t2, t1, t0, t7, *(w+17));
  haval_FF_1(t5, t4, t3, t2, t1, t0, t7, t6, *(w+18));
  haval_FF_1(t4, t3, t2, t1, t0, t7, t6, t5, *(w+19));
  haval_FF_1(t3, t2, t1, t0, t7, t6, t5, t4, *(w+20));
  haval_FF_1(t2, t1, t0, t7, t6, t5, t4, t3, *(w+21));
  haval_FF_1(t1, t0, t7, t6, t5, t4, t3, t2, *(w+22));
  haval_FF_1(t0, t7, t6, t5, t4, t3, t2, t1, *(w+23));

  haval_FF_1(t7, t6, t5, t4, t3, t2, t1, t0, *(w+24));
  haval_FF_1(t6, t5, t4, t3, t2, t1, t0, t7, *(w+25));
  haval_FF_1(t5, t4, t3, t2, t1, t0, t7, t6, *(w+26));
  haval_FF_1(t4, t3, t2, t1, t0, t7, t6, t5, *(w+27));
  haval_FF_1(t3, t2, t1, t0, t7, t6, t5, t4, *(w+28));
  haval_FF_1(t2, t1, t0, t7, t6, t5, t4, t3, *(w+29));
  haval_FF_1(t1, t0, t7, t6, t5, t4, t3, t2, *(w+30));
  haval_FF_1(t0, t7, t6, t5, t4, t3, t2, t1, *(w+31));

  /* Pass 2 */
  haval_FF_2(t7, t6, t5, t4, t3, t2, t1, t0, *(w+ 5), 0x452821E6L);
  haval_FF_2(t6, t5, t4, t3, t2, t1, t0, t7, *(w+14), 0x38D01377L);
  haval_FF_2(t5, t4, t3, t2, t1, t0, t7, t6, *(w+26), 0xBE5466CFL);
  haval_FF_2(t4, t3, t2, t1, t0, t7, t6, t5, *(w+18), 0x34E90C6CL);
  haval_FF_2(t3, t2, t1, t0, t7, t6, t5, t4, *(w+11), 0xC0AC29B7L);
  haval_FF_2(t2, t1, t0, t7, t6, t5, t4, t3, *(w+28), 0xC97C50DDL);
  haval_FF_2(t1, t0, t7, t6, t5, t4, t3, t2, *(w+ 7), 0x3F84D5B5L);
  haval_FF_2(t0, t7, t6, t5, t4, t3, t2, t1, *(w+16), 0xB5470917L);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -