Thrill  0.1
sha512.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/digest/sha512.hpp
3  *
4  * Public domain implementation of SHA-512 (SHA-2) processor. Copied from
5  * https://github.com/kalven/sha-2, which is based on LibTomCrypt.
6  *
7  * Part of tlx - http://panthema.net/tlx
8  *
9  * Copyright (C) 2018 Timo Bingmann <[email protected]>
10  *
11  * All rights reserved. Published under the Boost Software License, Version 1.0
12  ******************************************************************************/
13 
14 #ifndef TLX_DIGEST_SHA512_HEADER
15 #define TLX_DIGEST_SHA512_HEADER
16 
17 #include <cstdint>
18 #include <string>
19 
20 namespace tlx {
21 
22 //! \addtogroup tlx_digest
23 //! \{
24 
25 /*!
26  * SHA-512 processor without external dependencies.
27  */
28 class SHA512
29 {
30 public:
31  //! construct empty object.
32  SHA512();
33  //! construct context and process data range
34  SHA512(const void* data, uint32_t size);
35  //! construct context and process string
36  explicit SHA512(const std::string& str);
37 
38  //! process more data
39  void process(const void* data, uint32_t size);
40  //! process more data
41  void process(const std::string& str);
42 
43  //! digest length in bytes
44  static constexpr size_t kDigestLength = 64;
45 
46  //! finalize computation and output 64 byte (512 bit) digest
47  void finalize(void* digest);
48 
49  //! finalize computation and return 64 byte (512 bit) digest
51  //! finalize computation and return 64 byte (512 bit) digest hex encoded
53  //! finalize computation and return 64 byte (512 bit) digest upper-case hex
55 
56 private:
57  uint64_t length_;
58  uint64_t state_[8];
59  uint32_t curlen_;
60  uint8_t buf_[128];
61 };
62 
63 //! process data and return 64 byte (512 bit) digest hex encoded
64 std::string sha512_hex(const void* data, uint32_t size);
65 //! process data and return 64 byte (512 bit) digest hex encoded
67 
68 //! process data and return 64 byte (512 bit) digest upper-case hex encoded
69 std::string sha512_hex_uc(const void* data, uint32_t size);
70 //! process data and return 64 byte (512 bit) digest upper-case hex encoded
72 
73 //! \}
74 
75 } // namespace tlx
76 
77 #endif // !TLX_DIGEST_SHA512_HEADER
78 
79 /******************************************************************************/
void finalize(void *digest)
finalize computation and output 64 byte (512 bit) digest
Definition: sha512.cpp:205
uint8_t buf_[128]
Definition: sha512.hpp:60
std::string digest()
finalize computation and return 64 byte (512 bit) digest
Definition: sha512.cpp:239
std::string digest_hex()
finalize computation and return 64 byte (512 bit) digest hex encoded
Definition: sha512.cpp:245
std::string digest_hex_uc()
finalize computation and return 64 byte (512 bit) digest upper-case hex
Definition: sha512.cpp:251
uint64_t state_[8]
Definition: sha512.hpp:58
uint64_t length_
Definition: sha512.hpp:57
SHA512()
construct empty object.
Definition: sha512.cpp:146
void process(const void *data, uint32_t size)
process more data
Definition: sha512.cpp:167
std::string sha512_hex(const void *data, uint32_t size)
process data and return 64 byte (512 bit) digest hex encoded
Definition: sha512.cpp:257
SHA-512 processor without external dependencies.
Definition: sha512.hpp:28
static constexpr size_t kDigestLength
digest length in bytes
Definition: sha512.hpp:44
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
uint32_t curlen_
Definition: sha512.hpp:59
std::string sha512_hex_uc(const void *data, uint32_t size)
process data and return 64 byte (512 bit) digest upper-case hex encoded
Definition: sha512.cpp:265