Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pi-314159 authored Jun 17, 2024
1 parent d0d30b7 commit 0ca8a3c
Show file tree
Hide file tree
Showing 22 changed files with 49 additions and 80 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 PI
Copyright (c) 2024 PI (github.com/pi-314159)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Author: pi-314159@GitHub
# License: MIT
# Last Updated: 2024-06-10

CXX ?= g++
DEFAULT_FLAGS := -std=c++20 -O3 -Wall
Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ This is a TOTP (Time-Based One-Time Password) authenticator that adheres to [RFC

## Build

Before building, ensure that you have OpenSSL library 3.0 or higher installed and that your compiler supports C++20. By default, the executable is located in the `build` folder.
Before building, ensure that you have OpenSSL library installed and that your compiler supports C++20. By default, the executable is located in the `build` folder.

### Windows

If you're on Windows, you can install OpenSSL using vcpkg. Follow these steps:
By default, Visual Studio Release build will statically link dependencies. You can install OpenSSL using vcpkg. Follow these steps:

1. Install OpenSSL using vcpkg:
```bat
vcpkg.exe install openssl:x64-windows
```
or
```bat
vcpkg.exe install openssl
vcpkg.exe install openssl:x64-windows-static
```
2. Integrate vcpkg with Visual Studio:
Expand Down
6 changes: 6 additions & 0 deletions VisualStudio/Authenticator.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<PreferredToolArchitecture>x64</PreferredToolArchitecture>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
Expand All @@ -52,6 +53,7 @@
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<UseOfMfc>Static</UseOfMfc>
<PreferredToolArchitecture>x64</PreferredToolArchitecture>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down Expand Up @@ -81,6 +83,9 @@
<IntDir>$(SolutionDir)\..\tmp\</IntDir>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);$(SolutionDir)\..\src\include;</IncludePath>
</PropertyGroup>
<PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<VcpkgUseStatic>true</VcpkgUseStatic>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
Expand Down Expand Up @@ -141,6 +146,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>false</GenerateDebugInformation>
<StackReserveSize>8388608</StackReserveSize>
<AdditionalDependencies>AdvAPI32.Lib;Crypt32.Lib;User32.Lib;WS2_32.Lib</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
28 changes: 7 additions & 21 deletions src/Authenticator.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#include <actions/add.h>
#include <actions/generatetotp.h>
#include <tools/crypto.h>
#include <tools/file_io.h>
#include <tools/generateiv.h>
#include <tools/vectortostring.h>
#include <tools/string.h>

Expand All @@ -18,7 +16,7 @@

