【问题标题】:how to get md5sum command and get the string output in code如何获取 md5sum 命令并在代码中获取字符串输出
【发布时间】:2013-12-05 11:31:28
【问题描述】:

我正在用 C++ 编写代码,我希望得到某个字符串的 md5 校验和。 我知道我可以运行system("echo "myStirng" | md5sum > some_file.txt") 然后读取文件。 我正在为嵌入式系统编写代码,由于性能问题,我不想使用文件操作。另外,我不能在我的代码中使用任何 openSSL 函数。 我进行了搜索,我遇到的所有答案要么包括一些文件操作,要么使用了 openSSL 函数。 有没有办法在不使用文件操作和不使用 openSSL 的情况下获取 md5 校验和?可能将 linux 命令 md5sum 作为与 C 函数 system 之类的代码不同的进程运行,但返回校验和十六进制字符串而不是返回代码。

谢谢。

【问题讨论】:

标签: c++ linux unix


【解决方案1】:

这是我前段时间写的概念证明。很像@slugonmission 评论中链接中的第二个答案,但我认为要好一些(使用fread)。实际上,最后我更喜欢使用库的方法更快一点。因此,我建议您是否可以依赖 openssl 并且性能对于更喜欢该库很重要。

FILE* pipe = popen("md5sum /home/bla.txt", "r");                                                                                       
if (pipe != NULL)                                                                                                                                                    
{                                                                                                                                                     
  static const unsigned md5size = 32;                                                                                                                 
  unsigned char md5res[md5size + 1];                                                                                                                  
  int readSize = fread((void*) md5res, sizeof(char), md5size, pipe);                                                                                  
  if (readSize != md5size)                                                                                                                            
  {                                                                                                                                                    
      std::cout << "Error reading md5" << std::endl;                                                                                                     
      return 1;                                                                                                                                          
  }                                                                                                                                                    
  pclose(pipe);                                                                                                                        
  md5res[md5size] = 0;    
 }                                                                                                                            

【讨论】:

  • 感谢您的回答。但是这个答案包括文件,你认为有选项可以不使用文件来解决它,也许使用 md5sum linux 命令以某种方式?
【解决方案2】:

使用http://hashlib2plus.sourceforge.net/ 然后运行类似:

#include <hashlibpp.h>
#include <string>

int main() {
    hashwrapper *rap = new md5wrapper();
    std::string hash = rap->getHashFromFile("README.TXT");
}

【讨论】:

  • 感谢您的帮助,但我无法使用此库。
猜你喜欢
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-27
  • 1970-01-01
相关资源
最近更新 更多