Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
HashFactory.cpp
Go to the documentation of this file.
1 //===-- serialbox/core/hash/HashFactory.cpp -----------------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
20 #include <sstream>
21 
22 #include <iostream>
23 
24 namespace serialbox {
25 
26 std::unique_ptr<Hash> HashFactory::create(const std::string& name) {
27  if(name == MD5::Name) {
28  return std::make_unique<MD5>();
29  } else if(name == SHA256::Name) {
30  return std::make_unique<SHA256>();
31  } else {
32  std::stringstream ss;
33  ss << "cannot create Hash '" << name << "': hash does not exist or is not registred.\n";
34  ss << "Registered hashes:\n";
35  for(const auto& hash : HashFactory::registeredHashes())
36  ss << " " << hash << "\n";
37  throw Exception(ss.str().c_str());
38  }
39 }
40 
41 std::vector<std::string> HashFactory::registeredHashes() {
42  std::vector<std::string> hashes{MD5::Name, SHA256::Name};
43  return hashes;
44 }
45 
46 std::string HashFactory::defaultHash() {
47 #ifdef SERIALBOX_HAS_OPENSSL
48  return MD5::Name;
49 #else
50  return SHA256::Name;
51 #endif
52 }
53 
54 } // namespace serialbox
static std::string defaultHash()
Get the default hash algorithm (currently MD5 if avialable, SHA256 otherwise)
Definition: HashFactory.cpp:46
static const char * Name
Identifier of the hash.
Definition: SHA256.h:31
static std::unique_ptr< Hash > create(const std::string &name)
Construct an instance of the Hash name
Definition: HashFactory.cpp:26
Namespace of the serialbox library.
Definition: Archive.h:25
static std::vector< std::string > registeredHashes()
Get a vector of strings of the registered hashes.
Definition: HashFactory.cpp:41
static const char * Name
Identifier of the hash.
Definition: MD5.h:31
Exception class which stores a human-readable error description.
Definition: Exception.h:30