Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / src / shared / Auth / BigNumber.cpp
blob9d0a23599e64f2113e5dc146ef18e56d3ab8a92c
1 /*
2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
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.
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.
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
19 #include "Auth/BigNumber.h"
20 #include <openssl/bn.h>
21 #include <algorithm>
23 BigNumber::BigNumber()
25 _bn = BN_new();
26 _array = NULL;
29 BigNumber::BigNumber(const BigNumber &bn)
31 _bn = BN_dup(bn._bn);
32 _array = NULL;
35 BigNumber::BigNumber(uint32 val)
37 _bn = BN_new();
38 BN_set_word(_bn, val);
39 _array = NULL;
42 BigNumber::~BigNumber()
44 BN_free(_bn);
45 if(_array) delete[] _array;
48 void BigNumber::SetDword(uint32 val)
50 BN_set_word(_bn, val);
53 void BigNumber::SetQword(uint64 val)
55 BN_add_word(_bn, (uint32)(val >> 32));
56 BN_lshift(_bn, _bn, 32);
57 BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF));
60 void BigNumber::SetBinary(const uint8 *bytes, int len)
62 uint8 t[1000];
63 for (int i = 0; i < len; i++) t[i] = bytes[len - 1 - i];
64 BN_bin2bn(t, len, _bn);
67 void BigNumber::SetHexStr(const char *str)
69 BN_hex2bn(&_bn, str);
72 void BigNumber::SetRand(int numbits)
74 BN_rand(_bn, numbits, 0, 1);
77 BigNumber BigNumber::operator=(const BigNumber &bn)
79 BN_copy(_bn, bn._bn);
80 return *this;
83 BigNumber BigNumber::operator+=(const BigNumber &bn)
85 BN_add(_bn, _bn, bn._bn);
86 return *this;
89 BigNumber BigNumber::operator-=(const BigNumber &bn)
91 BN_sub(_bn, _bn, bn._bn);
92 return *this;
95 BigNumber BigNumber::operator*=(const BigNumber &bn)
97 BN_CTX *bnctx;
99 bnctx = BN_CTX_new();
100 BN_mul(_bn, _bn, bn._bn, bnctx);
101 BN_CTX_free(bnctx);
103 return *this;
106 BigNumber BigNumber::operator/=(const BigNumber &bn)
108 BN_CTX *bnctx;
110 bnctx = BN_CTX_new();
111 BN_div(_bn, NULL, _bn, bn._bn, bnctx);
112 BN_CTX_free(bnctx);
114 return *this;
117 BigNumber BigNumber::operator%=(const BigNumber &bn)
119 BN_CTX *bnctx;
121 bnctx = BN_CTX_new();
122 BN_mod(_bn, _bn, bn._bn, bnctx);
123 BN_CTX_free(bnctx);
125 return *this;
128 BigNumber BigNumber::Exp(const BigNumber &bn)
130 BigNumber ret;
131 BN_CTX *bnctx;
133 bnctx = BN_CTX_new();
134 BN_exp(ret._bn, _bn, bn._bn, bnctx);
135 BN_CTX_free(bnctx);
137 return ret;
140 BigNumber BigNumber::ModExp(const BigNumber &bn1, const BigNumber &bn2)
142 BigNumber ret;
143 BN_CTX *bnctx;
145 bnctx = BN_CTX_new();
146 BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
147 BN_CTX_free(bnctx);
149 return ret;
152 int BigNumber::GetNumBytes(void)
154 return BN_num_bytes(_bn);
157 uint32 BigNumber::AsDword()
159 return (uint32)BN_get_word(_bn);
162 uint8 *BigNumber::AsByteArray(int minSize)
164 int length = (minSize >= GetNumBytes()) ? minSize : GetNumBytes();
166 if (_array)
168 delete[] _array;
169 _array = NULL;
171 _array = new uint8[length];
173 // If we need more bytes than length of BigNumber set the rest to 0
174 if (length > GetNumBytes())
175 memset((void*)_array, 0, length);
177 BN_bn2bin(_bn, (unsigned char *)_array);
179 std::reverse(_array, _array + length);
181 return _array;
184 ByteBuffer BigNumber::AsByteBuffer()
186 ByteBuffer ret(GetNumBytes());
187 ret.append(AsByteArray(), GetNumBytes());
188 return ret;
191 std::vector<uint8> BigNumber::AsByteVector()
193 std::vector<uint8> ret;
194 ret.resize(GetNumBytes());
195 memcpy(&ret[0], AsByteArray(), GetNumBytes());
196 return ret;
199 const char *BigNumber::AsHexStr()
201 return BN_bn2hex(_bn);
204 const char *BigNumber::AsDecStr()
206 return BN_bn2dec(_bn);