我试图从txt文件实现Sha-1的哈希实现" abc"得到a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
,但我得到了输出E66882F9 DF84C901 18BADCFE 90325476 C3D2E1F0
。我知道问题出在我的阅读或convertChar
方法中,但我无法弄清楚我的问题是什么。
对于readfile,我需要在buffer []数组的末尾附加1位并返回文件中的字符数。变量长度是const unsigned int。
对于convertArrayToIntArray,我需要通过将4个字符包装到1个互变量中,将unsigned char转换为等效的无符号整数数组。
unsigned int readFile (unsigned char buffer[])
{
length = 0;
int b = 0;
int i = 0;
char *fileName = "abc.txt";
FILE *filePointer = fopen (fileName, "rb");
if (filePointer != NULL)
{
length = fread (buffer, sizeof (char), MAX_SIZE, filePointer);
if (ferror (filePointer) != 0) {
fputs ("Error", stderr);
}
if (length > MAX_SIZE) {
fputs ("Input file too big for program", stderr);
}
i = length - 1;
if (i < 0 && buffer[i] == '\n') {
buffer[i] = '\0';
}
length++;
buffer[length - 1] = 0x80;
}
fclose (filePointer);
return length;
}
void convertCharArrayToIntArray (unsigned char buffer[], unsigned int
message[], unsigned int sizeOfFileInBytes)
{
int e = 0;
size_t buffLength = length;
buffLength = (buffLength + 3) / 4;
for (e = 0; e < buffLength; e++) {
message[e] |= (buffer[e] << 24);
message[e] |= (buffer[e + 1] << 16);
message[e] |= (buffer[e + 2] << 8);
message[e] |= (buffer[e + 3]);
}
}
答案 0 :(得分:0)
你也忘了每8个字符就有一个空格,这会导致字符包装出错。
#define MAX_SIZE 256
unsigned int length = 0;
void convertCharArrayToIntArray(unsigned char buffer[], unsigned int message[])
{
int e = 0;
int cnt=0;
//int d = 0;
//buffLength shows me how many chars are in array
size_t buffLength= length;
//printf("%i tot", buffLength);
buffLength = (buffLength+ 3)/4;
printf("%d\n",buffLength);
int crs=0;
for (e=0 ; e< buffLength-1; e++)
{
unsigned int n=0;
n |= (buffer[crs ] << 24);
n |= (buffer[crs+1] << 16);
n |= (buffer[crs+2] << 8 );
n |= (buffer[crs+3] );
printf("4 chars :>%c%c%c%c to uint=%d\n",buffer[crs ],buffer[crs+1],buffer[crs+2],buffer[crs+3],n );
crs+=4;
cnt++;
if(cnt==2)/*We can not forget that there is a space between every 8 characters ...*/
{
cnt=0;
crs++;
}
message[e]=n;/*Save our uint in buffer*/
}
}
unsigned int readFile(unsigned char buffer[])
{
//int sizer;
int b = 0;
int i = 0;
char *fileName = "abc.txt";
FILE *filePointer = fopen(fileName, "rb");
if (filePointer != NULL)
{
length = fread(buffer, sizeof(char), MAX_SIZE , filePointer);
if (ferror(filePointer)!= 0)
{
fputs("Error", stderr);
return 0;/*error*/
}
if (length > MAX_SIZE )
{
fputs("Input file too big for program", stderr);
return 0;/*error*/
}
/*
It is not necessary to add a null at the end, the buffer is already initialized with zeros
*/
/*Did not display the content correctly because it was in hexadecimal*/
for (i = 0; i < length; i++)
printf("%c", buffer[i]);/*use %c show real data, it is rubbish but work */
/*for (i = 0; i < length; i++)
printf("%08x", buffer[i]); */
}
/*OR print*/
printf("\n\n%s\n",buffer);
fclose(filePointer);
return length;
}
void main()
{
unsigned char szbuffer[1024]={0};/*initialized to zeroes*/
if(readFile(szbuffer))
{
unsigned int message[100];
convertCharArrayToIntArray(szbuffer,message);
}
else
printf("error...\n");
return;
}