HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen) { DataLength i; BitSequence nextbit; for (i = 0;i < databitlen;++i) { nextbit = (data[i / 8] & mybit[i % 8]) / mybit[i % 8]; updatebit(state,nextbit); } return SUCCESS; }
HashReturn Final(hashState *state, BitSequence *hashval) { myuint32 x[2][2][2][2][2]; int i; /* "append a 1 bit to the input message;" */ updatebit(state,1); /* "then append the minimum possible number of 0 bits" */ /* "to reach a multiple of 8b bits" */ while (state->blockbits != 0) updatebit(state,0); /* "the integer 1 is xored into the last state word x_11111" */ state_tox(state->state,x); x[1][1][1][1][1] ^= 1; /* "the state is then transformed invertibly through 10r identical rounds" */ for (i = 0;i < 10;++i) rrounds(x); state_fromx(state->state,x); /* "output the first h/8 bytes of the state" */ for (i = 0;i < state->hashbitlen / 8;++i) hashval[i] = state->state[i]; return SUCCESS; }
int main () { int c; unsigned int x = 15; // 1111 printf ("x = %d (1111)\n\n", 15); printf ("x ^ 0 = %d (x)\n", x ^ 0); printf ("x ^ 1 = %d ~(x)\n", x ^ 15); printf ("x ^ x = %d (0)\n\n", x ^ x); printf ("x & 0 = %d (0)\n", x & 0); printf ("x & 1 = %d (x)\n", x & 15); printf ("x & x = %d (x)\n\n", x & x); printf ("x | 0 = %d (x)\n", x | 0); printf ("x | 1 = %d (1)\n", x | 15); printf ("x | x = %d (x)\n\n", x | x); printf ("x = %d (1010)\n", x = 10); for (c = 3; c >= 0; --c) printf ("get %d bit: %d\n", c, getbit (x, c)); printf ("\n"); printf ("set bit %d to 1: %d\n", 2, x = setbit (x, 2)); for (c = 3; c >= 0; --c) printf ("get %d bit: %d\n", c, getbit (x, c)); printf ("\n"); printf ("clear bit %d to 0: %d\n", 3, x = clearbit (x, 3)); for (c = 3; c >= 0; --c) printf ("get %d bit: %d\n", c, getbit (x, c)); printf ("\n"); printf ("x = %d (1111)\n", x = 15); printf ("clear most significant bits through bit %d: %d\n", 2, x = clearbits_msb (x, 2)); for (c = 3; c >= 0; --c) printf ("get %d bit: %d\n", c, getbit (x, c)); printf ("\n"); printf ("x = %d (1111)\n", x = 15); printf ("clear least significant bits through bit %d: %d\n", 1, x = clearbits_lsb (x, 1)); for (c = 3; c >= 0; --c) printf ("get %d bit: %d\n", c, getbit (x, c)); printf ("\n"); printf ("x = %d (1111)\n", x = 15); printf ("update bit %d to 0: %d\n", 2, x = updatebit (x, 2, 0)); printf ("update bit %d to 1: %d\n", 3, x = updatebit (x, 3, 1)); for (c = 3; c >= 0; --c) printf ("get %d bit: %d\n", c, getbit (x, c)); printf ("\n"); printf ("x = %d (1111)\n", x = 15); printf ("update bit %d to 0 (C support): %d\n", 2, x = updatebit_c (x, 2, 0)); printf ("update bit %d to 1 (C support): %d\n", 3, x = updatebit_c (x, 3, 1)); for (c = 3; c >= 0; --c) printf ("get %d bit: %d\n", c, getbit (x, c)); printf ("\n"); printf ("toggle bit %d: %d\n", 1, x = togglebit (x, 1)); printf ("toggle bit %d: %d\n", 3, x = togglebit (x, 3)); for (c = 3; c >= 0; --c) printf ("get %d bit: %d\n", c, getbit (x, c)); }