Browse Source

Minor project restructuring

master
Jake Wakeling 2 months ago
parent
commit
64956cdd9a
7 changed files with 106 additions and 126 deletions
  1. 21
    22
      CMakeLists.txt
  2. 21
    27
      src/base64/base64.c
  3. 11
    1
      src/base64/base64.h
  4. 0
    38
      src/global.h
  5. 12
    0
      src/ohash.h
  6. 31
    37
      src/sha/sha.c
  7. 10
    1
      src/sha/sha.h

+ 21
- 22
CMakeLists.txt View File

@@ -1,43 +1,42 @@
# CMakeLists.txt
# CMakeLists file for oHash

cmake_minimum_required(VERSION 3.9) # CMake Minimum Required Version
project(oHash) # Project
set(CMAKE_C_STANDARD 11) # C Standard

# Output Directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

include_directories( # Include Directories
C:/Include/
)

link_directories( # Library Directories
/lib/x64/
C:/Libraries/x64/
)

file(GLOB SOURCES_BASE64 # Add Source Files
src/base64.*
src/global.h
)
file(GLOB SOURCES_SHA # Add Source Files
src/sha.*
src/global.h
# Source Globs
file(GLOB SOURCES_BASE64 # Base64 Sources
${CMAKE_SOURCE_DIR}/src/base64/*
${CMAKE_SOURCE_DIR}/src/ohash.h
)

add_executable(base64 ${SOURCES_BASE64}) # Build Target
target_include_directories(base64 PUBLIC # Target Include Directories
/include/
C:/Include/
file(GLOB SOURCES_SHA # SHA Sources
${CMAKE_SOURCE_DIR}/src/sha/*
${CMAKE_SOURCE_DIR}/src/ohash.h
)
target_link_libraries(base64 # Target Libraries

# Targets
add_executable(base64 ${SOURCES_BASE64}) # Base64 Build Target
add_executable(sha ${SOURCES_SHA}) # SHA Build Target

# Target Libraries
target_link_libraries(base64
getopt
)

add_executable(sha ${SOURCES_SHA}) # Build Target
target_include_directories(sha PUBLIC # Target Include Directories
/include/
C:/Include/
)
target_link_libraries(sha # Target Libraries
target_link_libraries(sha
getopt
)

src/base64.c → src/base64/base64.c View File

@@ -9,29 +9,20 @@

#define _CRT_SECURE_NO_WARNINGS

#include <getopt.h>
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "global.h"
#include "base64.h"

char *B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int main(int argc, char *argv[]) {
byte_t flags = 0x00;
uint8_t flags = 0x00;
if (argc > 1) {
int opt;
static struct option longOptions[] = {
{ "file", ARG_NONE, NULL, 'f' },
{ "encode", ARG_NONE, NULL, 'e' },
{ "decode", ARG_NONE, NULL, 'd' },
{ GETOPT_HELP_OPTION },
{ GETOPT_VERSION_OPTION },
{ "help", ARG_NONE, NULL, 'h' },
{ "version", ARG_NONE, NULL, 'v' },
{ ARG_NULL, 0, ARG_NULL, 0 }
};

@@ -43,8 +34,8 @@ int main(int argc, char *argv[]) {
case 'f': { flags |= 0x80; break; } // File flag
case 'e': { flags |= 0x40; break; } // Encode flag
case 'd': { flags |= 0x20; break; } // Decode flag
case GETOPT_HELP_CHAR: { PrintHelp(); break; }
case GETOPT_VERSION_CHAR: { PrintVersion(); break; }
case 'h': { PrintHelp(); break; }
case 'v': { PrintVersion(); break; }
case '?': { break; } // Getopt error
default: { PrintHelp(); break; }
}
@@ -82,29 +73,32 @@ int main(int argc, char *argv[]) {

// Print usage message
void PrintUsage(void) {
printf("Usage: %s [-fhv] <-e | -d> message\n", programName);
printf("Try '%s --help' for more information", programName);
printf("Usage: %s [-fhv] <-e | -d> message\n", PROGRAM_NAME);
printf("Try '%s --help' for more information", PROGRAM_NAME);
printf("\n");
return;
}

// Print help message
void PrintHelp(void) {
printf("Usage: %s [-fhv] <-e | -d> message\n", programName);
printf("Usage: %s [-fhv] <-e | -d> message\n", PROGRAM_NAME);
printf("Options:\n");
printf(" -f, --file Encode or decode a file instead of a string\n");
printf(" -e, --encode Encode selected cryptographic function\n");
printf(" -d, --decode Decode selected cryptographic function\n");
printf(" --help Show help\n");
printf(" --version Show version information\n");
printf("\n");
return;
}

// Print version information
void PrintVersion(void) {
printf("%s (%s) %s\n", programName, oHashName, oHashVersion);
printf("%s (%s) %s\n", PROGRAM_NAME, oHashName, oHashVersion);
printf("Copyright (C) 2019, Jakob Wakeling\n");
printf("All rights reserved.\n");
printf("OMKOV Open Source Licence\n");
printf("\n");
return;
}

@@ -112,13 +106,13 @@ void PrintVersion(void) {
// src: Pointer to source block
// dst: Pointer to destination block, concatenate result here
// len: Block length, for base64 padding
void EncodeBlock(byte_t src[], byte_t *dst, int len) {
byte_t block[5]; // Base64 working block
void EncodeBlock(uint8_t src[], uint8_t *dst, int len) {
uint8_t block[5]; // Base64 working block
block[0] = B64[src[0] >> 2];
block[1] = B64[((src[0] & 0x03) << 4) | ((src[1] & 0xF0) >> 4)];
block[2] = (byte_t)(len > 1 ? B64[((src[1] & 0x0F) << 2) |
block[2] = (uint8_t)(len > 1 ? B64[((src[1] & 0x0F) << 2) |
((src[2] & 0xC0) >> 6)] : '=');
block[3] = (byte_t)(len > 2 ? B64[src[2] & 0x3F] : '=');
block[3] = (uint8_t)(len > 2 ? B64[src[2] & 0x3F] : '=');
block[4] = '\0';
strncat(dst, block, sizeof(block)); // Concatenate processed block
return;
@@ -130,12 +124,12 @@ void EncodeBlock(byte_t src[], byte_t *dst, int len) {
void EncodeBase64(char *strsrc, char *b64dst) {
b64dst[0] = '\0'; // Clear destination
for (int c = 0; strsrc[c] != 0;) { // Loop through string
byte_t block[3]; // Working block
uint8_t block[3]; // Working block
int len = 0; // Working block length

for (int i = 0; i < 3; ++i) {
if (strsrc[c]) { // Populate working block
block[i] = (byte_t)strsrc[c];
block[i] = (uint8_t)strsrc[c];
++len; ++c;
}
else { block[i] = 0; } // Pad block if string complete
@@ -151,8 +145,8 @@ void EncodeBase64(char *strsrc, char *b64dst) {
// Decode 4 6-bit characters into 3 8-bit binary bytes
// src: Pointer to source block
// dst: Pointer to destination block, concatenate result here
void DecodeBlock(byte_t src[], byte_t *dst) {
byte_t block[4]; // Base64 working block
void DecodeBlock(uint8_t src[], uint8_t *dst) {
uint8_t block[4]; // Base64 working block
block[0] = src[0] << 2 | src[1] >> 4;
block[1] = src[1] << 4 | src[2] >> 2;
block[2] = src[2] << 6 | src[3] >> 0;
@@ -170,7 +164,7 @@ void DecodeBase64(char *b64src, char *strdst) {
int phase = 0;

for (int i = 0; b64src[i] != 0; ++i) { // Loop through base64
byte_t block[4]; // Working block
uint8_t block[4]; // Working block

//for (int j = 0; j < 3; ++j) {
// int c = b64src[i];

src/base64.h → src/base64/base64.h View File

@@ -6,7 +6,17 @@
#ifndef BASE64_H
#define BASE64_H

#define programName "base64"
#define PROGRAM_NAME "base64"

#include <getopt.h>
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../ohash.h"

void PrintUsage(void);
void PrintHelp(void);

+ 0
- 38
src/global.h View File

@@ -1,38 +0,0 @@
// global.h
// Global header file for oHash
// Copyright (C) 2019, Jakob Wakeling
// All rights reserved.

#ifndef GLOBAL_H
#define GLOBAL_H

// Software information
#ifdef _UNICODE
#define oHashName L"oHash"
#define oHashVersion L"0.1"

enum {
GETOPT_HELP_CHAR = L'h',
GETOPT_VERSION_CHAR = L'v',
};

#define GETOPT_HELP_OPTION L"help", ARG_NONE, NULL, GETOPT_HELP_CHAR
#define GETOPT_VERSION_OPTION L"version", ARG_NONE, NULL, GETOPT_VERSION_CHAR
#else
#define oHashName "oHash"
#define oHashVersion "0.1"

enum {
GETOPT_HELP_CHAR = 'h',
GETOPT_VERSION_CHAR = 'v',
};

#define GETOPT_HELP_OPTION "help", ARG_NONE, NULL, GETOPT_HELP_CHAR
#define GETOPT_VERSION_OPTION "version", ARG_NONE, NULL, GETOPT_VERSION_CHAR
#endif

// Types
typedef unsigned char byte_t;
typedef unsigned int uint_t;

#endif // GLOBAL_H

+ 12
- 0
src/ohash.h View File

@@ -0,0 +1,12 @@
// ohash.h
// Global header file for oHash
// Copyright (C) 2019, Jakob Wakeling
// All rights reserved.

#ifndef OHASH_H
#define OHASH_H

#define oHashName "oHash"
#define oHashVersion "0.1.0"

#endif // OHASH_H

src/sha.c → src/sha/sha.c View File

@@ -8,46 +8,37 @@
// 0x000A, Memory Allocation Error

#define _CRT_SECURE_NO_WARNINGS
#define _UNICODE

#include <getopt.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "global.h"
#include "sha.h"

int main(int argc, wchar_t *argv[]) {
byte_t flags = 0x00;
int main(int argc, char *argv[]) {
uint8_t flags = 0x00;
if (argc > 1) {
int opt;
static struct option longOptions[] = {
{ L"help", ARG_NONE, NULL, L'h' },
{ L"version", ARG_NONE, NULL, L'v' },
{ L"file", ARG_NONE, NULL, L'f' },
{ GETOPT_HELP_OPTION },
{ GETOPT_VERSION_OPTION },
{ "help", ARG_NONE, NULL, 'h' },
{ "version", ARG_NONE, NULL, 'v' },
{ "file", ARG_NONE, NULL, 'f' },
{ "help", ARG_NONE, NULL, 'h' },
{ "version", ARG_NONE, NULL, 'v' },
{ ARG_NULL, 0, ARG_NULL, 0 }
};

int optionIndex = 0;
while ((opt = getoptLongA(argc, argv, "hvf",
while ((opt = getopt_long(argc, argv, "hvf",
longOptions, &optionIndex)) != -1) {
switch (opt) {
case 0: { break; } // Flag is not null
case L'f': { flags |= 0x80; break; } // File flag
case GETOPT_HELP_CHAR: { PrintHelp(); break; }
case GETOPT_VERSION_CHAR: { PrintVersion(); break; }
case L'?': { break; } // Getopt error
case 'f': { flags |= 0x80; break; } // File flag
case 'h': { PrintHelp(); break; }
case 'v': { PrintVersion(); break; }
case '?': { break; } // Getopt error
default: { PrintHelp(); break; }
}
}

if (optind < argc) {
byte_t *sha1 = malloc(sizeof(byte_t) * 21);
uint8_t *sha1 = malloc(sizeof(uint8_t) * 21);
if (sha1 == NULL) { return 0x000A; }

else {
@@ -65,32 +56,35 @@ int main(int argc, wchar_t *argv[]) {

// Print usage message
void PrintUsage(void) {
wprintf(L"Usage: %s [-fhv] message\n", programName);
wprintf(L"Try '%s --help' for more information", programName);
printf("Usage: %s [-fhv] message\n", programName);
printf("Try '%s --help' for more information", programName);
printf("\n");
return;
}

// Print help message
void PrintHelp(void) {
wprintf(L"Usage: %s [-fhv] message\n", programName);
wprintf(L"Options:\n");
wprintf(L" -f, --file Encode or decode a file instead of a string\n");
wprintf(L" --help Show help\n");
wprintf(L" --version Show version information\n");
printf("Usage: %s [-fhv] message\n", programName);
printf("Options:\n");
printf(" -f, --file Encode or decode a file instead of a string\n");
printf(" --help Show help\n");
printf(" --version Show version information\n");
printf("\n");
return;
}

// Print version information
void PrintVersion(void) {
wprintf(L"%s (%s) %s\n", programName, oHashName, oHashVersion);
wprintf(L"Copyright (C) 2019, Jakob Wakeling\n");
wprintf(L"All rights reserved.\n");
wprintf(L"OMKOV Open Source Licence\n");
printf("%s (%s) %s\n", programName, oHashName, oHashVersion);
printf("Copyright (C) 2019, Jakob Wakeling\n");
printf("All rights reserved.\n");
printf("OMKOV Open Source Licence\n");
printf("\n");
return;
}

// Debug Function for HashSHA1
void PrintMsg(byte_t *msg, size_t msgSize) {
void PrintMsg(uint8_t *msg, size_t msgSize) {
for (int i = 0; i < msgSize; ++i) {
printf("%02X ", msg[i]);
}
@@ -142,9 +136,9 @@ int HashSHA1(char *input, char *hash) {
size_t len; // Message length in bytes
for (len = 0; input[len] != '\0'; ++len);
size_t msgLen = len * 8; // Message length in bits
size_t msgSize = sizeof(byte_t) * (len);// Message size in bytes (dynamic)
size_t msgSize = sizeof(uint8_t) * (len);// Message size in bytes (dynamic)

byte_t *msg = malloc(++msgSize); // Allocate message memory
uint8_t *msg = malloc(++msgSize); // Allocate message memory
if (msg == NULL) { return 0x0001; }

// Preprocessing
@@ -161,7 +155,7 @@ int HashSHA1(char *input, char *hash) {
uint64_t *msgLenS = malloc(8); // Allocate memory for swapped len
if (msgLenS == NULL) { return 0x0001; }

*msgLenS = swap64(msgLen);
*msgLenS = swap64((size_t *)msgLen);

msg = realloc(msg, msgSize += 8); // Expand message memory by 8 bytes
if (msg == NULL) { return 0x0001; }

src/sha.h → src/sha/sha.h View File

@@ -6,7 +6,16 @@
#ifndef SHA_H
#define SHA_H

#define programName L"sha"
#define programName "sha"

#include <getopt.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../ohash.h"

void PrintUsage(void);
void PrintHelp(void);

Loading…
Cancel
Save