From 680c12a4bfc3dfb181ee8751365abf203fcbb56d Mon Sep 17 00:00:00 2001 From: Shauren Date: Sat, 19 Mar 2016 23:15:54 +0100 Subject: Core/Crypto: Renamed SHA1Randx/WardenKeyGeneration and made it a template class --- src/common/Cryptography/SHA1.h | 3 + src/common/Cryptography/SHA256.h | 3 + src/common/Cryptography/SessionKeyGeneration.h | 82 ++++++++++++++++++++++++++ src/common/Cryptography/WardenKeyGeneration.h | 81 ------------------------- 4 files changed, 88 insertions(+), 81 deletions(-) create mode 100644 src/common/Cryptography/SessionKeyGeneration.h delete mode 100644 src/common/Cryptography/WardenKeyGeneration.h (limited to 'src/common') diff --git a/src/common/Cryptography/SHA1.h b/src/common/Cryptography/SHA1.h index ffa02176a2d..944350265f4 100644 --- a/src/common/Cryptography/SHA1.h +++ b/src/common/Cryptography/SHA1.h @@ -21,6 +21,7 @@ #include "Define.h" #include +#include #include class BigNumber; @@ -28,6 +29,8 @@ class BigNumber; class SHA1Hash { public: + typedef std::integral_constant DigestLength; + SHA1Hash(); ~SHA1Hash(); diff --git a/src/common/Cryptography/SHA256.h b/src/common/Cryptography/SHA256.h index d31c24ed93b..33f23f639a4 100644 --- a/src/common/Cryptography/SHA256.h +++ b/src/common/Cryptography/SHA256.h @@ -20,6 +20,7 @@ #include "Define.h" #include +#include #include class BigNumber; @@ -27,6 +28,8 @@ class BigNumber; class SHA256Hash { public: + typedef std::integral_constant DigestLength; + SHA256Hash(); ~SHA256Hash(); diff --git a/src/common/Cryptography/SessionKeyGeneration.h b/src/common/Cryptography/SessionKeyGeneration.h new file mode 100644 index 00000000000..0ec4479b765 --- /dev/null +++ b/src/common/Cryptography/SessionKeyGeneration.h @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2008-2016 TrinityCore + * Copyright (C) 2005-2011 MaNGOS + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef SessionKeyGeneration_h__ +#define SessionKeyGeneration_h__ + +#include "Common.h" + +template +class SessionKeyGenerator +{ +public: + SessionKeyGenerator(uint8* buff, uint32 size) + { + uint32 halfSize = size / 2; + + sh.Initialize(); + sh.UpdateData(buff, halfSize); + sh.Finalize(); + + memcpy(o1, sh.GetDigest(), Hash::DigestLength::value); + + sh.Initialize(); + sh.UpdateData(buff + halfSize, size - halfSize); + sh.Finalize(); + + memcpy(o2, sh.GetDigest(), Hash::DigestLength::value); + + memset(o0, 0x00, Hash::DigestLength::value); + + FillUp(); + } + + void Generate(uint8* buf, uint32 sz) + { + for (uint32 i = 0; i < sz; ++i) + { + if (taken == Hash::DigestLength::value) + FillUp(); + + buf[i] = o0[taken]; + taken++; + } + } + +private: + void FillUp() + { + sh.Initialize(); + sh.UpdateData(o1, Hash::DigestLength::value); + sh.UpdateData(o0, Hash::DigestLength::value); + sh.UpdateData(o2, Hash::DigestLength::value); + sh.Finalize(); + + memcpy(o0, sh.GetDigest(), Hash::DigestLength::value); + + taken = 0; + } + + Hash sh; + uint32 taken; + uint8 o0[Hash::DigestLength::value]; + uint8 o1[Hash::DigestLength::value]; + uint8 o2[Hash::DigestLength::value]; +}; + +#endif // SessionKeyGeneration_h__ diff --git a/src/common/Cryptography/WardenKeyGeneration.h b/src/common/Cryptography/WardenKeyGeneration.h deleted file mode 100644 index 18118ece649..00000000000 --- a/src/common/Cryptography/WardenKeyGeneration.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2008-2016 TrinityCore - * Copyright (C) 2005-2011 MaNGOS - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -#include "SHA1.h" - -#include - -#ifndef _WARDEN_KEY_GENERATION_H -#define _WARDEN_KEY_GENERATION_H - -class SHA1Randx -{ -public: - SHA1Randx(uint8* buff, uint32 size) - { - uint32 halfSize = size / 2; - - sh.Initialize(); - sh.UpdateData(buff, halfSize); - sh.Finalize(); - - memcpy(o1, sh.GetDigest(), 20); - - sh.Initialize(); - sh.UpdateData(buff + halfSize, size - halfSize); - sh.Finalize(); - - memcpy(o2, sh.GetDigest(), 20); - - memset(o0, 0x00, 20); - - FillUp(); - } - - void Generate(uint8* buf, uint32 sz) - { - for (uint32 i = 0; i < sz; ++i) - { - if (taken == 20) - FillUp(); - - buf[i] = o0[taken]; - taken++; - } - } - -private: - void FillUp() - { - sh.Initialize(); - sh.UpdateData(o1, 20); - sh.UpdateData(o0, 20); - sh.UpdateData(o2, 20); - sh.Finalize(); - - memcpy(o0, sh.GetDigest(), 20); - - taken = 0; - } - - SHA1Hash sh; - uint32 taken; - uint8 o0[20], o1[20], o2[20]; -}; - -#endif -- cgit v1.2.3