int main(int argc, char* argv[]) {
std::cout << "=========================== PI Authenticator ===========================\n"
"= Last Updated: 2024-06-10 =\n"
"= Last Updated: 2024-06-17 =\n"
"= License: MIT =\n"
"= GitHub Repository: github.com/pi-314159/Authenticator =\n"
"========================================================================\n\n" << std::endl;
Expand Down Expand Up @@ -106,7 +104,7 @@ int main(int argc, char* argv[]) {
tOTPInstance.period = std::stoul(lineElement);
break;
case 3:
tOTPInstance.alg = lineElement[0];
tOTPInstance.alg = lineElement;
break;
case 4:
tOTPInstance.digits = std::stoi(lineElement);
Expand Down Expand Up @@ -140,7 +138,7 @@ int main(int argc, char* argv[]) {
while (action[0] != 'q') {
STARTLOOP:
if (action[0] == 'g') {
std::cout << "OTP: " << ACTIONS::GenerateTOTP(tOTPObjects[selectedOTPIndex]) << "\n" << std::endl;
std::cout << "OTP: " << ACTIONS::GenerateTotp(tOTPObjects[selectedOTPIndex]) << "\n" << std::endl;
}
else if (action[0] == 'l') {
if (tOTPObjectsSize > 0) {
Expand All @@ -159,15 +157,9 @@ int main(int argc, char* argv[]) {
if (tOTPObjectsSize > 1) {
tOTPObjects.erase(tOTPObjects.begin() + selectedOTPIndex);
action[0] = 'l';
unsigned char* iV = new unsigned char[16];
TOOLS::GenerateIV(iV);
auto toWrite = std::make_unique<std::string>("OK" + TOOLS::VectorToString(tOTPObjects));
auto encryptedContents = std::make_unique_for_overwrite<std::string>();
unsigned long long int encryptedContentsSize = 0;
crypto->Aes(key, iV, *toWrite, *encryptedContents, encryptedContentsSize);
fileIo->WriteBinary(*encryptedContents, iV);
encryptedContents.reset(); toWrite.reset();
delete[] iV;
fileIo->WriteBinary(*toWrite, key);
toWrite.reset();
}
else {
tOTPObjects.clear();
Expand All @@ -181,15 +173,9 @@ int main(int argc, char* argv[]) {
ACTIONS::Add(tOTPObjects);
action[0] = 'l';
++tOTPObjectsSize;
unsigned char* iV = new unsigned char[16];
TOOLS::GenerateIV(iV);
auto toWrite = std::make_unique<std::string>("OK" + TOOLS::VectorToString(tOTPObjects));
auto encryptedContents = std::make_unique_for_overwrite<std::string>();
unsigned long long int encryptedContentsSize = 0;
crypto->Aes(key, iV, *toWrite, *encryptedContents, encryptedContentsSize);
fileIo->WriteBinary(*encryptedContents, iV);
encryptedContents.reset(); toWrite.reset();
delete[] iV;
fileIo->WriteBinary(*toWrite, key);
toWrite.reset();
std::cout << "Added.\n" << std::endl;
goto STARTLOOP;
}
Expand Down
11 changes: 5 additions & 6 deletions src/actions/add.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#include <actions/add.h>
#include <tools/string.h>
Expand All @@ -12,14 +11,14 @@ namespace ACTIONS {
void Add(std::vector<tOTP_object>& tOTPObjects) {
auto str = std::make_unique_for_overwrite<TOOLS::String>();
std::string inputBuffer, tag, secret;
char alg = '1';
std::string alg = "sha1";
unsigned int period;
unsigned short digits;
while (inputBuffer.empty()) {
str->Input(inputBuffer, "Please enter the issuer (e.g., GitHub), then hit Enter TWICE to proceed:", true);
str->FormatInput(inputBuffer, inputBuffer);
}
tag = inputBuffer + "::";
tag = inputBuffer + ":";
inputBuffer = "";
while (inputBuffer.empty()) {
str->Input(inputBuffer, "Please enter the account (e.g., [email protected]), \nthen hit Enter TWICE to proceed:", true);
Expand All @@ -44,11 +43,11 @@ namespace ACTIONS {
}
str->Input(inputBuffer, "Please select the hash algorithm:\n[1] SHA-1\n[2] SHA-256\n[3] SHA-384\n[5] SHA-512\nEnter your choice (default 1):");
if (inputBuffer == "2") {
alg = '2';
alg = "sha256";
} else if (inputBuffer == "3") {
alg = '3';
alg = "sha384";
} else if (inputBuffer == "5") {
alg = '5';
alg = "sha512";
}
str->Input(inputBuffer, "Please enter the digits (between 6 - 10, default 6):");
if (str->FormatInput(inputBuffer, digits, '2')) {
Expand Down
4 changes: 1 addition & 3 deletions src/actions/generatetotp.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#include <actions/generatetotp.h>

#include <tools/base32decode.h>
#include <tools/crypto.h>

#include <chrono>
#include <cmath>

namespace ACTIONS {
std::string GenerateTOTP(tOTP_object& tOTP) {
std::string GenerateTotp(tOTP_object& tOTP) {
auto secretSize = static_cast<int>(floor(tOTP.secret.size() / 1.6));
unsigned char* decodedSecret = new unsigned char[secretSize];
TOOLS::Base32Decode(tOTP.secret, decodedSecret);
Expand Down
1 change: 0 additions & 1 deletion src/include/actions/add.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#ifndef ACTIONS__ADD_H_
#define ACTIONS__ADD_H_
Expand Down
3 changes: 1 addition & 2 deletions src/include/actions/generatetotp.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#ifndef ACTIONS__GENERATETOTP_H_
#define ACTIONS__GENERATETOTP_H_

#include <totp.h>

namespace ACTIONS {
std::string GenerateTOTP(tOTP_object& tOTP);
std::string GenerateTotp(tOTP_object& tOTP);
}

#endif
1 change: 0 additions & 1 deletion src/include/tools/base32decode.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#ifndef TOOLS__BASE32DECODE_H_
#define TOOLS__BASE32DECODE_H_
Expand Down
3 changes: 1 addition & 2 deletions src/include/tools/crypto.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#ifndef TOOLS__CRYPTO_H_
#define TOOLS__CRYPTO_H_
Expand All @@ -10,7 +9,7 @@
namespace TOOLS {
class Crypto {
public:
void CalculateHmac(char hashAlg, unsigned char* key, int& keySize, unsigned long long int msg, unsigned char* result, unsigned int* resultSize);
void CalculateHmac(std::string hashAlg, unsigned char* key, int& keySize, unsigned long long int msg, unsigned char* result, unsigned int* resultSize);
void Aes(unsigned char* key, unsigned char* iv, std::string& input, std::string& output, unsigned long long int& outputSize, bool encrypt = true, unsigned short int bufferSize = 1024);
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/include/tools/file_io.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#ifndef TOOLS__FILE_IO_H_
#define TOOLS__FILE_IO_H_
Expand All @@ -15,7 +14,7 @@ namespace TOOLS {
public:
File_Io(std::unique_ptr<std::string>& s);
bool ReadBinary(std::string& binaryFileContent);
bool WriteBinary(std::string& binaryFileContent, unsigned char* iV, unsigned int iVSize = 16);
bool WriteBinary(std::string& binaryFileContent, unsigned char key[], unsigned int iVSize = 16);
bool DeleteFile();
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/include/tools/generateiv.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#ifndef TOOLS__GENERATEIV_H_
#define TOOLS__GENERATEIV_H_

namespace TOOLS {
void GenerateIV(unsigned char* iV, unsigned int iVSize = 16);
void GenerateIv(unsigned char* iV, unsigned int iVSize = 16);
}

#endif
1 change: 0 additions & 1 deletion src/include/tools/string.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#ifndef TOOLS__STRING_H_
#define TOOLS__STRING_H_
Expand Down
1 change: 0 additions & 1 deletion src/include/tools/vectortostring.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#ifndef TOOLS__VECTORTOSTRING_H_
#define TOOLS__VECTORTOSTRING_H_
Expand Down
3 changes: 1 addition & 2 deletions src/include/totp.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#ifndef TOTP_H_
#define TOTP_H_
Expand All @@ -12,7 +11,7 @@ struct tOTP_object {
std::string tag;
std::string secret;
unsigned int period;
char alg;
std::string alg;
unsigned short digits;
};

Expand Down
1 change: 0 additions & 1 deletion src/tools/base32decode.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#include <tools/base32decode.h>

Expand Down
14 changes: 5 additions & 9 deletions src/tools/crypto.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#include <tools/crypto.h>

Expand All @@ -10,18 +9,15 @@
#include <ranges>

namespace TOOLS {
void Crypto::CalculateHmac(char hashAlg, unsigned char* key, int& keySize, unsigned long long int msg, unsigned char* result, unsigned int* resultSize) {
void Crypto::CalculateHmac(std::string hashAlg, unsigned char* key, int& keySize, unsigned long long int msg, unsigned char* result, unsigned int* resultSize) {
auto hashAlgorithm = EVP_sha1();
switch (hashAlg) {
case '2':
if (hashAlg == "sha256") {
hashAlgorithm = EVP_sha256();
break;
case '3':
} else if (hashAlg == "sha384") {
hashAlgorithm = EVP_sha384();
break;
case '5':
}
else if (hashAlg == "sha512") {
hashAlgorithm = EVP_sha512();
break;
}

unsigned char message[8] = {'0'};
Expand Down
24 changes: 13 additions & 11 deletions src/tools/file_io.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Author: pi-314159@GitHub
// License: MIT
// Last updated: 2024-06-10

#include <tools/crypto.h>
#include <tools/file_io.h>
#include <tools/generateiv.h>

#include <filesystem>
#include <fstream>
Expand All @@ -19,22 +20,23 @@ namespace TOOLS {
return true;
}

bool File_Io::WriteBinary(std::string& binaryFileContent, unsigned char* iV, unsigned int iVSize) {
bool File_Io::WriteBinary(std::string& binaryFileContent, unsigned char key[], unsigned int iVSize) {
unsigned char* iV = new unsigned char[16];
GenerateIv(iV);
std::string encryptedContents;
unsigned long long int encryptedContentsSize = 0;
auto crypto = std::make_unique_for_overwrite<TOOLS::Crypto>();
crypto->Aes(key, iV, binaryFileContent, encryptedContents, encryptedContentsSize);
crypto.reset();
std::ofstream writeBinaryFileHandle(filePath, std::ios::binary);
writeBinaryFileHandle.write((char*)iV, iVSize);
writeBinaryFileHandle.write(binaryFileContent.c_str(), binaryFileContent.size());
writeBinaryFileHandle.write(encryptedContents.c_str(), encryptedContentsSize);
writeBinaryFileHandle.close();
delete[] iV;
return true;
}

bool File_Io::DeleteFile() {
bool ret = true;
try {
std::filesystem::remove(filePath);
}
catch (...) {
ret = false;
}
return ret;
return std::filesystem::remove(filePath);
}
}
Loading

0 comments on commit 0ca8a3c

Please sign in to comment.