Added SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512 support

This commit is contained in:
Lynix 2012-05-10 00:53:36 +02:00
parent d0bc1d908a
commit 68b238fc74
23 changed files with 1621 additions and 15 deletions

View File

@ -1,6 +1,8 @@
project "NazaraNetwork"
links "ws2_32"
if (os.is("windows")) then
links "ws2_32"
end
files
{

View File

@ -5,6 +5,7 @@
#include <Nazara/Core/Config.hpp>
#if NAZARA_CORE_MEMORYLEAKTRACKER || defined(NAZARA_DEBUG)
#include <Nazara/Core/Debug/MemoryLeakTracker.hpp>
#include <Nazara/Core/Error.hpp>
#define delete NzMemoryManager::NextFree(__FILE__, __LINE__), delete
#define new new(__FILE__, __LINE__)

View File

@ -11,7 +11,7 @@
#include <Nazara/Core/String.hpp>
#include <Nazara/Utility/NonCopyable.hpp>
#define NAZARA_DYNLIB
#define NAZARA_CLASS_DYNLIB
#include <Nazara/Core/ThreadSafety.hpp>
class NzDynLibImpl;
@ -40,6 +40,6 @@ class NzDynLib : NzNonCopyable
NzDynLibImpl* m_impl;
};
#undef NAZARA_DYNLIB
#undef NAZARA_CLASS_DYNLIB
#endif // NAZARA_DYNLIB_HPP

View File

@ -2,6 +2,9 @@
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <algorithm>
#include <Nazara/Core/Debug.hpp>
inline void NzByteSwap(void* buffer, unsigned int size)
{
nzUInt8* bytes = reinterpret_cast<nzUInt8*>(buffer);
@ -36,3 +39,5 @@ inline nzEndianness NzGetPlatformEndianness()
return endianness;
#endif
}
#include <Nazara/Core/DebugOff.hpp>

View File

@ -0,0 +1,33 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_HASH_SHA1_HPP
#define NAZARA_HASH_SHA1_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/HashDigest.hpp>
#include <Nazara/Core/HashImpl.hpp>
union SHA_CTX;
class NAZARA_API NzHashSHA1 : public NzHashImpl
{
public:
NzHashSHA1();
virtual ~NzHashSHA1();
void Append(const nzUInt8* data, unsigned int len);
void Begin();
NzHashDigest End();
static unsigned int GetDigestLength();
static NzString GetHashName();
private:
SHA_CTX* m_state;
};
#endif // NAZARA_HASH_SHA1_HPP

View File

@ -0,0 +1,33 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_HASH_SHA224_HPP
#define NAZARA_HASH_SHA224_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/HashDigest.hpp>
#include <Nazara/Core/HashImpl.hpp>
union SHA_CTX;
class NAZARA_API NzHashSHA224 : public NzHashImpl
{
public:
NzHashSHA224();
virtual ~NzHashSHA224();
void Append(const nzUInt8* data, unsigned int len);
void Begin();
NzHashDigest End();
static unsigned int GetDigestLength();
static NzString GetHashName();
private:
SHA_CTX* m_state;
};
#endif // NAZARA_HASH_SHA224_HPP

View File

@ -0,0 +1,33 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_HASH_SHA256_HPP
#define NAZARA_HASH_SHA256_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/HashDigest.hpp>
#include <Nazara/Core/HashImpl.hpp>
union SHA_CTX;
class NAZARA_API NzHashSHA256 : public NzHashImpl
{
public:
NzHashSHA256();
virtual ~NzHashSHA256();
void Append(const nzUInt8* data, unsigned int len);
void Begin();
NzHashDigest End();
static unsigned int GetDigestLength();
static NzString GetHashName();
private:
SHA_CTX* m_state;
};
#endif // NAZARA_HASH_SHA256_HPP

View File

@ -0,0 +1,33 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_HASH_SHA384_HPP
#define NAZARA_HASH_SHA384_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/HashDigest.hpp>
#include <Nazara/Core/HashImpl.hpp>
union SHA_CTX;
class NAZARA_API NzHashSHA384 : public NzHashImpl
{
public:
NzHashSHA384();
virtual ~NzHashSHA384();
void Append(const nzUInt8* data, unsigned int len);
void Begin();
NzHashDigest End();
static unsigned int GetDigestLength();
static NzString GetHashName();
private:
SHA_CTX* m_state;
};
#endif // NAZARA_HASH_SHA384_HPP

