Я пытаюсь создать небольшую программу, которая открывает файл, читает каждую строку, хеширует эту строку с использованием алгоритма 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;
}
hash = crypt(line.c_str(), "");
- person Paradoxis   schedule 22.06.2016line.c_str()
выглядит при вызовеcrypt
? Это не NULL? Попробуйте добавитьstd::cout << "line: " << line.c_str() << std::endl;
непосредственно перед вызовомcrypt
. Также обратите внимание, что если вы используетеgcc
, то у вас есть есть отладчик — он называется gdb. - person Paul R   schedule 22.06.2016