forked from Consti10/wifibroadcast
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filesystem functions moved into separate file
- Loading branch information
Showing
8 changed files
with
75 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// Created by consti10 on 13.08.23. | ||
// | ||
|
||
#include "EncryptionFsUtils.hpp" | ||
|
||
#include <sodium/crypto_box.h> | ||
|
||
#include <cassert> | ||
#include <cstring> | ||
#include <iostream> | ||
|
||
#include <spdlog/spdlog.h> | ||
#include "../wifibroadcast_spdlog.hpp" | ||
|
||
int wb::write_keypair_to_file(const wb::KeyPairTxRx& keypair_txrx, | ||
const std::string& filename) { | ||
FILE *fp; | ||
if ((fp = fopen(filename.c_str(), "w")) == nullptr) { | ||
std::cerr<<"Unable to save "<<filename<<std::endl; | ||
assert(false); | ||
return 1; | ||
} | ||
assert(fwrite(keypair_txrx.key_1.secret_key.data(), crypto_box_SECRETKEYBYTES, 1, fp)==1); | ||
assert(fwrite(keypair_txrx.key_1.public_key.data(), crypto_box_PUBLICKEYBYTES, 1, fp)==1); | ||
assert(fwrite(keypair_txrx.key_2.secret_key.data(), crypto_box_SECRETKEYBYTES, 1, fp)==1); | ||
assert(fwrite(keypair_txrx.key_2.public_key.data(), crypto_box_PUBLICKEYBYTES, 1, fp)==1); | ||
fclose(fp); | ||
return 0; | ||
} | ||
|
||
wb::KeyPairTxRx wb::read_keypair_from_file(const std::string& filename) { | ||
KeyPairTxRx ret{}; | ||
FILE *fp; | ||
if ((fp = fopen(filename.c_str(), "r")) == nullptr) { | ||
std::cerr<<fmt::format("Unable to open {}: {}", filename.c_str(), strerror(errno))<<std::endl; | ||
assert(false); | ||
} | ||
assert(fread(ret.key_1.secret_key.data(), crypto_box_SECRETKEYBYTES, 1, fp)==1); | ||
assert(fread(ret.key_1.public_key.data(), crypto_box_PUBLICKEYBYTES, 1, fp)==1); | ||
assert(fread(ret.key_2.secret_key.data(), crypto_box_SECRETKEYBYTES, 1, fp)==1); | ||
assert(fread(ret.key_2.public_key.data(), crypto_box_PUBLICKEYBYTES, 1, fp)==1); | ||
fclose(fp); | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef ENCRYPTION_FS_UTILS_HPP | ||
#define ENCRYPTION_FS_UTILS_HPP | ||
|
||
#include "KeyPairTxRx.hpp" | ||
#include <string> | ||
|
||
namespace wb { | ||
|
||
/** | ||
* Saves the KeyPairTxRx as a raw file | ||
*/ | ||
int write_keypair_to_file(const KeyPairTxRx& keypair_txrx, | ||
const std::string& filename); | ||
|
||
/** | ||
* Reads a raw KeyPairTxRx from a raw file previusly generated. | ||
*/ | ||
KeyPairTxRx read_keypair_from_file(const std::string& filename); | ||
|
||
} // namespace wb end | ||
|
||
#endif // ENCRYPTION_FS_UTILS_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters