Browse Source

Inital commit

master
blank X 4 months ago
commit
66b0be4f32
Signed by: blankie GPG Key ID: CC15FC822C7F61F5
  1. 1
      .gitignore
  2. 7
      CMakeLists.txt
  3. 21
      LICENSE
  4. 4
      build
  5. 61
      main.c

1
.gitignore

@ -0,0 +1 @@
builddir

7
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)

21
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.

4
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"

61
main.c

@ -0,0 +1,61 @@
#include <stdio.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#define CHUNK_SIZE 8 * 1024
int main(int argc, char *argv[]) {
if (argc == 0) {
fprintf(stderr, "Usage: streamhash <algorithm>\n");
return 1;
}
if (argc != 2) {
fprintf(stderr, "Usage: %s <algorithm>\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;
}
Loading…
Cancel
Save