gpt4 book ai didi

c - 围绕 MD5 代码的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:24:54 24 4
gpt4 key购买 nike

当我运行此 md5 代码时,它在运行时输入的最大长度为 64 个字符。每当我给出超过 64 个字符时,它就会显示

Inconsistency detected by ld.so: dl-fini.c: 205: _dl_fini: Assertion ns != 0 || i == nloaded failed!

我需要散列将近 10kb 的输入(仅字符串)。我需要更改头文件中的任何内容吗?谁能告诉我解决方案?

md5.h

#ifndef HEADER_MD5_H
#define HEADER_MD5_H

#include <openssl/e_os2.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef OPENSSL_NO_MD5
#error MD5 is disabled.
#endif

/*
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* ! MD5_LONG has to be at least 32 bits wide. If it's wider, then !
* ! MD5_LONG_LOG2 has to be defined along. !
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/

#if defined(__LP64__)
#define MD5_LONG unsigned long
#elif defined(OPENSSL_SYS_CRAY) || defined(__ILP64__)
#define MD5_LONG unsigned long
#define MD5_LONG_LOG2 3
/*
* _CRAY note. I could declare short, but I have no idea what impact
* does it have on performance on none-T3E machines. I could declare
* int, but at least on C90 sizeof(int) can be chosen at compile time.
* So I've chosen long...
* <appro@fy.chalmers.se>
*/
#else
#define MD5_LONG unsigned long
#endif

#define MD5_CBLOCK 64
#define MD5_LBLOCK (MD5_CBLOCK/2)
#define MD5_DIGEST_LENGTH 16

typedef struct MD5state_st
{
MD5_LONG A,B,C,D;
MD5_LONG Nl,Nh;
MD5_LONG data[MD5_LBLOCK];
unsigned int num;
} MD5_CTX;

#ifdef OPENSSL_FIPS
int private_MD5_Init(MD5_CTX *c);
#endif
int MD5_Init(MD5_CTX *c);
int MD5_Update(MD5_CTX *c, const void *data, size_t len);
int MD5_Final(unsigned char *md, MD5_CTX *c);
unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md);
void MD5_Transform(MD5_CTX *c, const unsigned char *b);
#ifdef __cplusplus
}
#endif

#endif

md5.c

  #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "md5.h"

char *pt(char *, int );
int main(int argc, char **argv)
{
char *in;
char *out;
printf("ENter the string\n");
scanf("%[^\n]s",in);
size_t len; //unsigned long len; size_t len;
len = printf("len is %d\n",strlen(in));
out = pt(in, len);
printf("MD5 is\t: %s\n", out);
free(out);
//return 0;
}


char *pt(char *str, int length)
{
int n;
MD5_CTX c;
unsigned char digest[16];
char *output = (char*)malloc(33);

MD5_Init(&c);

MD5_Update(&c, str, length);

MD5_Final(digest, &c);

for (n = 0; n < 16; ++n)
{
sprintf(&output[n*2], "%02x", (unsigned int)digest[n]);
}

return output;
}

最佳答案

问题一

对于这个语句:

scanf("%[^\n]s",in);

当我使用 -Wall 标志编译它时,我收到警告:

warning: 'in' is used uninitialized in this function [-Wuninitialized]
scanf("%[^\n]s",in);
^

如您所见,in 并未指向内存中的任何位置,因此您首先需要使用数组或 malloc() 分配一些内存:

char in[500]; //or a higher value
char *out;
printf("Enter the string\n");
scanf("%499[^\n]s", in);
printf("\nin = .%s.\n", in);

char *in;
char *out;
in = malloc(500); //or a higher value
printf("Enter the string\n");
scanf("%499[^\n]s", in);
printf("\nin = .%s.\n", in);

可能的问题2

您正在将 printf() 的返回分配给变量 len

len = printf("len is %d\n",strlen(in));

返回值 printf :

Upon successful return, it returns the number of characters printed (excluding the null byte used to end output to strings).

假设您希望变量 len 包含字符串 in 的长度,而不是 printf("len is %d\n",strlen(in)),您可能希望分配来自 strlen() 的返回值第一:

len = strlen(in);
printf("len is %d\n", len);

关于c - 围绕 MD5 代码的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31072240/

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