gpt4 book ai didi

c - 将 char 数组分配给 union 数据类型时,我的编译器抛出错误 : "conversion from char* to non-scalar type"

转载 作者:太空宇宙 更新时间:2023-11-04 03:51:33 25 4
gpt4 key购买 nike

让我解释一下我的程序的整体功能。用户可以输入一个字符串,然后根据 MD5 哈希算法对该字符串进行哈希处理。然后它将字符串打印到标准输出。

我发现使用 union 结构简化了代码:

union md5hash
{
uint ui[4]; //= 16 bytes --> 128 bits
char ch[16]; //= 16 bytes --> 128 bits
};

我的程序在那种情况下运行良好。

但是,我想扩展我的程序的功能。我不想输入要散列的字符串,而是希望通过标准输入输入 MD5 散列。

这是一个尝试复制此功能的简短程序:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>

typedef unsigned int uint;

union md5hash
{
uint ui[4]; //= 16 bytes --> 128 bits
char ch[16]; //= 16 bytes --> 128 bits
};

int main(void){


std::string hash = "abcdefghjklqwertyuiopasdfghjklzx";
std::cout << hash.length();
char* chash = (char*) malloc(sizeof(hash.length()+1));
memcpy(chash, hash.c_str(), hash.size()+1);
for(int i = 0; chash[i] ; i++) printf("%c",chash[i]);
md5hash test = chash;
getchar();

}

是的,我知道我的数组有 32 个字符长,而 union 中的数组只有 16 个字符(这是我尚未解决的问题)。在任何情况下,我都需要通过 stdin 输入的哈希值与经过哈希处理的字符串采用相同的形式。

例如:

Enter string: "Hello"
Goes through hash algorithm: 5d41402abc4b2a76b9719d911017c592

Enter hash:"5d41402abc4b2a76b9719d911017c592"

Are they equal? (Compare their hash values).

然而,hash 是一个 char 数组,而哈希值是 md5hash 数据类型。如何将通过标准输入输入的哈希值转换为 md5hash 数据类型?

此外,我正在使用 Dev C++ - 它使用 GCC(GNU 编译器集合)的 Mingw 端口作为编译器。我收到的错误是

 conversion from `char*' to non-scalar type `md5hash' requested. 

(我从上述程序中收到错误并不感到惊讶)。

感谢任何建设性的意见。

编辑:

这就是我对字符串进行哈希处理的方式(这不是完整的代码,但这是为哈希准备字符串的方式):

void md5_prep(char *c0)
{
uint len = 0;
char *c = c0;
while(*c) {len++; c++;}
c[0] = 0x80; // bit 1 after the message (Appears counterintuitive, but remember c was incremented in the above loop
((uint*)c0)[14] = len * 8; // message length in bits (Note: 4 * 14 is 56 - appends to end of buffer as stated in Wikipedia's psuedocode)
}

void print_md5(uint *hash, bool crlf)
{
for(int i = 0; i != 16; i++) { printf("%02x", (uint)(((unsigned char *)hash)[i])); }
if(crlf) printf("\n");
}

union md5hash
{
uint ui[4]; //= 16 bytes --> 128 bits (allows the message to be divided into chunks due to union structure)
char ch[16]; //= 16 bytes --> 128 bits
};


md5hash single_md5(char* ptext)
{
md5hash h;

char w[64] = {0}; //Declare an empty array
//for(int i = 0; ptext[i]; i++) w[i] = ptext[i];
strncpy(w, ptext, 56); //Copy over the text to be hashed; this effectively "zero pads" the string (limiting to 56 - my choice)
md5_prep(w);
MD5_CPU((uint*)&w[0], h.ui[0], h.ui[1], h.ui[2], h.ui[3]); //Hash the message

return h;
}

//Test
int main(void){
//Declarations
std::string target = "Hello";

char tes[120] = {0};
tes[0] = 'h';
tes[1] = 'e';
for(int i =0; tes[i]; i++)
printf("%c", tes[i]);
printf("\n");

//Execute
md5hash test = single_md5(tes);
print_md5(test.ui, false);

return 0;
}

因此,如果我想将输入的哈希值与输入字符串的计算哈希值进行比较,我可以这样做

   a == target[0] && b == target[1] && c == target[2] && d == target[3]

其中 a,b,c,dui[1], ui[2], ui[3], ui[4]

但是,我需要以某种方式将哈希字符串转换为 union 定义的那种形式。

最佳答案

您的问题似乎是用户输入了哈希的 32 位文本(十六进制)表示,而您需要原始数据。因此,您必须在两者之间进行转换:

int hex2int(char ch)
{
if (isdigit(ch))
return ch - '0';

switch (ch) {
case 'a': case 'A': return 10;
case 'b': case 'B': return 11;
case 'c': case 'C': return 12;
case 'd': case 'D': return 13;
case 'e': case 'E': return 14;
case 'f': case 'F': return 15;
default: abort(); // shan't happen
}
}

char md5_hex2raw(char raw[16], const char hex[32])
{
for (int i = 0; i < 32; i += 2) {
raw[i] = (hex2int(hex[0]) << 4) | hex2int(hex[1]);
}
}

然后您可以调用转换函数,以便它直接复制到 union 中:

md5hash hash;
md5_hex2raw(hash.ch, user_entered_string);

关于c - 将 char 数组分配给 union 数据类型时,我的编译器抛出错误 : "conversion from char* to non-scalar type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20309489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com