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>
23 BigNumber::BigNumber()
29 BigNumber::BigNumber(const BigNumber
&bn
)
35 BigNumber::BigNumber(uint32 val
)
38 BN_set_word(_bn
, val
);
42 BigNumber::~BigNumber()
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
)
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
)
72 void BigNumber::SetRand(int numbits
)
74 BN_rand(_bn
, numbits
, 0, 1);
77 BigNumber
BigNumber::operator=(const BigNumber
&bn
)
83 BigNumber
BigNumber::operator+=(const BigNumber
&bn
)
85 BN_add(_bn
, _bn
, bn
._bn
);
89 BigNumber
BigNumber::operator-=(const BigNumber
&bn
)
91 BN_sub(_bn
, _bn
, bn
._bn
);
95 BigNumber
BigNumber::operator*=(const BigNumber
&bn
)
100 BN_mul(_bn
, _bn
, bn
._bn
, bnctx
);
106 BigNumber
BigNumber::operator/=(const BigNumber
&bn
)
110 bnctx
= BN_CTX_new();
111 BN_div(_bn
, NULL
, _bn
, bn
._bn
, bnctx
);
117 BigNumber
BigNumber::operator%=(const BigNumber
&bn
)
121 bnctx
= BN_CTX_new();
122 BN_mod(_bn
, _bn
, bn
._bn
, bnctx
);
128 BigNumber
BigNumber::Exp(const BigNumber
&bn
)
133 bnctx
= BN_CTX_new();
134 BN_exp(ret
._bn
, _bn
, bn
._bn
, bnctx
);
140 BigNumber
BigNumber::ModExp(const BigNumber
&bn1
, const BigNumber
&bn2
)
145 bnctx
= BN_CTX_new();
146 BN_mod_exp(ret
._bn
, _bn
, bn1
._bn
, bn2
._bn
, bnctx
);
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();
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
);
184 ByteBuffer
BigNumber::AsByteBuffer()
186 ByteBuffer
ret(GetNumBytes());
187 ret
.append(AsByteArray(), GetNumBytes());
191 std::vector
<uint8
> BigNumber::AsByteVector()
193 std::vector
<uint8
> ret
;
194 ret
.resize(GetNumBytes());
195 memcpy(&ret
[0], AsByteArray(), GetNumBytes());
199 const char *BigNumber::AsHexStr()
201 return BN_bn2hex(_bn
);
204 const char *BigNumber::AsDecStr()
206 return BN_bn2dec(_bn
);