timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
md5.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25#pragma once
26
27#include <array>
28#include <cstdint>
29#include <cstring>
30#include <iostream>
31#include <string>
32
33namespace tim
34{
35namespace md5
36{
37// helper function
39compute_md5(const std::string& inp);
40
41// a small class for calculating MD5 hashes of strings or byte arrays
42//
43// usage: 1) feed it blocks of uchars with update()
44// 2) finalize()
45// 3) get hexdigest() string
46// or
47// MD5(std::string).hexdigest()
48//
49// assumes that char is 8 bit and int is 32 bit
50class md5sum
51{
52public:
53 using size_type = uint32_t; // must be 32bit
54 static constexpr int blocksize = 64;
55
56 md5sum(const std::string& text);
57 md5sum() = default;
58 ~md5sum() = default;
59 md5sum(const md5sum&) = default;
60 md5sum(md5sum&&) = default;
61
62 md5sum& operator=(const md5sum&) = default;
63 md5sum& operator=(md5sum&&) = default;
64
65 md5sum& update(const unsigned char* buf, size_type length);
66 md5sum& update(const char* buf, size_type length);
68 std::string hexdigest() const;
69 friend std::ostream& operator<<(std::ostream&, md5sum md5);
70
71private:
72 void transform(const uint8_t block[blocksize]);
73
74 bool finalized = false;
75 // 64bit counter for number of bits (lo, hi)
76 std::array<uint32_t, 2> count = { 0, 0 };
77 std::array<uint8_t, blocksize> buffer{}; // overflow bytes from last 64 byte chunk
78 // digest so far, initialized to magic initialization constants.
79 std::array<uint32_t, 4> state = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };
80 std::array<uint8_t, 16> digest{}; // result
81};
82} // namespace md5
83} // namespace tim
84
86
87#if defined(TIMEMORY_UTILITY_HEADER_MODE)
89#endif
md5sum & operator=(const md5sum &)=default
uint32_t size_type
Definition: md5.hpp:53
md5sum(md5sum &&)=default
md5sum & update(const unsigned char *buf, size_type length)
Definition: md5.cpp:288
md5sum()=default
~md5sum()=default
friend std::ostream & operator<<(std::ostream &, md5sum md5)
Definition: md5.cpp:395
std::string hexdigest() const
Definition: md5.cpp:378
md5sum & operator=(md5sum &&)=default
md5sum(const md5sum &)=default
static constexpr int blocksize
Definition: md5.hpp:54
md5sum & finalize()
Definition: md5.cpp:338
TIMEMORY_UTILITY_INLINE std::string compute_md5(const std::string &inp)
Definition: md5.cpp:404
Definition: kokkosp.cpp:39
tim::mpl::apply< std::string > string
Definition: macros.hpp:53