地穴(3)导致分割故障
crypt(3) causing segmentation fault
我正在尝试制作一个小程序来打开文件,读取每一行,使用crypt(3)
算法哈希,然后将其写回输出文件。
但是,每当我尝试使用crypt()
方法时,它都会导致段故障。谁能告诉我我在做什么错?谢谢。
命令我用来编译代码:
g++ hasher.cpp -o hasher -lcrypt
我的代码:
#include <iostream> // User I/O
#include <fstream> // File I/O
#include <vector> // String array
#include <cstdlib> // Exit method
#include <crypt.h> // Crypt(3)
// Input & Output file names
std::string input_file;
std::string output_file;
// Plaintext & Hashed passwords
std::vector<std::string> passwords;
// Read input and output files
void read_file_names()
{
std::cout << "Input: ";
std::getline(std::cin, input_file);
std::cout << "Output: ";
std::getline(std::cin, output_file);
}
// Load passwords from input file
void load_passwords()
{
// Line / Hash declarations
std::string line;
std::string hash;
// Declare files
std::ifstream f_input;
std::ifstream f_output;
// Open files
f_input.open(input_file.c_str());
// Check if file can be opened
if (!f_input) {
std::cout << "Failed to open " << input_file << " for reading." << std::endl;
std::exit(1);
}
// Read all lines from file
while(getline(f_input, line))
{
// This line causes a segmentation fault
// I have no idea why
hash = crypt(line.c_str(), "");
std::cout << "Hashed [" << hash << "] " << line << std::endl;
}
}
// Main entry point of the app
int main()
{
read_file_names();
load_passwords();
return 0;
}
呼叫crypt()(盐)的第二个参数取一个字符串。您应该传递一个至少2个字符的字符串,以便使用它(如手册中)。例如:crypt(line.c_str(), "Any string here");
相关文章:
- 分割故障C++矩阵
- C ,二进制树的高度,而不是检查我的子树是否为空,而是在检查我的子树是否是叶子节点.抛出分割故障
- 我正在为此代码分割故障
- 在执行strcpy时获取分割故障错误
- 空隙指针分割故障
- Xcode会产生分割故障,而不是例外
- OPENCV C 中的分割故障误差
- 分割故障:核心倾倒C 矢量对字符串:
- C 2D数组导致分割故障(核心倾倒)
- 分割故障/错误读数变量-C 列表
- DLSYM导致分割故障
- 使用C 中的操作员删除的分割故障
- 使用Linux X64下的Libmozjs-52(SpidermonKey)进行分割故障
- 使用长变量时分割故障(11)错误
- C - 返回字符串时的分割故障
- 创建VertexArray时的分割故障
- 向量分割故障的向量
- 从std :: fileSystem ::路径对象的类中退出时的分割故障
- C 矢量分割故障
- 基于向量的数据格式的分割故障误差