File indexing completed on 2025-01-05 04:37:31

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef BTSHA1HASHGEN_H
0007 #define BTSHA1HASHGEN_H
0008 
0009 #include "constants.h"
0010 #include "sha1hash.h"
0011 #include <ktorrent_export.h>
0012 
0013 class QCryptographicHash;
0014 
0015 namespace bt
0016 {
0017 /**
0018  * @author Joris Guisson
0019  *
0020  * Generates a SHA1 hash, code based on wikipedia's pseudocode
0021  * There are 2 ways to use this class :
0022  * - generate : all data is present from the start
0023  * - start, update and end : data can be delivered in chunks
0024  *
0025  * Mixing the 2, is not a good idea
0026  */
0027 class KTORRENT_EXPORT SHA1HashGen
0028 {
0029 public:
0030     SHA1HashGen();
0031     ~SHA1HashGen();
0032 
0033     /**
0034      * Generate a hash from a bunch of data.
0035      * @param data The data
0036      * @param len The length
0037      * @return The SHA1 hash
0038      */
0039     SHA1Hash generate(const Uint8 *data, Uint32 len);
0040 
0041     /**
0042      * Start SHA1 hash generation in chunks.
0043      */
0044     void start();
0045 
0046     /**
0047      * Update the hash.
0048      * @param data The data
0049      * @param len Length of the data
0050      */
0051     void update(const Uint8 *data, Uint32 len);
0052 
0053     /**
0054      * All data has been delivered, calculate the final hash.
0055      * @return
0056      */
0057     void end();
0058 
0059     /**
0060      * Get the hash generated.
0061      */
0062     SHA1Hash get() const;
0063 
0064 private:
0065     QCryptographicHash *h;
0066     Uint8 result[20];
0067 };
0068 
0069 }
0070 
0071 #endif