View File

@ -0,0 +1,33 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_HASH_SHA512_HPP
#define NAZARA_HASH_SHA512_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/HashDigest.hpp>
#include <Nazara/Core/HashImpl.hpp>
union SHA_CTX;
class NAZARA_API NzHashSHA512 : public NzHashImpl
{
public:
NzHashSHA512();
virtual ~NzHashSHA512();
void Append(const nzUInt8* data, unsigned int len);
void Begin();
NzHashDigest End();
static unsigned int GetDigestLength();
static NzString GetHashName();
private:
SHA_CTX* m_state;
};
#endif // NAZARA_HASH_SHA512_HPP

View File

@ -15,7 +15,7 @@ class NAZARA_API NzHashDigest
{
public:
NzHashDigest();
NzHashDigest(NzString hashName, const nzUInt8* digest, unsigned int length);
NzHashDigest(const NzString& hashName, const nzUInt8* digest, unsigned int length);
NzHashDigest(const NzHashDigest& rhs);
NzHashDigest(NzHashDigest&& rhs);
~NzHashDigest();

View File

@ -14,6 +14,11 @@ enum nzHash
nzHash_CRC32,
nzHash_Fletcher16,
nzHash_MD5,
nzHash_SHA1,
nzHash_SHA224,
nzHash_SHA256,
nzHash_SHA384,
nzHash_SHA512,
nzHash_Whirlpool
};
@ -27,7 +32,7 @@ class NAZARA_API NzHashable
public:
NzHashable() = default;
virtual ~NzHashable() {}
virtual ~NzHashable();
NzHashDigest GetHash(nzHash hash) const;
NzHashDigest GetHash(NzHashImpl* impl) const;

View File

@ -14,11 +14,10 @@
#error No implementation for this platform
#endif
#define NAZARA_CLASS_DYNLIB
#include <Nazara/Core/ThreadSafety.hpp>
#include <Nazara/Core/Debug.hpp>
#define NAZARA_DYNLIB_CPP
NzDynLib::NzDynLib(const NzString& libraryPath) :
m_path(libraryPath),
m_impl(nullptr)

View File

