1 #ifndef Corrade_Utility_Sha1_h 2 #define Corrade_Utility_Sha1_h 3 /* 4 This file is part of Corrade. 5 6 Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 7 2017, 2018, 2019, 2020 Vladimír Vondruš <mosra@centrum.cz> 8 Copyright © 2019 Jonathan Hale <squareys@googlemail.com> 9 10 Permission is hereby granted, free of charge, to any person obtaining a 11 copy of this software and associated documentation files (the "Software"), 12 to deal in the Software without restriction, including without limitation 13 the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 and/or sell copies of the Software, and to permit persons to whom the 15 Software is furnished to do so, subject to the following conditions: 16 17 The above copyright notice and this permission notice shall be included 18 in all copies or substantial portions of the Software. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** @file 30 * @brief Class @ref Corrade::Utility::Sha1 31 */ 32 33 #include "Corrade/Containers/Containers.h" 34 #include "Corrade/Utility/AbstractHash.h" 35 #include "Corrade/Utility/StlForwardString.h" 36 #include "Corrade/Utility/visibility.h" 37 38 namespace Corrade { namespace Utility { 39 40 /** 41 @brief SHA-1 42 43 Implementation of the [Secure Hash Algorithm 1](https://en.wikipedia.org/wiki/SHA-1). 44 Example usage: 45 46 @snippet Utility.cpp Sha1-usage 47 */ 48 class CORRADE_UTILITY_EXPORT Sha1: public AbstractHash<20> { 49 public: 50 /** 51 * @brief Digest of given data 52 * 53 * Convenience function for @cpp (Utility::Sha1{} << data).digest() @ce. 54 */ digest(const std::string & data)55 static Digest digest(const std::string& data) { 56 return (Sha1() << data).digest(); 57 } 58 59 explicit Sha1(); 60 61 /** @brief Add data for digesting */ 62 Sha1& operator<<(Containers::ArrayView<const char> data); 63 64 /** @overload */ 65 Sha1& operator<<(const std::string& data); 66 67 /** 68 * @brief @cpp operator<< @ce with C strings is not allowed 69 * 70 * To clarify your intent with handling the @cpp '\0' @ce delimiter, 71 * cast to @ref Containers::ArrayView or @ref std::string instead. 72 */ 73 Sha1& operator<<(const char*) = delete; 74 75 /** @brief Digest of all added data */ 76 Digest digest(); 77 78 private: 79 CORRADE_UTILITY_LOCAL void processChunk(const char* data); 80 81 char _buffer[128]; 82 std::size_t _bufferSize = 0; 83 unsigned long long _dataSize = 0; 84 unsigned int _digest[5]; 85 }; 86 87 }} 88 89 #endif 90