root/trunk/src/shared/Auth/AuthCrypt.cpp @ 2

Revision 2, 2.1 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19#include "AuthCrypt.h"
20#include "Hmac.h"
21
22AuthCrypt::AuthCrypt()
23{
24    _initialized = false;
25}
26
27void AuthCrypt::Init()
28{
29    _send_i = _send_j = _recv_i = _recv_j = 0;
30    _initialized = true;
31}
32
33void AuthCrypt::DecryptRecv(uint8 *data, size_t len)
34{
35    if (!_initialized) return;
36    if (len < CRYPTED_RECV_LEN) return;
37
38    for (size_t t = 0; t < CRYPTED_RECV_LEN; t++)
39    {
40        _recv_i %= _key.size();
41        uint8 x = (data[t] - _recv_j) ^ _key[_recv_i];
42        ++_recv_i;
43        _recv_j = data[t];
44        data[t] = x;
45    }
46}
47
48void AuthCrypt::EncryptSend(uint8 *data, size_t len)
49{
50    if (!_initialized) return;
51    if (len < CRYPTED_SEND_LEN) return;
52
53    for (size_t t = 0; t < CRYPTED_SEND_LEN; t++)
54    {
55        _send_i %= _key.size();
56        uint8 x = (data[t] ^ _key[_send_i]) + _send_j;
57        ++_send_i;
58        data[t] = _send_j = x;
59    }
60}
61
62void AuthCrypt::SetKey(BigNumber *bn)
63{
64    uint8 *key = new uint8[SHA_DIGEST_LENGTH];
65    GenerateKey(key, bn);
66    _key.resize(SHA_DIGEST_LENGTH);
67    std::copy(key, key + SHA_DIGEST_LENGTH, _key.begin());
68    delete key;
69}
70
71AuthCrypt::~AuthCrypt()
72{
73}
74
75void AuthCrypt::GenerateKey(uint8 *key, BigNumber *bn)
76{
77    HmacHash hash;
78    hash.UpdateBigNumber(bn);
79    hash.Finalize();
80    memcpy(key, hash.GetDigest(), SHA_DIGEST_LENGTH);
81}
Note: See TracBrowser for help on using the browser.