@ -2,12 +2,15 @@
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#define NAZARA_HASH_CPP
#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>
@ -27,6 +30,26 @@ NzHash::NzHash(nzHash hash)
m_impl = new NzHashMD5;
break;
case nzHash_SHA1:
m_impl = new NzHashSHA1;
break;
case nzHash_SHA224:
m_impl = new NzHashSHA224;
break;
case nzHash_SHA256:
m_impl = new NzHashSHA256;
break;
case nzHash_SHA384:
m_impl = new NzHashSHA384;
break;
case nzHash_SHA512:
m_impl = new NzHashSHA512;
break;
case nzHash_Whirlpool:
m_impl = new NzHashWhirlpool;
break;
@ -48,7 +71,7 @@ NzHashDigest NzHash::Hash(const NzHashable& hashable)
m_impl->Begin();
if (hashable.FillHash(m_impl))
return m_impl->End();
else
else // Erreur
{
m_impl->End();

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,107 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
/*
* FILE: sha2.h
* AUTHOR: Aaron D. Gifford
* http://www.aarongifford.com/computers/sha.html
*
* Copyright (c) 2000-2003, Aaron D. Gifford
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: sha2.h,v 1.4 2004/01/07 19:06:18 adg Exp $
*/
#pragma once
#ifndef NAZARA_HASH_SHA2_INTERNAL_HPP
#define NAZARA_HASH_SHA2_INTERNAL_HPP
/* Digest lengths for SHA-1/224/256/384/512 */
#define SHA1_DIGEST_LENGTH 20
#define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
#define SHA224_DIGEST_LENGTH 28
#define SHA224_DIGEST_STRING_LENGTH (SHA224_DIGEST_LENGTH * 2 + 1)
#define SHA256_DIGEST_LENGTH 32
#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
#define SHA384_DIGEST_LENGTH 48
#define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
#define SHA512_DIGEST_LENGTH 64
#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
#include <Nazara/Prerequesites.hpp>
union SHA_CTX
{
/* SHA-1 uses this part of the union: */
struct
{
nzUInt32 state[5];
nzUInt64 bitcount;
nzUInt8 buffer[64];
} s1;
/* SHA-224 and SHA-256 use this part of the union: */
struct
{
nzUInt32 state[8];
nzUInt64 bitcount;
nzUInt8 buffer[64];
} s256;
/* SHA-384 and SHA-512 use this part of the union: */
struct
{
nzUInt64 state[8];
nzUInt64 bitcount[2];
nzUInt8 buffer[128];
} s512;
};
void SHA1_Init(SHA_CTX*);
void SHA1_Update(SHA_CTX*, const nzUInt8*, std::size_t);
void SHA1_End(SHA_CTX*, nzUInt8*);
void SHA224_Init(SHA_CTX*);
void SHA224_Update(SHA_CTX*, const nzUInt8*, std::size_t);
void SHA224_End(SHA_CTX*, nzUInt8*);
void SHA256_Init(SHA_CTX*);
void SHA256_Update(SHA_CTX*, const nzUInt8*, std::size_t);
void SHA256_End(SHA_CTX*, nzUInt8*);
void SHA384_Init(SHA_CTX*);
void SHA384_Update(SHA_CTX*, const nzUInt8*, std::size_t);
void SHA384_End(SHA_CTX*, nzUInt8*);
void SHA512_Init(SHA_CTX*);
void SHA512_Update(SHA_CTX*, const nzUInt8*, std::size_t);
void SHA512_End(SHA_CTX*, nzUInt8*);
#endif /* NAZARA_HASH_SHA2_INTERNAL_HPP */

View File

@ -0,0 +1,47 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Hash/SHA1.hpp>
#include <Nazara/Core/Hash/SHA/Internal.hpp>
#include <Nazara/Core/Debug.hpp>
NzHashSHA1::NzHashSHA1()
{
m_state = new SHA_CTX;
}
NzHashSHA1::~NzHashSHA1()
{
delete m_state;
}
void NzHashSHA1::Append(const nzUInt8* data, unsigned int len)
{
SHA1_Update(m_state, data, len);
}
void NzHashSHA1::Begin()
{
SHA1_Init(m_state);
}
NzHashDigest NzHashSHA1::End()
{
nzUInt8 digest[SHA1_DIGEST_LENGTH];
SHA1_End(m_state, digest);
return NzHashDigest(GetHashName(), digest, SHA1_DIGEST_LENGTH);
}
unsigned int NzHashSHA1::GetDigestLength()
{
return SHA1_DIGEST_LENGTH;
}
NzString NzHashSHA1::GetHashName()
{
static NzString hashName = "SHA1";
return hashName;
}

View File

@ -0,0 +1,47 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Hash/SHA224.hpp>
#include <Nazara/Core/Hash/SHA/Internal.hpp>
#include <Nazara/Core/Debug.hpp>
NzHashSHA224::NzHashSHA224()
{
m_state = new SHA_CTX;
}
NzHashSHA224::~NzHashSHA224()
{
delete m_state;
}
void NzHashSHA224::Append(const nzUInt8* data, unsigned int len)
{
SHA224_Update(m_state, data, len);
}
void NzHashSHA224::Begin()
{
SHA224_Init(m_state);
}
NzHashDigest NzHashSHA224::End()
{
nzUInt8 digest[SHA224_DIGEST_LENGTH];
SHA224_End(m_state, digest);
return NzHashDigest(GetHashName(), digest, SHA224_DIGEST_LENGTH);
}
unsigned int NzHashSHA224::GetDigestLength()
{
return SHA224_DIGEST_LENGTH;
}
NzString NzHashSHA224::GetHashName()
{
static NzString hashName = "SHA224";
return hashName;
}

View File

@ -0,0 +1,47 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Hash/SHA256.hpp>
#include <Nazara/Core/Hash/SHA/Internal.hpp>
#include <Nazara/Core/Debug.hpp>
NzHashSHA256::NzHashSHA256()
{
m_state = new SHA_CTX;
}
NzHashSHA256::~NzHashSHA256()
{
delete m_state;
}
void NzHashSHA256::Append(const nzUInt8* data, unsigned int len)
{
SHA256_Update(m_state, data, len);
}
void NzHashSHA256::Begin()
{
SHA256_Init(m_state);
}
NzHashDigest NzHashSHA256::End()
{
nzUInt8 digest[SHA256_DIGEST_LENGTH];
SHA256_End(m_state, digest);
return NzHashDigest(GetHashName(), digest, SHA256_DIGEST_LENGTH);
}
unsigned int NzHashSHA256::GetDigestLength()
{
return SHA256_DIGEST_LENGTH;
}
NzString NzHashSHA256::GetHashName()
{
static NzString hashName = "SHA256";
return hashName;
}

View File

@ -0,0 +1,47 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Hash/SHA384.hpp>
#include <Nazara/Core/Hash/SHA/Internal.hpp>
#include <Nazara/Core/Debug.hpp>
NzHashSHA384::NzHashSHA384()
{
m_state = new SHA_CTX;
}
NzHashSHA384::~NzHashSHA384()
{
delete m_state;
}
void NzHashSHA384::Append(const nzUInt8* data, unsigned int len)
{
SHA384_Update(m_state, data, len);
}
void NzHashSHA384::Begin()
{
SHA384_Init(m_state);
}
NzHashDigest NzHashSHA384::End()
{
nzUInt8 digest[SHA384_DIGEST_LENGTH];
SHA384_End(m_state, digest);
return NzHashDigest(GetHashName(), digest, SHA384_DIGEST_LENGTH);
}
unsigned int NzHashSHA384::GetDigestLength()
{
return SHA384_DIGEST_LENGTH;
}
NzString NzHashSHA384::GetHashName()
{
static NzString hashName = "SHA384";
return hashName;
}

View File

@ -0,0 +1,47 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Hash/SHA512.hpp>
#include <Nazara/Core/Hash/SHA/Internal.hpp>
#include <Nazara/Core/Debug.hpp>
NzHashSHA512::NzHashSHA512()
{
m_state = new SHA_CTX;
}
NzHashSHA512::~NzHashSHA512()
{
delete m_state;
}
void NzHashSHA512::Append(const nzUInt8* data, unsigned int len)
{
SHA512_Update(m_state, data, len);
}
void NzHashSHA512::Begin()
{
SHA512_Init(m_state);
}
NzHashDigest NzHashSHA512::End()
{
nzUInt8 digest[SHA512_DIGEST_LENGTH];
SHA512_End(m_state, digest);
return NzHashDigest(GetHashName(), digest, SHA512_DIGEST_LENGTH);
}
unsigned int NzHashSHA512::GetDigestLength()
{
return SHA512_DIGEST_LENGTH;
}
NzString NzHashSHA512::GetHashName()
{
static NzString hashName = "SHA512";
return hashName;
}

View File

@ -15,7 +15,7 @@ m_digestLength(0)
{
}
NzHashDigest::NzHashDigest(NzString hashName, const nzUInt8* digest, unsigned int length) :
NzHashDigest::NzHashDigest(const NzString& hashName, const nzUInt8* digest, unsigned int length) :
m_hashName(hashName),
m_digestLength(length)
{

View File

@ -2,12 +2,12 @@
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#define NAZARA_HASHABLE_CPP
#include <Nazara/Core/Hashable.hpp>
#include <Nazara/Core/Hash.hpp>
#include <Nazara/Core/Debug.hpp>
NzHashable::~NzHashable() = default;
NzHashDigest NzHashable::GetHash(nzHash hash) const
{
NzHash h(hash);

View File

@ -5,8 +5,6 @@
#include <Nazara/Core/StringStream.hpp>
#include <Nazara/Core/Debug.hpp>
#define NAZARA_STRINGSTREAM_CPP
NzStringStream::NzStringStream() :
m_bufferSize(0)
{