root/trunk/src/shared/Auth/BigNumber.cpp @ 102

Revision 102, 4.1 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "Auth/BigNumber.h"
22#include <openssl/bn.h>
23#include <algorithm>
24
25BigNumber::BigNumber()
26{
27    _bn = BN_new();
28    _array = NULL;
29}
30
31BigNumber::BigNumber(const BigNumber &bn)
32{
33    _bn = BN_dup(bn._bn);
34    _array = NULL;
35}
36
37BigNumber::BigNumber(uint32 val)
38{
39    _bn = BN_new();
40    BN_set_word(_bn, val);
41    _array = NULL;
42}
43
44BigNumber::~BigNumber()
45{
46    BN_free(_bn);
47    if(_array) delete[] _array;
48}
49
50void BigNumber::SetDword(uint32 val)
51{
52    BN_set_word(_bn, val);
53}
54
55void BigNumber::SetQword(uint64 val)
56{
57    BN_add_word(_bn, (uint32)(val >> 32));
58    BN_lshift(_bn, _bn, 32);
59    BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF));
60}
61
62void BigNumber::SetBinary(const uint8 *bytes, int len)
63{
64    uint8 t[1000];
65    for (int i = 0; i < len; i++) t[i] = bytes[len - 1 - i];
66    BN_bin2bn(t, len, _bn);
67}
68
69void BigNumber::SetHexStr(const char *str)
70{
71    BN_hex2bn(&_bn, str);
72}
73
74void BigNumber::SetRand(int numbits)
75{
76    BN_rand(_bn, numbits, 0, 1);
77}
78
79BigNumber BigNumber::operator=(const BigNumber &bn)
80{
81    BN_copy(_bn, bn._bn);
82    return *this;
83}
84
85BigNumber BigNumber::operator+=(const BigNumber &bn)
86{
87    BN_add(_bn, _bn, bn._bn);
88    return *this;
89}
90
91BigNumber BigNumber::operator-=(const BigNumber &bn)
92{
93    BN_sub(_bn, _bn, bn._bn);
94    return *this;
95}
96
97BigNumber BigNumber::operator*=(const BigNumber &bn)
98{
99    BN_CTX *bnctx;
100
101    bnctx = BN_CTX_new();
102    BN_mul(_bn, _bn, bn._bn, bnctx);
103    BN_CTX_free(bnctx);
104
105    return *this;
106}
107
108BigNumber BigNumber::operator/=(const BigNumber &bn)
109{
110    BN_CTX *bnctx;
111
112    bnctx = BN_CTX_new();
113    BN_div(_bn, NULL, _bn, bn._bn, bnctx);
114    BN_CTX_free(bnctx);
115
116    return *this;
117}
118
119BigNumber BigNumber::operator%=(const BigNumber &bn)
120{
121    BN_CTX *bnctx;
122
123    bnctx = BN_CTX_new();
124    BN_mod(_bn, _bn, bn._bn, bnctx);
125    BN_CTX_free(bnctx);
126
127    return *this;
128}
129
130BigNumber BigNumber::Exp(const BigNumber &bn)
131{
132    BigNumber ret;
133    BN_CTX *bnctx;
134
135    bnctx = BN_CTX_new();
136    BN_exp(ret._bn, _bn, bn._bn, bnctx);
137    BN_CTX_free(bnctx);
138
139    return ret;
140}
141
142BigNumber BigNumber::ModExp(const BigNumber &bn1, const BigNumber &bn2)
143{
144    BigNumber ret;
145    BN_CTX *bnctx;
146
147    bnctx = BN_CTX_new();
148    BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
149    BN_CTX_free(bnctx);
150
151    return ret;
152}
153
154int BigNumber::GetNumBytes(void)
155{
156    return BN_num_bytes(_bn);
157}
158
159uint32 BigNumber::AsDword()
160{
161    return (uint32)BN_get_word(_bn);
162}
163
164uint8 *BigNumber::AsByteArray(int minSize)
165{
166    int length = (minSize >= GetNumBytes()) ? minSize : GetNumBytes();
167
168    if (_array)
169    {
170        delete[] _array;
171        _array = NULL;
172    }
173    _array = new uint8[length];
174
175    // If we need more bytes than length of BigNumber set the rest to 0
176    if (length > GetNumBytes())
177        memset((void*)_array, 0, length);
178   
179    BN_bn2bin(_bn, (unsigned char *)_array);
180
181    std::reverse(_array, _array + length);
182
183    return _array;
184}
185
186ByteBuffer BigNumber::AsByteBuffer()
187{
188    ByteBuffer ret(GetNumBytes());
189    ret.append(AsByteArray(), GetNumBytes());
190    return ret;
191}
192
193std::vector<uint8> BigNumber::AsByteVector()
194{
195    std::vector<uint8> ret;
196    ret.resize(GetNumBytes());
197    memcpy(&ret[0], AsByteArray(), GetNumBytes());
198    return ret;
199}
200
201const char *BigNumber::AsHexStr()
202{
203    return BN_bn2hex(_bn);
204}
205
206const char *BigNumber::AsDecStr()
207{
208    return BN_bn2dec(_bn);
209}
Note: See TracBrowser for help on using the browser.