From 66b0be4f3250d4a9f0413873a6cf2ab0e0a4f462 Mon Sep 17 00:00:00 2001 From: blank X Date: Sun, 8 Aug 2021 22:01:42 +0700 Subject: [PATCH] Inital commit --- .gitignore | 1 + CMakeLists.txt | 7 ++++++ LICENSE | 21 +++++++++++++++++ build | 4 ++++ main.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 LICENSE create mode 100755 build create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a57078d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +builddir diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7c7610a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.20.2) +project(streamhash C) + +find_package(OpenSSL REQUIRED) + +add_executable(${PROJECT_NAME} main.c) +target_link_libraries(${PROJECT_NAME} OpenSSL::SSL) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..211185a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 blank X + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/build b/build new file mode 100755 index 0000000..4a0a5ed --- /dev/null +++ b/build @@ -0,0 +1,4 @@ +#!/bin/sh +test "$1" = "debug" && TYPE="Debug" || TYPE="Release" +echo "Building in $TYPE" +cmake -S . -B builddir -D CMAKE_BUILD_TYPE=$TYPE && cd builddir && cmake --build . --config "$TYPE" diff --git a/main.c b/main.c new file mode 100644 index 0000000..e479fa7 --- /dev/null +++ b/main.c @@ -0,0 +1,61 @@ +#include +#include +#include + +#define CHUNK_SIZE 8 * 1024 + +int main(int argc, char *argv[]) { + if (argc == 0) { + fprintf(stderr, "Usage: streamhash \n"); + return 1; + } + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + const EVP_MD *type = EVP_get_digestbyname(argv[1]); + if (!type) { + fprintf(stderr, "Invalid algorithm: %s\n", argv[1]); + return 1; + } + EVP_MD_CTX *ctx = EVP_MD_CTX_new(); + if (EVP_DigestInit(ctx, type) != 1) { + ERR_print_errors_fp(stderr); + return 1; + } + char chunk[CHUNK_SIZE]; + char loop = 1; + while (loop) { + size_t size = fread(chunk, 1, CHUNK_SIZE, stdin); + if (size != CHUNK_SIZE) { + if (feof(stdin)) + loop = 0; + else { + EVP_MD_CTX_free(ctx); + perror("fread"); + return 1; + } + } + if (fwrite(chunk, 1, size, stdout) != size) { + EVP_MD_CTX_free(ctx); + perror("fwrite"); + return 1; + } + if (EVP_DigestUpdate(ctx, chunk, size) != 1) { + EVP_MD_CTX_free(ctx); + ERR_print_errors_fp(stderr); + return 1; + } + } + unsigned char md[EVP_MAX_MD_SIZE]; + unsigned int size; + if (EVP_DigestFinal(ctx, md, &size) != 1) { + ERR_print_errors_fp(stderr); + EVP_MD_CTX_free(ctx); + return 1; + } + for (unsigned int i=0; i < size; i++) + fprintf(stderr, "%02x", md[i]); + fprintf(stderr, "\n"); + return 0; +}