diff options
Diffstat (limited to 'libqpdf/qpdf')
-rw-r--r-- | libqpdf/qpdf/MD5.hh | 70 | ||||
-rw-r--r-- | libqpdf/qpdf/MD5_native.hh | 92 | ||||
-rw-r--r-- | libqpdf/qpdf/QPDFCrypto_native.hh | 10 |
3 files changed, 95 insertions, 77 deletions
diff --git a/libqpdf/qpdf/MD5.hh b/libqpdf/qpdf/MD5.hh new file mode 100644 index 00000000..d04a89b4 --- /dev/null +++ b/libqpdf/qpdf/MD5.hh @@ -0,0 +1,70 @@ +#ifndef MD5_HH +#define MD5_HH + +#include <qpdf/DLL.h> +#include <qpdf/Types.h> +#include <qpdf/QPDFCryptoImpl.hh> +#include <string> +#include <memory> + +class MD5 +{ + public: + typedef unsigned char Digest[16]; + + QPDF_DLL + MD5(); + QPDF_DLL + void reset(); + + // encodes string and finalizes + QPDF_DLL + void encodeString(char const* input_string); + + // encodes file and finalizes; offset < 0 reads whole file + QPDF_DLL + void encodeFile(char const* filename, qpdf_offset_t up_to_offset = -1); + + // appends string to current md5 object + QPDF_DLL + void appendString(char const* input_string); + + // appends arbitrary data to current md5 object + QPDF_DLL + void encodeDataIncrementally(char const* input_data, size_t len); + + // computes a raw digest + QPDF_DLL + void digest(Digest); + + // prints the digest to stdout terminated with \r\n (primarily for + // testing) + QPDF_DLL + void print(); + + // returns the digest as a hexadecimal string + QPDF_DLL + std::string unparse(); + + // Convenience functions + QPDF_DLL + static std::string getDataChecksum(char const* buf, size_t len); + QPDF_DLL + static std::string getFileChecksum(char const* filename, + qpdf_offset_t up_to_offset = -1); + QPDF_DLL + static bool checkDataChecksum(char const* const checksum, + char const* buf, size_t len); + QPDF_DLL + static bool checkFileChecksum(char const* const checksum, + char const* filename, + qpdf_offset_t up_to_offset = -1); + + private: + void init(); + void finalize(); + + std::shared_ptr<QPDFCryptoImpl> crypto; +}; + +#endif // MD5_HH diff --git a/libqpdf/qpdf/MD5_native.hh b/libqpdf/qpdf/MD5_native.hh index 02cdb3cf..d7e65a50 100644 --- a/libqpdf/qpdf/MD5_native.hh +++ b/libqpdf/qpdf/MD5_native.hh @@ -1,94 +1,32 @@ -#ifndef MD5_HH -#define MD5_HH +#ifndef MD5_NATIVE_HH +#define MD5_NATIVE_HH -#include <qpdf/DLL.h> #include <qpdf/qpdf-config.h> -#include <qpdf/Types.h> -#ifdef HAVE_INTTYPES_H -# include <inttypes.h> -#endif -#ifdef HAVE_STDINT_H -# include <stdint.h> -#endif -#include <string> +#include <cstdint> +#include <cstring> -class MD5 +class MD5_native { public: typedef unsigned char Digest[16]; - QPDF_DLL - MD5(); - QPDF_DLL - void reset(); - - // encodes string and finalizes - QPDF_DLL - void encodeString(char const* input_string); - - // encodes file and finalizes; offset < 0 reads whole file - QPDF_DLL - void encodeFile(char const* filename, qpdf_offset_t up_to_offset = -1); - - // appends string to current md5 object - QPDF_DLL - void appendString(char const* input_string); - - // appends arbitrary data to current md5 object - QPDF_DLL - void encodeDataIncrementally(char const* input_data, size_t len); - - // computes a raw digest - QPDF_DLL - void digest(Digest); - - // prints the digest to stdout terminated with \r\n (primarily for - // testing) - QPDF_DLL - void print(); - - // returns the digest as a hexadecimal string - QPDF_DLL - std::string unparse(); - - // Convenience functions - QPDF_DLL - static std::string getDataChecksum(char const* buf, size_t len); - QPDF_DLL - static std::string getFileChecksum(char const* filename, - qpdf_offset_t up_to_offset = -1); - QPDF_DLL - static bool checkDataChecksum(char const* const checksum, - char const* buf, size_t len); - QPDF_DLL - static bool checkFileChecksum(char const* const checksum, - char const* filename, - qpdf_offset_t up_to_offset = -1); - - private: - // POINTER defines a generic pointer type - typedef void *POINTER; - - // UINT2 defines a two byte word - typedef uint16_t UINT2; - - // UINT4 defines a four byte word - typedef uint32_t UINT4; - + MD5_native(); void init(); void update(unsigned char *, size_t); - void final(); + void finalize(); + void digest(Digest); - static void transform(UINT4 [4], unsigned char [64]); - static void encode(unsigned char *, UINT4 *, size_t); - static void decode(UINT4 *, unsigned char *, size_t); + private: + static void transform(uint32_t[4], unsigned char[64]); + static void encode(unsigned char *, uint32_t *, size_t); + static void decode(uint32_t *, unsigned char *, size_t); - UINT4 state[4]; // state (ABCD) - UINT4 count[2]; // number of bits, modulo 2^64 (lsb first) + uint32_t state[4]; // state (ABCD) + uint32_t count[2]; // number of bits, modulo 2^64 (lsb first) unsigned char buffer[64]; // input buffer bool finalized; Digest digest_val; }; -#endif // MD5_HH +#endif // MD5_NATIVE_HH diff --git a/libqpdf/qpdf/QPDFCrypto_native.hh b/libqpdf/qpdf/QPDFCrypto_native.hh index 785d6c6f..77179075 100644 --- a/libqpdf/qpdf/QPDFCrypto_native.hh +++ b/libqpdf/qpdf/QPDFCrypto_native.hh @@ -3,6 +3,8 @@ #include <qpdf/DLL.h> #include <qpdf/QPDFCryptoImpl.hh> +#include <qpdf/MD5_native.hh> +#include <memory> class QPDFCrypto_native: public QPDFCryptoImpl { @@ -11,6 +13,14 @@ class QPDFCrypto_native: public QPDFCryptoImpl QPDF_DLL virtual ~QPDFCrypto_native() = default; + + virtual void MD5_init(); + virtual void MD5_update(unsigned char const* data, size_t len); + virtual void MD5_finalize(); + virtual void MD5_digest(MD5_Digest); + + private: + std::shared_ptr<MD5_native> md5; }; #endif // QPDFCRYPTO_NATIVE_HH |