Skip to content

Commit

Permalink
Add example/hash2sum.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Oct 19, 2024
1 parent 8867076 commit 9265e92
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 10 deletions.
1 change: 1 addition & 0 deletions example/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
project : default-build release <link>static ;

exe md5sum : md5sum.cpp ;
exe hash2sum : hash2sum.cpp ;
136 changes: 136 additions & 0 deletions example/hash2sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright 2024 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#define _CRT_SECURE_NO_WARNINGS

#include <boost/hash2/md5.hpp>
#include <boost/hash2/sha1.hpp>
#include <boost/hash2/sha2.hpp>
#include <boost/hash2/ripemd.hpp>
#include <boost/mp11.hpp>
#include <array>
#include <string>
#include <cerrno>
#include <cstdio>

template<std::size_t N> std::string result_to_string( std::array<unsigned char, N> const & v )
{
std::string r;

for( std::size_t i = 0; i < N; ++i )
{
char buffer[ 8 ];
std::snprintf( buffer, sizeof( buffer ), "%02x", static_cast<int>( v[ i ] ) );

r += buffer;
}

return r;
}

template<class Hash> void hash2sum( std::FILE* f, char const* fn )
{
Hash hash;

int const N = 4096;
unsigned char buffer[ N ];

for( ;; )
{
std::size_t n = std::fread( buffer, 1, N, f );

if( std::ferror( f ) )
{
std::fprintf( stderr, "'%s': read error: %s\n", fn, std::strerror( errno ) );
return;
}

if( n == 0 ) break;

hash.update( buffer, n );
}

std::string digest = result_to_string( hash.result() );

std::printf( "%s *%s\n", digest.c_str(), fn );
}

template<class Hash> void hash2sum( char const* fn )
{
std::FILE* f = std::fopen( fn, "rb" );

if( f == 0 )
{
std::fprintf( stderr, "'%s': open error: %s\n", fn, std::strerror( errno ) );
}
else
{
hash2sum<Hash>( f, fn );
std::fclose( f );
}
}

using namespace boost::mp11;
using namespace boost::hash2;

using hashes = mp_list<

md5_128,
sha1_160,
sha2_256,
sha2_224,
sha2_512,
sha2_384,
sha2_512_256,
sha2_512_224,
ripemd_160

>;

constexpr char const* names[] = {

"md5_128",
"sha1_160",
"sha2_256",
"sha2_224",
"sha2_512",
"sha2_384",
"sha2_512_256",
"sha2_512_224",
"ripemd_160"

};

int main( int argc, char const* argv[] )
{
if( argc < 2 )
{
std::fputs( "usage: hash2sum <hash> <files...>\n", stderr );
return 2;
}

std::string hash( argv[1] );
bool found = false;

mp_for_each< mp_iota<mp_size<hashes>> >([&](auto I){

if( hash == names[ I ] )
{
using Hash = mp_at_c<hashes, I>;

for( int i = 2; i < argc; ++i )
{
hash2sum<Hash>( argv[i] );
}

found = true;
}

});

if( !found )
{
std::fprintf( stderr, "hash2sum: unknown hash name '%s'\n", hash.c_str() );
}
}
1 change: 0 additions & 1 deletion example/md5sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ template<std::size_t N> std::string result_to_string( std::array<unsigned char,
for( std::size_t i = 0; i < N; ++i )
{
char buffer[ 8 ];

std::snprintf( buffer, sizeof( buffer ), "%02x", static_cast<int>( v[ i ] ) );

r += buffer;
Expand Down
22 changes: 13 additions & 9 deletions test/Jamfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017-2023 Peter Dimov
# Copyright 2017-2024 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.

import testing ;
Expand Down Expand Up @@ -51,10 +51,13 @@ run xxhash.cpp ;

run md5.cpp ;
run hmac_md5.cpp ;

run sha1.cpp ;
run sha2.cpp ;
run hmac_sha1.cpp ;

run sha2.cpp ;
run hmac_sha2.cpp ;

run ripemd.cpp ;

# legacy
Expand All @@ -70,13 +73,14 @@ run plaintext_leak.cpp ;
run multiple_result.cpp ;
run integral_result.cpp ;

# compile benchmarks
# benchmarks

compile ../benchmark/buffer.cpp ;
compile ../benchmark/unordered.cpp ;
compile ../benchmark/average.cpp ;
compile ../benchmark/keys.cpp ;
link ../benchmark/buffer.cpp ;
link ../benchmark/unordered.cpp ;
link ../benchmark/average.cpp ;
link ../benchmark/keys.cpp ;

# compile examples
# examples

compile ../example/md5sum.cpp ;
link ../example/md5sum.cpp ;
link ../example/hash2sum.cpp ;

0 comments on commit 9265e92

Please sign in to comment.