SHA-1数据填充和扩展

  1. main.cpp
#include
#include"SHA-1算法-2016123068.h"
using namespace std;
int main(int argc, char *argv[]){
	SHA_1_068();
	printf("\n");
}
  1. SHA-1算法-2016123068.h
#pragma once
void SHA_1_068();
void PutBox(char *s,int *box);//将消息转换成2进制数据依次填入数组
void Addlast(int *box, int bit);//将消息的二进制长度输入末尾
void divide(int *box, int w[][8]);//将2进制数据每4位转换成16进制数填入w数组
void extend(int w[][8]);//扩展w数组
void bin(int w[][8],long long int p,int t);//异或循环左移的数据填充进w数组
void putdata(int w[][8]);//输出结果

  1. SHA-1算法-2016123068.cpp
#include"SHA-1算法-2016123068.h"
#include
#include
#include
#define leftbit(bits,word) ((((word) << (bits)) & 0xFFFFFFFF) | ((word) >> (32-(bits))))
//char string[100];
int lenth;//输入消息的长度
int bit;//输入消息的初始bit
char sput[100];
int box1[512];
int box2[512];
int w[80][8];//数据扩充存放数组
int w_[80][8];//输入bit大于448是要动用的扩充数组
long long int a, b, c, d,x;
void SHA_1_068() {
	printf("请输入消息:");
	gets_s(sput);
	lenth = strlen(sput);
	bit = lenth * 8;
	PutBox(sput, box1);
	box1[bit] = 1;//原始消息后添加一个"1"比特位
	if (bit < 448) {
		Addlast(box1, bit);
		divide(box1, w);
		extend(w);
		putdata(w);
	}
	else {
		Addlast(box2, bit);
		divide(box1, w);
		divide(box2, w_);
		extend(w);
		extend(w_);
		putdata(w);
		putdata(w_);
	}
}
void PutBox(char*s, int *box) {
	int i=0;
	int temp;//暂时保存二进制的数
	while (s[i] != '\0') {
		for (; i < lenth; i++) {
			int count = 0;
			while (s[i] != 0) {
				temp = s[i] % 2;
				s[i] = s[i] / 2;
				box[(i + 1) * 7 +i- count] = temp;
				count++;
			}
		}
	}
}
void Addlast(int *box, int bit) {
	int temp;
	int count = 0;
	while (bit != 0) {
		temp = bit % 2;
		bit = bit / 2;
		box[511- count] = temp;
		count++;
	}
}
void divide(int *box, int w[][8]) {
	int i=0, j=0,count=0;
	int sum = 0,flag=0;
	while (count < 513) {
		if (j == 8) {
			i++;
			j = 0;
		}     
		if (flag== 4) {
			w[i][j] = sum;
			flag= 0;
			sum = 0;
			j++;
		}
		if (count % 4 == 0) {
			sum+=box[count] * 8;
			flag++;
		}
		else if (count % 4 == 1) {
			sum += box[count] * 4;
			flag++;
		}
		else if (count % 4 == 2) {
			sum += box[count] * 2;
			flag++;
		}
		else if (count % 4 == 3) {
			sum += box[count] * 1;
			flag++;
		}
		count++;
	}
}
void extend(int w[][8]) {
	int i = 0;
	long long int i_;
	int t = 16;
	while (t < 80) {
		for (i=0; i < 8; i++) {
			a |= ((unsigned int)w[t - 3][7 - i] & 0xFu) << (i * 4);
		}
		for (i=0; i < 8; i++) {
			b |= ((unsigned int)w[t - 8][7 - i] & 0xFu) << (i * 4);
		}
		for (i=0; i < 8; i++) {
			c |= ((unsigned int)w[t - 14][7 - i] & 0xFu) << (i * 4);
		}
		for (i=0; i < 8; i++) {
			d |= ((unsigned int)w[t - 16][7 - i] & 0xFu) << (i * 4);
		}
		x = a^b^c^d;
		i_ = leftbit(1, x);
		bin(w, i_, t);
		a = b = c = d = 0;
		t++;
	}
}
void bin(int w[][8],long long int p,int t) {
	int temp;
	int i = 0;
	while (p != 0) {
		temp = p % 16;
		p = p / 16;
		w[t][7 - i] = temp;
		i++;
	}
}
void putdata(int w[][8]) {
	int i, j;
	printf("w[0]:");
		for (j = 0; j < 8; j++) {
			printf("%x",w[0][j]);
		}
		printf("\n");
		printf("w[1]:");
		for (j = 0; j < 8; j++) {
			printf("%x", w[1][j]);
		}
		printf("\n");
		printf("w[2]:");
		for (j = 0; j < 8; j++) {
			printf("%x", w[2][j]);
		}
		printf("\n");
		printf("w[15]:");
		for (j = 0; j < 8; j++) {
			printf("%x", w[15][j]);
		}
		printf("\n");
		printf("w[79]:");
	for (i = 0; i < 8; i++) {
		printf("%x", w[79][i]);
	}
	printf("\n");
}

  1. 运行结果
    SHA-1数据填充和扩展_第1张图片
    SHA-1数据填充和扩展_第2张图片

你可能感兴趣的:(密码算法)