Core: Reworked hashes
-Removed HashDigest class (replaced by ByteArray) -Removed Hashable/Hash classes (replaced by ComputeHash function and Hashable template struct) -Fixed ByteArray operator<< -Renamed File::GetHash to File::ComputeHash Former-commit-id: cc5eaf2d4c88a556878190b8205e66713512e2d2
This commit is contained in:
parent
86da939520
commit
21f223f1c7
|
|
@ -8,10 +8,12 @@
|
|||
#define NAZARA_ABSTRACTHASH_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Enums.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class HashDigest;
|
||||
class ByteArray;
|
||||
|
||||
class NAZARA_CORE_API AbstractHash
|
||||
{
|
||||
|
|
@ -23,10 +25,15 @@ namespace Nz
|
|||
|
||||
virtual void Append(const UInt8* data, unsigned int len) = 0;
|
||||
virtual void Begin() = 0;
|
||||
virtual HashDigest End() = 0;
|
||||
virtual ByteArray End() = 0;
|
||||
|
||||
virtual unsigned int GetDigestLength() const = 0;
|
||||
virtual const char* GetHashName() const = 0;
|
||||
|
||||
AbstractHash& operator=(const AbstractHash&) = delete;
|
||||
AbstractHash& operator=(AbstractHash&&) = default;
|
||||
|
||||
static std::unique_ptr<AbstractHash> Get(HashType hash);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,15 +8,24 @@
|
|||
#define NAZARA_ALGORITHM_CORE_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Enums.hpp>
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class AbstractHash;
|
||||
class ByteArray;
|
||||
|
||||
template<typename F, typename Tuple> auto Apply(F&& fn, Tuple&& t);
|
||||
template<typename O, typename F, typename Tuple> auto Apply(O& object, F&& fn, Tuple&& t);
|
||||
template<typename T> ByteArray ComputeHash(HashType hash, const T& v);
|
||||
template<typename T> ByteArray ComputeHash(AbstractHash* hash, const T& v);
|
||||
template<typename T> void HashCombine(std::size_t& seed, const T& v);
|
||||
|
||||
template<typename T>
|
||||
struct Hashable;
|
||||
|
||||
template<typename T>
|
||||
struct TypeTag {};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
// Merci à Ryan "FullMetal Alchemist" Lahfa
|
||||
// Merci aussi à Freedom de siteduzero.com
|
||||
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
|
|
@ -41,6 +42,21 @@ namespace Nz
|
|||
|
||||
return Detail::ApplyImplMethod(object, std::forward<F>(fn), std::forward<Tuple>(t), std::make_index_sequence<tSize>());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ByteArray ComputeHash(HashType hash, const T& v)
|
||||
{
|
||||
return ComputeHash(AbstractHash::Get(hash).get(), v);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ByteArray ComputeHash(AbstractHash* hash, const T& v)
|
||||
{
|
||||
hash->Begin();
|
||||
Hashable<T>()(v, hash);
|
||||
return hash->End();
|
||||
}
|
||||
|
||||
// Algorithme venant de CityHash par Google
|
||||
// http://stackoverflow.com/questions/8513911/how-to-create-a-good-hash-combine-with-64-bit-output-inspired-by-boosthash-co
|
||||
template<typename T>
|
||||
|
|
|
|||
|
|
@ -8,8 +8,6 @@
|
|||
#define NAZARA_BYTEARRAY_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/Hashable.hpp>
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <vector>
|
||||
|
||||
|
|
@ -17,7 +15,7 @@ namespace Nz
|
|||
{
|
||||
class AbstractHash;
|
||||
|
||||
class NAZARA_CORE_API ByteArray : public Hashable
|
||||
class NAZARA_CORE_API ByteArray
|
||||
{
|
||||
using Container = std::vector<UInt8>;
|
||||
|
||||
|
|
@ -90,6 +88,7 @@ namespace Nz
|
|||
inline void ShrinkToFit();
|
||||
inline void Swap(ByteArray& other);
|
||||
|
||||
inline String ToHex() const;
|
||||
inline String ToString() const;
|
||||
|
||||
// STL interface
|
||||
|
|
@ -109,6 +108,8 @@ namespace Nz
|
|||
inline size_type size() const noexcept;
|
||||
|
||||
// Operators
|
||||
NAZARA_CORE_API friend std::ostream& operator<<(std::ostream& out, const Nz::ByteArray& byteArray);
|
||||
|
||||
inline reference operator[](size_type pos);
|
||||
inline const_reference operator[](size_type pos) const;
|
||||
inline ByteArray& operator=(const ByteArray& array) = default;
|
||||
|
|
@ -124,13 +125,12 @@ namespace Nz
|
|||
inline bool operator>=(const ByteArray& rhs) const;
|
||||
|
||||
private:
|
||||
bool FillHash(AbstractHash* hash) const;
|
||||
|
||||
Container m_array;
|
||||
};
|
||||
}
|
||||
|
||||
NAZARA_CORE_API std::ostream& operator<<(std::ostream& out, const Nz::ByteArray& byteArray);
|
||||
template<>
|
||||
struct Hashable<ByteArray>;
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
|
|
|
|||
|
|
@ -193,6 +193,17 @@ namespace Nz
|
|||
m_array.swap(other.m_array);
|
||||
}
|
||||
|
||||
inline String ByteArray::ToHex() const
|
||||
{
|
||||
unsigned int length = m_array.size() * 2;
|
||||
|
||||
String hexOutput(length, '\0');
|
||||
for (unsigned int i = 0; i < m_array.size(); ++i)
|
||||
std::sprintf(&hexOutput[i * 2], "%02x", m_array[i]);
|
||||
|
||||
return hexOutput;
|
||||
}
|
||||
|
||||
inline String ByteArray::ToString() const
|
||||
{
|
||||
return String(reinterpret_cast<const char*>(GetConstBuffer()), GetSize());
|
||||
|
|
@ -321,6 +332,16 @@ namespace Nz
|
|||
{
|
||||
return m_array >= rhs.m_array;
|
||||
}
|
||||
|
||||
template<>
|
||||
struct Hashable<ByteArray>
|
||||
{
|
||||
bool operator()(const ByteArray& byteArray, AbstractHash* hash) const
|
||||
{
|
||||
hash->Append(byteArray.GetConstBuffer(), byteArray.GetSize());
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace std
|
||||
|
|
|
|||
|
|
@ -11,8 +11,6 @@
|
|||
#include <Nazara/Core/ByteArray.hpp>
|
||||
#include <Nazara/Core/Directory.hpp>
|
||||
#include <Nazara/Core/Endianness.hpp>
|
||||
#include <Nazara/Core/Hashable.hpp>
|
||||
#include <Nazara/Core/HashDigest.hpp>
|
||||
#include <Nazara/Core/InputStream.hpp>
|
||||
#include <Nazara/Core/String.hpp>
|
||||
|
||||
|
|
@ -28,7 +26,7 @@ namespace Nz
|
|||
{
|
||||
class FileImpl;
|
||||
|
||||
class NAZARA_CORE_API File : public Hashable, public InputStream
|
||||
class NAZARA_CORE_API File : public InputStream
|
||||
{
|
||||
public:
|
||||
File();
|
||||
|
|
@ -83,6 +81,8 @@ namespace Nz
|
|||
File& operator=(File&& file) noexcept;
|
||||
|
||||
static String AbsolutePath(const String& filePath);
|
||||
static inline ByteArray ComputeHash(HashType hash, const String& filePath);
|
||||
static inline ByteArray ComputeHash(AbstractHash* hash, const String& filePath);
|
||||
static bool Copy(const String& sourcePath, const String& targetPath);
|
||||
static bool Delete(const String& filePath);
|
||||
static bool Exists(const String& filePath);
|
||||
|
|
@ -90,8 +90,6 @@ namespace Nz
|
|||
static String GetDirectory(const String& filePath);
|
||||
static time_t GetLastAccessTime(const String& filePath);
|
||||
static time_t GetLastWriteTime(const String& filePath);
|
||||
static HashDigest GetHash(const String& filePath, HashType hash);
|
||||
static HashDigest GetHash(const String& filePath, AbstractHash* hash);
|
||||
static UInt64 GetSize(const String& filePath);
|
||||
static bool IsAbsolute(const String& filePath);
|
||||
static String NormalizePath(const String& filePath);
|
||||
|
|
@ -99,8 +97,6 @@ namespace Nz
|
|||
static bool Rename(const String& sourcePath, const String& targetPath);
|
||||
|
||||
private:
|
||||
bool FillHash(AbstractHash* hash) const;
|
||||
|
||||
NazaraMutexAttrib(m_mutex, mutable)
|
||||
|
||||
Endianness m_endianness;
|
||||
|
|
@ -108,6 +104,11 @@ namespace Nz
|
|||
FileImpl* m_impl;
|
||||
unsigned int m_openMode;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Hashable<File>;
|
||||
}
|
||||
|
||||
#include <Nazara/Core/File.inl>
|
||||
|
||||
#endif // NAZARA_FILE_HPP
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline ByteArray File::ComputeHash(HashType hash, const String& filePath)
|
||||
{
|
||||
return ComputeHash(AbstractHash::Get(hash).get(), filePath);
|
||||
}
|
||||
|
||||
inline ByteArray File::ComputeHash(AbstractHash* hash, const String& filePath)
|
||||
{
|
||||
return Nz::ComputeHash(hash, File(filePath));
|
||||
}
|
||||
|
||||
template<>
|
||||
struct Hashable<File>
|
||||
{
|
||||
bool operator()(const Nz::File& originalFile, AbstractHash* hash) const
|
||||
{
|
||||
|
||||
File file(originalFile.GetPath());
|
||||
if (!file.Open(OpenMode_ReadOnly))
|
||||
{
|
||||
NazaraError("Unable to open file");
|
||||
return false;
|
||||
}
|
||||
|
||||
UInt64 remainingSize = file.GetSize();
|
||||
|
||||
char buffer[NAZARA_CORE_FILE_BUFFERSIZE];
|
||||
while (remainingSize > 0)
|
||||
{
|
||||
unsigned int size = static_cast<unsigned int>(std::min(remainingSize, static_cast<UInt64>(NAZARA_CORE_FILE_BUFFERSIZE)));
|
||||
if (file.Read(&buffer[0], sizeof(char), size) != sizeof(char)*size)
|
||||
{
|
||||
NazaraError("Unable to read file");
|
||||
return false;
|
||||
}
|
||||
|
||||
remainingSize -= size;
|
||||
hash->Append(reinterpret_cast<UInt8*>(&buffer[0]), size);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_HASH_HPP
|
||||
#define NAZARA_HASH_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/Hashable.hpp>
|
||||
#include <Nazara/Core/HashDigest.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_CORE_API Hash
|
||||
{
|
||||
public:
|
||||
Hash(HashType hash);
|
||||
Hash(AbstractHash* hashImpl);
|
||||
Hash(const Hash&) = delete;
|
||||
Hash(Hash&&) = delete; ///TODO
|
||||
~Hash();
|
||||
|
||||
HashDigest Process(const Hashable& hashable);
|
||||
|
||||
Hash& operator=(const Hash&) = delete;
|
||||
Hash& operator=(Hash&&) = delete; ///TODO
|
||||
|
||||
private:
|
||||
AbstractHash* m_impl;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_HASH_HPP
|
||||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/HashDigest.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
|
|
@ -21,12 +21,12 @@ namespace Nz
|
|||
HashCRC32(UInt32 polynomial = 0x04c11db7);
|
||||
virtual ~HashCRC32();
|
||||
|
||||
void Append(const UInt8* data, unsigned int len);
|
||||
void Begin();
|
||||
HashDigest End();
|
||||
void Append(const UInt8* data, unsigned int len) override;
|
||||
void Begin() override;
|
||||
ByteArray End() override;
|
||||
|
||||
static unsigned int GetDigestLength();
|
||||
static String GetHashName();
|
||||
unsigned int GetDigestLength() const override;
|
||||
const char* GetHashName() const override;
|
||||
|
||||
private:
|
||||
HashCRC32_state* m_state;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/HashDigest.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
#include <Nazara/Core/String.hpp>
|
||||
|
||||
namespace Nz
|
||||
|
|
@ -22,12 +22,12 @@ namespace Nz
|
|||
HashFletcher16();
|
||||
virtual ~HashFletcher16();
|
||||
|
||||
void Append(const UInt8* data, unsigned int len);
|
||||
void Begin();
|
||||
HashDigest End();
|
||||
void Append(const UInt8* data, unsigned int len) override;
|
||||
void Begin() override;
|
||||
ByteArray End() override;
|
||||
|
||||
static unsigned int GetDigestLength();
|
||||
static String GetHashName();
|
||||
unsigned int GetDigestLength() const override;
|
||||
const char* GetHashName() const override;
|
||||
|
||||
private:
|
||||
HashFletcher16_state* m_state;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/HashDigest.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
|
|
@ -21,12 +21,12 @@ namespace Nz
|
|||
HashMD5();
|
||||
virtual ~HashMD5();
|
||||
|
||||
void Append(const UInt8* data, unsigned int len);
|
||||
void Begin();
|
||||
HashDigest End();
|
||||
void Append(const UInt8* data, unsigned int len) override;
|
||||
void Begin() override;
|
||||
ByteArray End() override;
|
||||
|
||||
static unsigned int GetDigestLength();
|
||||
static String GetHashName();
|
||||
unsigned int GetDigestLength() const override;
|
||||
const char* GetHashName() const override;
|
||||
|
||||
private:
|
||||
HashMD5_state* m_state;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/HashDigest.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
|
|
@ -21,12 +21,12 @@ namespace Nz
|
|||
HashSHA1();
|
||||
virtual ~HashSHA1();
|
||||
|
||||
void Append(const UInt8* data, unsigned int len);
|
||||
void Begin();
|
||||
HashDigest End();
|
||||
void Append(const UInt8* data, unsigned int len) override;
|
||||
void Begin() override;
|
||||
ByteArray End() override;
|
||||
|
||||
static unsigned int GetDigestLength();
|
||||
static String GetHashName();
|
||||
unsigned int GetDigestLength() const override;
|
||||
const char* GetHashName() const override;
|
||||
|
||||
private:
|
||||
SHA_CTX* m_state;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/HashDigest.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
|
|
@ -21,12 +21,12 @@ namespace Nz
|
|||
HashSHA224();
|
||||
virtual ~HashSHA224();
|
||||
|
||||
void Append(const UInt8* data, unsigned int len);
|
||||
void Begin();
|
||||
HashDigest End();
|
||||
void Append(const UInt8* data, unsigned int len) override;
|
||||
void Begin() override;
|
||||
ByteArray End() override;
|
||||
|
||||
static unsigned int GetDigestLength();
|
||||
static String GetHashName();
|
||||
unsigned int GetDigestLength() const override;
|
||||
const char* GetHashName() const override;
|
||||
|
||||
private:
|
||||
SHA_CTX* m_state;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/HashDigest.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
|
|
@ -21,12 +21,12 @@ namespace Nz
|
|||
HashSHA256();
|
||||
virtual ~HashSHA256();
|
||||
|
||||
void Append(const UInt8* data, unsigned int len);
|
||||
void Begin();
|
||||
HashDigest End();
|
||||
void Append(const UInt8* data, unsigned int len) override;
|
||||
void Begin() override;
|
||||
ByteArray End() override;
|
||||
|
||||
static unsigned int GetDigestLength();
|
||||
static String GetHashName();
|
||||
unsigned int GetDigestLength() const override;
|
||||
const char* GetHashName() const override;
|
||||
|
||||
private:
|
||||
SHA_CTX* m_state;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/HashDigest.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
|
|
@ -21,12 +21,12 @@ namespace Nz
|
|||
HashSHA384();
|
||||
virtual ~HashSHA384();
|
||||
|
||||
void Append(const UInt8* data, unsigned int len);
|
||||
void Begin();
|
||||
HashDigest End();
|
||||
void Append(const UInt8* data, unsigned int len) override;
|
||||
void Begin() override;
|
||||
ByteArray End() override;
|
||||
|
||||
static unsigned int GetDigestLength();
|
||||
static String GetHashName();
|
||||
unsigned int GetDigestLength() const override;
|
||||
const char* GetHashName() const override;
|
||||
|
||||
private:
|
||||
SHA_CTX* m_state;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/HashDigest.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
|
|
@ -21,12 +21,12 @@ namespace Nz
|
|||
HashSHA512();
|
||||
virtual ~HashSHA512();
|
||||
|
||||
void Append(const UInt8* data, unsigned int len);
|
||||
void Begin();
|
||||
HashDigest End();
|
||||
void Append(const UInt8* data, unsigned int len) override;
|
||||
void Begin() override;
|
||||
ByteArray End() override;
|
||||
|
||||
static unsigned int GetDigestLength();
|
||||
static String GetHashName();
|
||||
unsigned int GetDigestLength() const override;
|
||||
const char* GetHashName() const override;
|
||||
|
||||
private:
|
||||
SHA_CTX* m_state;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/HashDigest.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
|
|
@ -19,12 +19,12 @@ namespace Nz
|
|||
HashWhirlpool();
|
||||
virtual ~HashWhirlpool();
|
||||
|
||||
void Append(const UInt8* data, unsigned int len);
|
||||
void Begin();
|
||||
HashDigest End();
|
||||
void Append(const UInt8* data, unsigned int len) override;
|
||||
void Begin() override;
|
||||
ByteArray End() override;
|
||||
|
||||
static unsigned int GetDigestLength();
|
||||
static String GetHashName();
|
||||
unsigned int GetDigestLength() const;
|
||||
const char* GetHashName() const;
|
||||
|
||||
private:
|
||||
HashWhirlpool_state* m_state;
|
||||
|
|
|
|||
|
|
@ -1,56 +0,0 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_HASHDIGEST_HPP
|
||||
#define NAZARA_HASHDIGEST_HPP
|
||||
|
||||
///TODO: Remplacer par ByteArray
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <iosfwd>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_CORE_API HashDigest
|
||||
{
|
||||
public:
|
||||
HashDigest();
|
||||
HashDigest(const String& hashName, const UInt8* digest, unsigned int length);
|
||||
HashDigest(const HashDigest& rhs);
|
||||
HashDigest(HashDigest&& rhs) noexcept;
|
||||
~HashDigest();
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
const UInt8* GetDigest() const;
|
||||
unsigned int GetDigestLength() const;
|
||||
String GetHashName() const;
|
||||
|
||||
String ToHex() const;
|
||||
|
||||
UInt8 operator[](unsigned int pos) const;
|
||||
|
||||
HashDigest& operator=(const HashDigest& rhs);
|
||||
HashDigest& operator=(HashDigest&& rhs) noexcept;
|
||||
|
||||
bool operator==(const HashDigest& rhs) const;
|
||||
bool operator!=(const HashDigest& rhs) const;
|
||||
bool operator<(const HashDigest& rhs) const;
|
||||
bool operator<=(const HashDigest& rhs) const;
|
||||
bool operator>(const HashDigest& rhs) const;
|
||||
bool operator>=(const HashDigest& rhs) const;
|
||||
|
||||
NAZARA_CORE_API friend std::ostream& operator<<(std::ostream& out, const HashDigest& string);
|
||||
|
||||
private:
|
||||
String m_hashName;
|
||||
UInt8* m_digest;
|
||||
unsigned int m_digestLength;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_HASHDIGEST_HPP
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef HASHABLE_HPP_INCLUDED
|
||||
#define HASHABLE_HPP_INCLUDED
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Enums.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class AbstractHash;
|
||||
class HashDigest;
|
||||
|
||||
class NAZARA_CORE_API Hashable
|
||||
{
|
||||
friend class Hash;
|
||||
|
||||
public:
|
||||
Hashable() = default;
|
||||
Hashable(const Hashable&) = default;
|
||||
Hashable(Hashable&&) = default;
|
||||
virtual ~Hashable();
|
||||
|
||||
HashDigest GetHash(HashType hash) const;
|
||||
HashDigest GetHash(AbstractHash* impl) const;
|
||||
|
||||
Hashable& operator=(const Hashable&) = default;
|
||||
Hashable& operator=(Hashable&&) = default;
|
||||
|
||||
private:
|
||||
virtual bool FillHash(AbstractHash* impl) const = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // HASHABLE_HPP_INCLUDED
|
||||
|
|
@ -8,7 +8,7 @@
|
|||
#define NAZARA_STRING_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Core/Hashable.hpp>
|
||||
#include <Nazara/Core/Algorithm.hpp>
|
||||
#include <atomic>
|
||||
#include <iosfwd>
|
||||
#include <memory>
|
||||
|
|
@ -20,7 +20,7 @@ namespace Nz
|
|||
class AbstractHash;
|
||||
class HashDigest;
|
||||
|
||||
class NAZARA_CORE_API String : public Hashable
|
||||
class NAZARA_CORE_API String
|
||||
{
|
||||
public:
|
||||
enum Flags
|
||||
|
|
@ -306,7 +306,6 @@ namespace Nz
|
|||
String(std::shared_ptr<SharedString>&& sharedString);
|
||||
|
||||
void EnsureOwnership(bool discardContent = false);
|
||||
bool FillHash(AbstractHash* hash) const;
|
||||
inline void ReleaseString();
|
||||
|
||||
static const std::shared_ptr<SharedString>& GetEmptyString();
|
||||
|
|
@ -324,6 +323,9 @@ namespace Nz
|
|||
std::unique_ptr<char[]> string;
|
||||
};
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Hashable<String>;
|
||||
}
|
||||
|
||||
namespace std
|
||||
|
|
@ -331,6 +333,9 @@ namespace std
|
|||
NAZARA_CORE_API istream& getline(istream& is, Nz::String& str);
|
||||
NAZARA_CORE_API istream& getline(istream& is, Nz::String& str, char delim);
|
||||
NAZARA_CORE_API void swap(Nz::String& lhs, Nz::String& rhs);
|
||||
|
||||
template<>
|
||||
struct hash<Nz::String>;
|
||||
}
|
||||
|
||||
#include <Nazara/Core/String.inl>
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
|
|
@ -37,6 +38,16 @@ namespace Nz
|
|||
{
|
||||
string[strSize] = '\0';
|
||||
}
|
||||
|
||||
template<>
|
||||
struct Hashable<String>
|
||||
{
|
||||
bool operator()(const String& str, AbstractHash* hash) const
|
||||
{
|
||||
hash->Append(reinterpret_cast<const UInt8*>(str.GetConstBuffer()), str.GetSize());
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace std
|
||||
|
|
|
|||
|
|
@ -3,9 +3,57 @@
|
|||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/Hash/CRC32.hpp>
|
||||
#include <Nazara/Core/Hash/Fletcher16.hpp>
|
||||
#include <Nazara/Core/Hash/MD5.hpp>
|
||||
#include <Nazara/Core/Hash/SHA1.hpp>
|
||||
#include <Nazara/Core/Hash/SHA224.hpp>
|
||||
#include <Nazara/Core/Hash/SHA256.hpp>
|
||||
#include <Nazara/Core/Hash/SHA384.hpp>
|
||||
#include <Nazara/Core/Hash/SHA512.hpp>
|
||||
#include <Nazara/Core/Hash/Whirlpool.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
AbstractHash::~AbstractHash() = default;
|
||||
|
||||
std::unique_ptr<AbstractHash> AbstractHash::Get(HashType type)
|
||||
{
|
||||
NazaraAssert(type <= HashType_Max, "Hash type value out of enum");
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case HashType_Fletcher16:
|
||||
return std::unique_ptr<AbstractHash>(new HashFletcher16);
|
||||
|
||||
case HashType_CRC32:
|
||||
return std::unique_ptr<AbstractHash>(new HashCRC32);
|
||||
|
||||
case HashType_MD5:
|
||||
return std::unique_ptr<AbstractHash>(new HashMD5);
|
||||
|
||||
case HashType_SHA1:
|
||||
return std::unique_ptr<AbstractHash>(new HashSHA1);
|
||||
|
||||
case HashType_SHA224:
|
||||
return std::unique_ptr<AbstractHash>(new HashSHA224);
|
||||
|
||||
case HashType_SHA256:
|
||||
return std::unique_ptr<AbstractHash>(new HashSHA256);
|
||||
|
||||
case HashType_SHA384:
|
||||
return std::unique_ptr<AbstractHash>(new HashSHA384);
|
||||
|
||||
case HashType_SHA512:
|
||||
return std::unique_ptr<AbstractHash>(new HashSHA512);
|
||||
|
||||
case HashType_Whirlpool:
|
||||
return std::unique_ptr<AbstractHash>(new HashWhirlpool);
|
||||
}
|
||||
|
||||
NazaraInternalError("Hash type not handled (0x" + String::Number(type, 16) + ')');
|
||||
return std::unique_ptr<AbstractHash>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,16 +9,9 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
std::ostream& operator<<(std::ostream& out, const ByteArray& byteArray)
|
||||
std::ostream& operator<<(std::ostream& out, const Nz::ByteArray& byteArray)
|
||||
{
|
||||
out << byteArray.ToString();
|
||||
out << byteArray.ToHex();
|
||||
return out;
|
||||
}
|
||||
|
||||
bool ByteArray::FillHash(AbstractHash* hash) const
|
||||
{
|
||||
hash->Append(GetConstBuffer(), GetSize());
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@
|
|||
#include <Nazara/Core/Config.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/ErrorFlags.hpp>
|
||||
#include <Nazara/Core/Hash.hpp>
|
||||
#include <Nazara/Core/StringStream.hpp>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
|
|
@ -631,22 +630,6 @@ namespace Nz
|
|||
return FileImpl::GetLastWriteTime(NormalizePath(filePath));
|
||||
}
|
||||
|
||||
HashDigest File::GetHash(const String& filePath, HashType hash)
|
||||
{
|
||||
File file(filePath);
|
||||
|
||||
Hash h(hash);
|
||||
return h.Process(file);
|
||||
}
|
||||
|
||||
HashDigest File::GetHash(const String& filePath, AbstractHash* hash)
|
||||
{
|
||||
File file(filePath);
|
||||
|
||||
Hash h(hash);
|
||||
return h.Process(file);
|
||||
}
|
||||
|
||||
UInt64 File::GetSize(const String& filePath)
|
||||
{
|
||||
if (filePath.IsEmpty())
|
||||
|
|
@ -714,32 +697,4 @@ namespace Nz
|
|||
|
||||
return FileImpl::Rename(NormalizePath(sourcePath), NormalizePath(targetPath));
|
||||
}
|
||||
|
||||
bool File::FillHash(AbstractHash* hash) const
|
||||
{
|
||||
File file(m_filePath);
|
||||
if (!file.Open(OpenMode_ReadOnly))
|
||||
{
|
||||
NazaraError("Unable to open file");
|
||||
return false;
|
||||
}
|
||||
|
||||
UInt64 remainingSize = file.GetSize();
|
||||
|
||||
char buffer[NAZARA_CORE_FILE_BUFFERSIZE];
|
||||
while (remainingSize > 0)
|
||||
{
|
||||
unsigned int size = static_cast<unsigned int>(std::min(remainingSize, static_cast<UInt64>(NAZARA_CORE_FILE_BUFFERSIZE)));
|
||||
if (file.Read(&buffer[0], sizeof(char), size) != sizeof(char)*size)
|
||||
{
|
||||
NazaraError("Unable to read file");
|
||||
return false;
|
||||
}
|
||||
|
||||
remainingSize -= size;
|
||||
hash->Append(reinterpret_cast<UInt8*>(&buffer[0]), size);
|
||||
}
|
||||
|
||||
return true;
|
||||
} // Fermeture automatique du fichier
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,83 +0,0 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/Hash.hpp>
|
||||
#include <Nazara/Core/Hash/CRC32.hpp>
|
||||
#include <Nazara/Core/Hash/Fletcher16.hpp>
|
||||
#include <Nazara/Core/Hash/MD5.hpp>
|
||||
#include <Nazara/Core/Hash/SHA1.hpp>
|
||||
#include <Nazara/Core/Hash/SHA224.hpp>
|
||||
#include <Nazara/Core/Hash/SHA256.hpp>
|
||||
#include <Nazara/Core/Hash/SHA384.hpp>
|
||||
#include <Nazara/Core/Hash/SHA512.hpp>
|
||||
#include <Nazara/Core/Hash/Whirlpool.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
Hash::Hash(HashType hash)
|
||||
{
|
||||
switch (hash)
|
||||
{
|
||||
case HashType_Fletcher16:
|
||||
m_impl = new HashFletcher16;
|
||||
break;
|
||||
|
||||
case HashType_CRC32:
|
||||
m_impl = new HashCRC32;
|
||||
break;
|
||||
|
||||
case HashType_MD5:
|
||||
m_impl = new HashMD5;
|
||||
break;
|
||||
|
||||
case HashType_SHA1:
|
||||
m_impl = new HashSHA1;
|
||||
break;
|
||||
|
||||
case HashType_SHA224:
|
||||
m_impl = new HashSHA224;
|
||||
break;
|
||||
|
||||
case HashType_SHA256:
|
||||
m_impl = new HashSHA256;
|
||||
break;
|
||||
|
||||
case HashType_SHA384:
|
||||
m_impl = new HashSHA384;
|
||||
break;
|
||||
|
||||
case HashType_SHA512:
|
||||
m_impl = new HashSHA512;
|
||||
break;
|
||||
|
||||
case HashType_Whirlpool:
|
||||
m_impl = new HashWhirlpool;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Hash::Hash(AbstractHash* hashImpl) :
|
||||
m_impl(hashImpl)
|
||||
{
|
||||
}
|
||||
|
||||
Hash::~Hash()
|
||||
{
|
||||
delete m_impl;
|
||||
}
|
||||
|
||||
HashDigest Hash::Process(const Hashable& hashable)
|
||||
{
|
||||
m_impl->Begin();
|
||||
if (hashable.FillHash(m_impl))
|
||||
return m_impl->End();
|
||||
else // Erreur
|
||||
{
|
||||
m_impl->End();
|
||||
|
||||
return HashDigest();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -109,7 +109,7 @@ namespace Nz
|
|||
m_state->crc = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
HashDigest HashCRC32::End()
|
||||
ByteArray HashCRC32::End()
|
||||
{
|
||||
m_state->crc ^= 0xFFFFFFFF;
|
||||
|
||||
|
|
@ -117,17 +117,16 @@ namespace Nz
|
|||
SwapBytes(&m_state->crc, sizeof(UInt32));
|
||||
#endif
|
||||
|
||||
return HashDigest(GetHashName(), reinterpret_cast<UInt8*>(&m_state->crc), 4);
|
||||
return ByteArray(reinterpret_cast<UInt8*>(&m_state->crc), 4);
|
||||
}
|
||||
|
||||
unsigned int HashCRC32::GetDigestLength()
|
||||
unsigned int HashCRC32::GetDigestLength() const
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
|
||||
String HashCRC32::GetHashName()
|
||||
const char* HashCRC32::GetHashName() const
|
||||
{
|
||||
static String hashName = "CRC32";
|
||||
return hashName;
|
||||
return "CRC32";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ namespace Nz
|
|||
m_state->sum2 = 0xff;
|
||||
}
|
||||
|
||||
HashDigest HashFletcher16::End()
|
||||
ByteArray HashFletcher16::End()
|
||||
{
|
||||
m_state->sum1 = (m_state->sum1 & 0xff) + (m_state->sum1 >> 8);
|
||||
m_state->sum2 = (m_state->sum2 & 0xff) + (m_state->sum2 >> 8);
|
||||
|
|
@ -59,17 +59,16 @@ namespace Nz
|
|||
SwapBytes(&fletcher, sizeof(UInt32));
|
||||
#endif
|
||||
|
||||
return HashDigest(GetHashName(), reinterpret_cast<UInt8*>(&fletcher), 2);
|
||||
return ByteArray(reinterpret_cast<UInt8*>(&fletcher), 2);
|
||||
}
|
||||
|
||||
unsigned int HashFletcher16::GetDigestLength()
|
||||
unsigned int HashFletcher16::GetDigestLength() const
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
String HashFletcher16::GetHashName()
|
||||
const char* HashFletcher16::GetHashName() const
|
||||
{
|
||||
static String hashName = "Fletcher16";
|
||||
return hashName;
|
||||
return "Fletcher16";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -325,7 +325,7 @@ namespace Nz
|
|||
m_state->abcd[3] = 0x10325476;
|
||||
}
|
||||
|
||||
HashDigest HashMD5::End()
|
||||
ByteArray HashMD5::End()
|
||||
{
|
||||
static const unsigned char pad[64] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
|
|
@ -349,17 +349,16 @@ namespace Nz
|
|||
for (i = 0; i < 16; ++i)
|
||||
digest[i] = static_cast<UInt8>(m_state->abcd[i >> 2] >> ((i & 3) << 3));
|
||||
|
||||
return HashDigest(GetHashName(), &digest[0], 16);
|
||||
return ByteArray(&digest[0], 16);
|
||||
}
|
||||
|
||||
unsigned int HashMD5::GetDigestLength()
|
||||
unsigned int HashMD5::GetDigestLength() const
|
||||
{
|
||||
return 16;
|
||||
}
|
||||
|
||||
String HashMD5::GetHashName()
|
||||
const char* HashMD5::GetHashName() const
|
||||
{
|
||||
static String hashName = "MD5";
|
||||
return hashName;
|
||||
return "MD5";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,23 +28,22 @@ namespace Nz
|
|||
SHA1_Init(m_state);
|
||||
}
|
||||
|
||||
HashDigest HashSHA1::End()
|
||||
ByteArray HashSHA1::End()
|
||||
{
|
||||
UInt8 digest[SHA1_DIGEST_LENGTH];
|
||||
|
||||
SHA1_End(m_state, digest);
|
||||
|
||||
return HashDigest(GetHashName(), digest, SHA1_DIGEST_LENGTH);
|
||||
return ByteArray(digest, SHA1_DIGEST_LENGTH);
|
||||
}
|
||||
|
||||
unsigned int HashSHA1::GetDigestLength()
|
||||
unsigned int HashSHA1::GetDigestLength() const
|
||||
{
|
||||
return SHA1_DIGEST_LENGTH;
|
||||
}
|
||||
|
||||
String HashSHA1::GetHashName()
|
||||
const char* HashSHA1::GetHashName() const
|
||||
{
|
||||
static String hashName = "SHA1";
|
||||
return hashName;
|
||||
return "SHA1";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,23 +28,22 @@ namespace Nz
|
|||
SHA224_Init(m_state);
|
||||
}
|
||||
|
||||
HashDigest HashSHA224::End()
|
||||
ByteArray HashSHA224::End()
|
||||
{
|
||||
UInt8 digest[SHA224_DIGEST_LENGTH];
|
||||
|
||||
SHA224_End(m_state, digest);
|
||||
|
||||
return HashDigest(GetHashName(), digest, SHA224_DIGEST_LENGTH);
|
||||
return ByteArray(digest, SHA224_DIGEST_LENGTH);
|
||||
}
|
||||
|
||||
unsigned int HashSHA224::GetDigestLength()
|
||||
unsigned int HashSHA224::GetDigestLength() const
|
||||
{
|
||||
return SHA224_DIGEST_LENGTH;
|
||||
}
|
||||
|
||||
String HashSHA224::GetHashName()
|
||||
const char* HashSHA224::GetHashName() const
|
||||
{
|
||||
static String hashName = "SHA224";
|
||||
return hashName;
|
||||
return "SHA224";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,23 +28,22 @@ namespace Nz
|
|||
SHA256_Init(m_state);
|
||||
}
|
||||
|
||||
HashDigest HashSHA256::End()
|
||||
ByteArray HashSHA256::End()
|
||||
{
|
||||
UInt8 digest[SHA256_DIGEST_LENGTH];
|
||||
|
||||
SHA256_End(m_state, digest);
|
||||
|
||||
return HashDigest(GetHashName(), digest, SHA256_DIGEST_LENGTH);
|
||||
return ByteArray(digest, SHA256_DIGEST_LENGTH);
|
||||
}
|
||||
|
||||
unsigned int HashSHA256::GetDigestLength()
|
||||
unsigned int HashSHA256::GetDigestLength() const
|
||||
{
|
||||
return SHA256_DIGEST_LENGTH;
|
||||
}
|
||||
|
||||
String HashSHA256::GetHashName()
|
||||
const char* HashSHA256::GetHashName() const
|
||||
{
|
||||
static String hashName = "SHA256";
|
||||
return hashName;
|
||||
return "SHA256";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,23 +28,22 @@ namespace Nz
|
|||
SHA384_Init(m_state);
|
||||
}
|
||||
|
||||
HashDigest HashSHA384::End()
|
||||
ByteArray HashSHA384::End()
|
||||
{
|
||||
UInt8 digest[SHA384_DIGEST_LENGTH];
|
||||
|
||||
SHA384_End(m_state, digest);
|
||||
|
||||
return HashDigest(GetHashName(), digest, SHA384_DIGEST_LENGTH);
|
||||
return ByteArray(digest, SHA384_DIGEST_LENGTH);
|
||||
}
|
||||
|
||||
unsigned int HashSHA384::GetDigestLength()
|
||||
unsigned int HashSHA384::GetDigestLength() const
|
||||
{
|
||||
return SHA384_DIGEST_LENGTH;
|
||||
}
|
||||
|
||||
String HashSHA384::GetHashName()
|
||||
const char* HashSHA384::GetHashName() const
|
||||
{
|
||||
static String hashName = "SHA384";
|
||||
return hashName;
|
||||
return "SHA384";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,23 +28,22 @@ namespace Nz
|
|||
SHA512_Init(m_state);
|
||||
}
|
||||
|
||||
HashDigest HashSHA512::End()
|
||||
ByteArray HashSHA512::End()
|
||||
{
|
||||
UInt8 digest[SHA512_DIGEST_LENGTH];
|
||||
|
||||
SHA512_End(m_state, digest);
|
||||
|
||||
return HashDigest(GetHashName(), digest, SHA512_DIGEST_LENGTH);
|
||||
return ByteArray(digest, SHA512_DIGEST_LENGTH);
|
||||
}
|
||||
|
||||
unsigned int HashSHA512::GetDigestLength()
|
||||
unsigned int HashSHA512::GetDigestLength() const
|
||||
{
|
||||
return SHA512_DIGEST_LENGTH;
|
||||
}
|
||||
|
||||
String HashSHA512::GetHashName()
|
||||
const char* HashSHA512::GetHashName() const
|
||||
{
|
||||
static String hashName = "SHA512";
|
||||
return hashName;
|
||||
return "SHA512";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -962,7 +962,7 @@ namespace Nz
|
|||
m_state->hash[i] = 0L; // initial value
|
||||
}
|
||||
|
||||
HashDigest HashWhirlpool::End()
|
||||
ByteArray HashWhirlpool::End()
|
||||
{
|
||||
UInt8 result[64];
|
||||
|
||||
|
|
@ -1012,17 +1012,16 @@ namespace Nz
|
|||
m_state->bufferBits = bufferBits;
|
||||
m_state->bufferPos = bufferPos;
|
||||
|
||||
return HashDigest(GetHashName(), &result[0], 64);
|
||||
return ByteArray(&result[0], 64);
|
||||
}
|
||||
|
||||
unsigned int HashWhirlpool::GetDigestLength()
|
||||
unsigned int HashWhirlpool::GetDigestLength() const
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
|
||||
String HashWhirlpool::GetHashName()
|
||||
const char* HashWhirlpool::GetHashName() const
|
||||
{
|
||||
static String hashName = "Whirlpool";
|
||||
return hashName;
|
||||
return "Whirlpool";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,194 +0,0 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/HashDigest.hpp>
|
||||
#include <Nazara/Core/Config.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
HashDigest::HashDigest() :
|
||||
m_digest(nullptr),
|
||||
m_digestLength(0)
|
||||
{
|
||||
}
|
||||
|
||||
HashDigest::HashDigest(const String& hashName, const UInt8* digest, unsigned int length) :
|
||||
m_hashName(hashName),
|
||||
m_digestLength(length)
|
||||
{
|
||||
if (m_digestLength > 0)
|
||||
{
|
||||
m_digest = new UInt8[length];
|
||||
std::memcpy(m_digest, digest, length);
|
||||
}
|
||||
else
|
||||
m_digest = nullptr;
|
||||
}
|
||||
|
||||
HashDigest::HashDigest(const HashDigest& rhs) :
|
||||
m_hashName(rhs.m_hashName),
|
||||
m_digestLength(rhs.m_digestLength)
|
||||
{
|
||||
if (m_digestLength > 0)
|
||||
{
|
||||
m_digest = new UInt8[m_digestLength];
|
||||
std::memcpy(m_digest, rhs.m_digest, m_digestLength);
|
||||
}
|
||||
else
|
||||
m_digest = nullptr;
|
||||
}
|
||||
|
||||
HashDigest::HashDigest(HashDigest&& rhs) noexcept :
|
||||
m_hashName(std::move(rhs.m_hashName)),
|
||||
m_digest(rhs.m_digest),
|
||||
m_digestLength(rhs.m_digestLength)
|
||||
{
|
||||
rhs.m_digest = nullptr;
|
||||
rhs.m_digestLength = 0;
|
||||
}
|
||||
|
||||
HashDigest::~HashDigest()
|
||||
{
|
||||
delete[] m_digest;
|
||||
}
|
||||
|
||||
bool HashDigest::IsValid() const
|
||||
{
|
||||
return m_digestLength > 0;
|
||||
}
|
||||
|
||||
const UInt8* HashDigest::GetDigest() const
|
||||
{
|
||||
return m_digest;
|
||||
}
|
||||
|
||||
unsigned int HashDigest::GetDigestLength() const
|
||||
{
|
||||
return m_digestLength;
|
||||
}
|
||||
|
||||
String HashDigest::GetHashName() const
|
||||
{
|
||||
return m_hashName;
|
||||
}
|
||||
|
||||
String HashDigest::ToHex() const
|
||||
{
|
||||
if (m_digestLength == 0)
|
||||
return String();
|
||||
|
||||
unsigned int length = m_digestLength*2;
|
||||
|
||||
String hexOutput(length);
|
||||
for (unsigned int i = 0; i < m_digestLength; ++i)
|
||||
std::sprintf(&hexOutput[i*2], "%02x", m_digest[i]);
|
||||
|
||||
return hexOutput;
|
||||
}
|
||||
|
||||
UInt8 HashDigest::operator[](unsigned int pos) const
|
||||
{
|
||||
#if NAZARA_CORE_SAFE
|
||||
if (pos >= m_digestLength)
|
||||
{
|
||||
NazaraError("Position out of range");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
return m_digest[pos];
|
||||
}
|
||||
|
||||
HashDigest& HashDigest::operator=(const HashDigest& rhs)
|
||||
{
|
||||
if (this == &rhs)
|
||||
return *this;
|
||||
|
||||
m_hashName = rhs.m_hashName;
|
||||
|
||||
m_digestLength = rhs.m_digestLength;
|
||||
if (m_digestLength > 0)
|
||||
{
|
||||
m_digest = new UInt8[m_digestLength];
|
||||
std::memcpy(m_digest, rhs.m_digest, m_digestLength);
|
||||
}
|
||||
else
|
||||
m_digest = nullptr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
HashDigest& HashDigest::operator=(HashDigest&& rhs) noexcept
|
||||
{
|
||||
std::swap(m_hashName, rhs.m_hashName);
|
||||
std::swap(m_digest, rhs.m_digest);
|
||||
std::swap(m_digestLength, rhs.m_digestLength);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool HashDigest::operator==(const HashDigest& rhs) const
|
||||
{
|
||||
if (m_digest == nullptr || rhs.m_digest == nullptr)
|
||||
return m_digest == rhs.m_digest;
|
||||
|
||||
if (m_digestLength != rhs.m_digestLength)
|
||||
return false;
|
||||
|
||||
return m_hashName == rhs.m_hashName && std::memcmp(m_digest, rhs.m_digest, m_digestLength) == 0;
|
||||
}
|
||||
|
||||
bool HashDigest::operator!=(const HashDigest& rhs) const
|
||||
{
|
||||
return !operator==(rhs);
|
||||
}
|
||||
|
||||
bool HashDigest::operator<(const HashDigest& rhs) const
|
||||
{
|
||||
if (rhs.m_digest == nullptr)
|
||||
return false;
|
||||
|
||||
if (m_digest == nullptr)
|
||||
return true;
|
||||
|
||||
int cmp = String::Compare(m_hashName, rhs.m_hashName);
|
||||
if (cmp == 0)
|
||||
{
|
||||
cmp = std::memcmp(m_digest, rhs.m_digest, std::min(m_digestLength, rhs.m_digestLength));
|
||||
|
||||
if (cmp == 0)
|
||||
return m_digestLength < rhs.m_digestLength;
|
||||
else
|
||||
return cmp < 0;
|
||||
}
|
||||
else
|
||||
return cmp < 0;
|
||||
}
|
||||
|
||||
bool HashDigest::operator<=(const HashDigest& rhs) const
|
||||
{
|
||||
return !rhs.operator<(*this);
|
||||
}
|
||||
|
||||
bool HashDigest::operator>(const HashDigest& rhs) const
|
||||
{
|
||||
return rhs.operator<(*this);
|
||||
}
|
||||
|
||||
bool HashDigest::operator>=(const HashDigest& rhs) const
|
||||
{
|
||||
return !operator<(rhs);
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const Nz::HashDigest& hashstring)
|
||||
{
|
||||
out << hashstring.ToHex();
|
||||
return out;
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/Hashable.hpp>
|
||||
#include <Nazara/Core/Hash.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
Hashable::~Hashable() = default;
|
||||
|
||||
HashDigest Hashable::GetHash(HashType hash) const
|
||||
{
|
||||
Hash h(hash);
|
||||
return h.Process(*this);
|
||||
}
|
||||
|
||||
HashDigest Hashable::GetHash(AbstractHash* impl) const
|
||||
{
|
||||
Hash h(impl);
|
||||
return h.Process(*this);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,7 +5,6 @@
|
|||
///TODO: Réécrire une bonne partie des algorithmes employés (Relu jusqu'à 3538)
|
||||
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/Config.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/Unicode.hpp>
|
||||
|
|
@ -4203,13 +4202,6 @@ namespace Nz
|
|||
}
|
||||
}
|
||||
|
||||
bool String::FillHash(AbstractHash* hash) const
|
||||
{
|
||||
hash->Append(reinterpret_cast<const UInt8*>(GetConstBuffer()), GetSize());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const std::shared_ptr<String::SharedString>& String::GetEmptyString()
|
||||
{
|
||||
static auto emptyString = std::make_shared<SharedString>();
|
||||
|
|
|
|||
Loading…
Reference in New Issue