使用文件路径和哈希类型的标准输入运行 certutil -hashfile (c++)

问题描述

第一次发帖。 我正在尝试编写一个简单的程序,该程序从标准输入中获取文件路径和哈希类型,并使用 certutil 输出相应的哈希。稍后我想将哈希值与用户输入的哈希值进行比较,并输出通过或失败语句。

#include <iostream>
#include <string>

using namespace std;

int main() {

string hash_type;
string path_to_file;

cout<<"Enter full file path to hash: "<<endl;
cin>>path_to_file;
cout<<"\nFilepath is: "<<path_to_file;
cout<<"\nWhich hash to use? (e.g SHA256)"<<endl;
cin>>hash_type;
cout<<"\nHash type is: "<<hash_type<<endl;

无论是否使用 cout,我都尝试过;

cout<<system("certutil -hashfile path_to_file hash_type");  

system("certutil -hashfile C:\Users\moose\Desktop\current.cpp SHA256")

这会运行得很好,我看到了结果哈希。我似乎无法使用标准输入中的字符串。我怀疑这可能与文件路径或其他分隔符中所需的额外反斜杠有关?

return 0;
}

我仍在学习指针,但我也尝试了以下方法; system("certutil -hashfile *path_to_file *hash_type"); 因为我收到以下错误

Certutil:-hashfile 命令失败:0x80070002 Certutil: 系统找不到指定的文件。 'hash_type' 不是内部或外部命令,也不是可运行的程序或批处理文件

我将不胜感激。提前致谢。

解决方法

我已经开始工作了。

string hash_args = "certutil -hashfile " + path_to_file + " " + hash_type;
system(hash_args.c_str());

这很有帮助: cannot convert 'std::basic_string<char>' to 'const char*' for argument '1' to 'int system(const char*)'