- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
让我解释一下我的程序的整体功能。用户可以输入一个字符串,然后根据 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,d
是 ui[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/
这个问题在这里已经有了答案: Why don't Java's +=, -=, *=, /= compound assignment operators require casting? (11 个
当我尝试运行以下代码时,List(.of) 无法编译并给出主题错误。 package collections; import java.util.LinkedHashSet; import java.
我正在尝试编译使用 ChatScript 库的程序。这是我在名为 main.cpp 的文件中的代码: #include #include "common.h" using namespace std
我想在我的程序中外部使用 ChatScript。在documents它说: Embedding Step #1 First, you will need to modify `common.h and
假设我有一个 char,我想用一行代码将其 strcat() 转换为 char 数组。对于 [一个非实用的] 示例: strcat("ljsdflusdfg",getchar()); 或者我想做相反的
我有以下类型签名: *Main Lib> let f :: a -> a -> a -> a; f = undefined *Main Lib> let x :: Char; x = undefin
我正在学习如何在 C 中使用指针(使用 malloc 和 free),但我在这个练习中遇到了一些麻烦。我只想制作一个指针数组,我想在其中保存每个单词的方向。然后我想为一个特定的词做一个 free(),
我有一个字符*: char* version = "10.5.108"; 我想通过字符分隔符获取两个新的 char*。 char delimiter = '.'; 执行以下代码后: printf("|
最近在学习Cpp,今天在学习使用Clion做测试的时候,发生了奇怪的事情。 这是我的代码 int main() { char c = 'b'; char carr[1]{'a'};
我对 c 很陌生,我正在审查一些代码。我遇到了这个: static char * fromDataType; static char * toDataType; static char * fromR
我有一个像这样的动态结构: struct network { int count; char** ips; } 如果我知道每个字符串数组都是 16 个字节(即 INET_ADDRSTR
我有一个旧程序,其中使用了一些库函数,但我没有那个库。 所以我正在使用 C++ 库编写该程序。在那个旧代码中有一些函数是这样调用的 *string = newstrdup("这里有一些字符串"); 字
我正在编写一个函数,该函数接受 ArrayList,然后将每个 char[] 复制到另一个增加长度的 char[] 中,然后将新的 char[] 添加到新的 ArrayList 中。当我尝试复制数组时
我正在寻找 map >并生成每个可能的 map从它。 我知道这可能会占用大量内存并需要一些时间。 每个map需要包含每个字母 a-z,并映射到唯一的 a-z 字符。 IE。啊bjcp迪EVfh嘎血红蛋
#define NAME_LEN 20 #include "stdio.h" #include "stdlib.h" #include "string.h" #pragma warning(disab
所以我必须创建一个函数来找到一对带有第一个字母并返回第二个字母的函数。 我实际上找到了一个答案,但是使用 map 功能却找不到。 lookUp :: Char -> [(Char, Cha
我最近接受采访并要求写mystrcat(*s1, *s2, *s3) 其中s1 和s2 是源字符串连接结果由 s3 给出。有人告诉我,不要担心 s3 的内存分配,并假设 s1 和 s2 不是空/无效字
今天我与一位同事讨论了他(对我来说)不寻常的“main”函数签名。他喜欢这样声明: int main(int argc, char* (*argv)[]) { printf("at index
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: What's the difference between new char[10] and new cha
通常字符串文字是 const char[] 类型。但是当我把它当作其他类型时,我得到了奇怪的结果。 unsigned char *a = "\355\1\23"; 使用此编译器会抛出警告“初始化中的指
我是一名优秀的程序员,十分优秀!