博客 | 算法 | 摘要算法CubeHash
摘要算法CubeHash
·
CubeHash是一种实现非常简单的摘要算法,其操作全部都是简单的加、移位和交换操作,却仍具备高度的安全性。忙,不多说了。
#include "STDIO.H"
#include "STRING.H"
#include "STDLIB.H"
#ifdef _MSC_VER
#define ROL32(x,bits) (_rotl(x,bits))
#define ROR32(x,bits) (_rotr(x,bits))
#else
#define ROL32(x,bits) ((x<<bits) | (x>>(32-bits)))
#define ROR32(x,bits) ((x>>bits) | (x<<(32-bits)))
#endif
#define SWAP(A,B) {\
A^=B;\
B^=A;\
A^=B;\
}
#define INITIALIZE_ROUNDS 80
#define HASH_ROUNDS 8
#define FINAL_ROUNDS 80
#define OUTPUT_BYTES 64
typedef struct {
unsigned char state[128];
} cubehash_t; /* cubehash80+8/1+80-512, i = 80, r = 8, b = 1, f = 80, h = 512 */
void cubehash_round(cubehash_t *self);
cubehash_t *cubehash_new(void);
cubehash_t *cubehash_new() {
cubehash_t *result;
int i;
unsigned int *ptr;
result = malloc(sizeof(cubehash_t));
memset(result, 0, sizeof(cubehash_t));
ptr = (unsigned int *)(result->state);
ptr[0] = OUTPUT_BYTES;
ptr[1] = 1;
ptr[2] = HASH_ROUNDS;
for(i = 0; i < INITIALIZE_ROUNDS; i++) {
cubehash_round(result);
}
return result;
}
void cubehash_round(cubehash_t *self) {
unsigned int *ptr;
int i;
ptr = (unsigned int *)(self->state);
for(i = 0; i < 16; i++) { /* STEP 1 */
ptr[i + 16] += ptr[i];
}
for(i = 0; i < 16; i++) { /* STEP 2 */
ptr[i] = ROL32(ptr[i], 7);
}
for(i = 0; i < 8; i++) { /* STEP 3 */
SWAP(ptr[i], ptr[i + 8]);
}
for(i = 0; i < 16; i++) { /* STEP 4 */
ptr[i] ^= ptr[i + 16];
}
for(i = 0; i < 4; i++) { /* STEP 5 */
SWAP(ptr[16 + i * 4], ptr[16 + i * 4 + 2]);
SWAP(ptr[16 + i * 4 + 1], ptr[16 + i * 4 + 3]);
}
for(i = 0; i < 16; i++) { /* STEP 6 */
ptr[i + 16] += ptr[i];
}
for(i = 0; i < 16; i++) { /* STEP 7 */
ptr[i] = ROL32(ptr[i], 11);
}
for(i = 0; i < 4; i++) { /* STEP 8 */
SWAP(ptr[i + 4], ptr[i]);
SWAP(ptr[i + 12], ptr[i + 8]);
}
for(i = 0; i < 16; i++) { /* STEP 9 */
ptr[i] ^= ptr[i + 16];
}
for(i = 0; i < 8; i++) { /* STEP 10 */
SWAP(ptr[16 + (i << 1)], ptr[17 + (i << 1)]);
}
}
void cubehash_final(cubehash_t *self, unsigned char output[OUTPUT_BYTES]) {
unsigned int *ptr;
int i;
self->state[0] ^= 128;
for(i = 0; i < HASH_ROUNDS; i++) {
cubehash_round(self);
}
ptr = (unsigned int *)self->state;
ptr[31] ^= 1;
for(i = 0; i < FINAL_ROUNDS; i++) {
cubehash_round(self);
}
memset(output, 0, OUTPUT_BYTES);
memcpy(output, self->state, OUTPUT_BYTES);
}
void cubehash_update(cubehash_t *self, unsigned char *input, int length) {
int i, j;
for(i = 0; i < length; i++) {
self->state[0] ^= input[i];
for(j = 0; j < 8; j++) {
cubehash_round(self);
}
}
}
char *message = "The quick brown fox jumps over the lazy dog";
int main(int argc, char **argv) {
cubehash_t *md;
unsigned char result[OUTPUT_BYTES];
int i;
md = cubehash_new();
cubehash_update(md, message, strlen(message));
cubehash_final(md, result);
printf("MESSAGE:%s\n", message);
printf("SHALL BE:\n%s\nACTUAL:\n", "ca942b088ed9103726af1fa87b4deb59e50cf3b5c6dcfbcebf5bba22fb39a6be9936c87bfdd7c52fc5e71700993958fa4e7b5e6e2a3672122475c40f9ec816ba");
for(i = 0; i < OUTPUT_BYTES; i++) {
printf("%02x", result[i]);
}
printf("\n");
return(0);
}
更多推荐
所有评论(0)