OregonCore  revision fb2a440-git
Your Favourite TBC server
BigNumber Class Reference

#include <BigNumber.h>

Public Member Functions

 BigNumber ()
 
 BigNumber (const BigNumber &bn)
 
 BigNumber (uint32)
 
 ~BigNumber ()
 
void SetDword (uint32)
 
void SetQword (uint64)
 
void SetBinary (const uint8 *bytes, int len)
 
void SetHexStr (const char *str)
 
void SetRand (int numbits)
 
BigNumber operator= (const BigNumber &bn)
 
BigNumber operator+= (const BigNumber &bn)
 
BigNumber operator+ (const BigNumber &bn)
 
BigNumber operator-= (const BigNumber &bn)
 
BigNumber operator- (const BigNumber &bn)
 
BigNumber operator*= (const BigNumber &bn)
 
BigNumber operator* (const BigNumber &bn)
 
BigNumber operator/= (const BigNumber &bn)
 
BigNumber operator/ (const BigNumber &bn)
 
BigNumber operator%= (const BigNumber &bn)
 
BigNumber operator% (const BigNumber &bn)
 
bool isZero () const
 
BigNumber ModExp (const BigNumber &bn1, const BigNumber &bn2)
 
BigNumber Exp (const BigNumber &)
 
int GetNumBytes (void)
 
struct bignum_st * BN ()
 
uint32 AsDword ()
 
uint8AsByteArray (int minSize=0, bool reverse=true)
 
ByteBuffer AsByteBuffer ()
 
std::vector< uint8AsByteVector ()
 
const char * AsHexStr ()
 
const char * AsDecStr ()
 

Private Attributes

struct bignum_st * _bn
 
uint8_array
 

Detailed Description

Definition at line 26 of file BigNumber.h.

Constructor & Destructor Documentation

BigNumber::BigNumber ( )

Definition at line 22 of file BigNumber.cpp.

References _array, and _bn.

23 {
24  _bn = BN_new();
25  _array = NULL;
26 }
uint8 * _array
Definition: BigNumber.h:96
struct bignum_st * _bn
Definition: BigNumber.h:95
BigNumber::BigNumber ( const BigNumber bn)

Definition at line 28 of file BigNumber.cpp.

References _array, and _bn.

29 {
30  _bn = BN_dup(bn._bn);
31  _array = NULL;
32 }
uint8 * _array
Definition: BigNumber.h:96
struct bignum_st * _bn
Definition: BigNumber.h:95
BigNumber::BigNumber ( uint32  val)

Definition at line 34 of file BigNumber.cpp.

References _array, and _bn.

35 {
36  _bn = BN_new();
37  BN_set_word(_bn, val);
38  _array = NULL;
39 }
uint8 * _array
Definition: BigNumber.h:96
struct bignum_st * _bn
Definition: BigNumber.h:95
BigNumber::~BigNumber ( )

Definition at line 41 of file BigNumber.cpp.

References _array, and _bn.

42 {
43  BN_free(_bn);
44  if (_array) delete[] _array;
45 }
uint8 * _array
Definition: BigNumber.h:96
struct bignum_st * _bn
Definition: BigNumber.h:95

Member Function Documentation

uint8 * BigNumber::AsByteArray ( int  minSize = 0,
bool  reverse = true 
)

Definition at line 166 of file BigNumber.cpp.

References _array, _bn, and GetNumBytes().

Referenced by AuthSocket::_HandleLogonProof(), AuthSocket::_SetVSFields(), AsByteBuffer(), AsByteVector(), BN(), WardenWin::HandleData(), WardenMac::Init(), WardenWin::Init(), CWardenDataStorage::LoadWardenDataResult(), WardenWin::RequestData(), HmacHash::UpdateBigNumber(), and Sha1Hash::UpdateBigNumbers().

167 {
168  int length = (minSize >= GetNumBytes()) ? minSize : GetNumBytes();
169 
170  if (_array)
171  {
172  delete[] _array;
173  _array = NULL;
174  }
175  _array = new uint8[length];
176 
177  // If we need more bytes than length of BigNumber set the rest to 0
178  if (length > GetNumBytes())
179  memset((void*)_array, 0, length);
180 
181  BN_bn2bin(_bn, (unsigned char*)_array);
182 
183  if (reverse)
184  std::reverse(_array, _array + length);
185 
186  return _array;
187 }
int GetNumBytes(void)
Definition: BigNumber.cpp:151
uint8 * _array
Definition: BigNumber.h:96
ACE_UINT8 uint8
Definition: Define.h:73
struct bignum_st * _bn
Definition: BigNumber.h:95
ByteBuffer BigNumber::AsByteBuffer ( )

Definition at line 189 of file BigNumber.cpp.

References ByteBuffer::append(), AsByteArray(), and GetNumBytes().

Referenced by BN().

190 {
191  ByteBuffer ret(GetNumBytes());
192  ret.append(AsByteArray(), GetNumBytes());
193  return ret;
194 }
uint8 * AsByteArray(int minSize=0, bool reverse=true)
Definition: BigNumber.cpp:166
int GetNumBytes(void)
Definition: BigNumber.cpp:151
std::vector< uint8 > BigNumber::AsByteVector ( )

Definition at line 196 of file BigNumber.cpp.

References AsByteArray(), and GetNumBytes().

Referenced by BN().

197 {
198  std::vector<uint8> ret;
199  ret.resize(GetNumBytes());
200  memcpy(&ret[0], AsByteArray(), GetNumBytes());
201  return ret;
202 }
uint8 * AsByteArray(int minSize=0, bool reverse=true)
Definition: BigNumber.cpp:166
int GetNumBytes(void)
Definition: BigNumber.cpp:151
const char * BigNumber::AsDecStr ( )

Definition at line 209 of file BigNumber.cpp.

References _bn.

Referenced by BN().

210 {
211  return BN_bn2dec(_bn);
212 }
struct bignum_st * _bn
Definition: BigNumber.h:95
uint32 BigNumber::AsDword ( )

Definition at line 156 of file BigNumber.cpp.

References _bn.

Referenced by BN().

157 {
158  return (uint32)BN_get_word(_bn);
159 }
struct bignum_st * _bn
Definition: BigNumber.h:95
ACE_UINT32 uint32
Definition: Define.h:71
const char * BigNumber::AsHexStr ( )

Definition at line 204 of file BigNumber.cpp.

References _bn.

Referenced by BN(), and WorldSocket::HandleAuthSession().

205 {
206  return BN_bn2hex(_bn);
207 }
struct bignum_st * _bn
Definition: BigNumber.h:95
struct bignum_st* BigNumber::BN ( )
inline

Definition at line 81 of file BigNumber.h.

References _bn, AsByteArray(), AsByteBuffer(), AsByteVector(), AsDecStr(), AsDword(), and AsHexStr().

82  {
83  return _bn;
84  }
struct bignum_st * _bn
Definition: BigNumber.h:95
BigNumber BigNumber::Exp ( const BigNumber bn)

Definition at line 127 of file BigNumber.cpp.

References _bn.

Referenced by operator%().

128 {
129  BigNumber ret;
130  BN_CTX* bnctx;
131 
132  bnctx = BN_CTX_new();
133  BN_exp(ret._bn, _bn, bn._bn, bnctx);
134  BN_CTX_free(bnctx);
135 
136  return ret;
137 }
struct bignum_st * _bn
Definition: BigNumber.h:95
bool BigNumber::isZero ( ) const

Definition at line 161 of file BigNumber.cpp.

References _bn.

Referenced by AuthSocket::_HandleLogonProof(), and operator%().

162 {
163  return BN_is_zero(_bn) != 0;
164 }
struct bignum_st * _bn
Definition: BigNumber.h:95
BigNumber BigNumber::ModExp ( const BigNumber bn1,
const BigNumber bn2 
)

Definition at line 139 of file BigNumber.cpp.

References _bn.

Referenced by AuthSocket::_HandleLogonChallenge(), AuthSocket::_HandleLogonProof(), and operator%().

140 {
141  BigNumber ret;
142  BN_CTX* bnctx;
143 
144  bnctx = BN_CTX_new();
145  BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
146  BN_CTX_free(bnctx);
147 
148  return ret;
149 }
struct bignum_st * _bn
Definition: BigNumber.h:95
BigNumber BigNumber::operator% ( const BigNumber bn)
inline

Definition at line 68 of file BigNumber.h.

References Exp(), GetNumBytes(), isZero(), and ModExp().

69  {
70  BigNumber t(*this);
71  return t %= bn;
72  }
BigNumber BigNumber::operator%= ( const BigNumber bn)

Definition at line 116 of file BigNumber.cpp.

References _bn.

Referenced by operator/().

117 {
118  BN_CTX* bnctx;
119 
120  bnctx = BN_CTX_new();
121  BN_mod(_bn, _bn, bn._bn, bnctx);
122  BN_CTX_free(bnctx);
123 
124  return *this;
125 }
struct bignum_st * _bn
Definition: BigNumber.h:95
BigNumber BigNumber::operator* ( const BigNumber bn)
inline

Definition at line 56 of file BigNumber.h.

References operator/=().

57  {
58  BigNumber t(*this);
59  return t *= bn;
60  }
BigNumber BigNumber::operator*= ( const BigNumber bn)

Definition at line 94 of file BigNumber.cpp.

References _bn.

Referenced by operator-().

95 {
96  BN_CTX* bnctx;
97 
98  bnctx = BN_CTX_new();
99  BN_mul(_bn, _bn, bn._bn, bnctx);
100  BN_CTX_free(bnctx);
101 
102  return *this;
103 }
struct bignum_st * _bn
Definition: BigNumber.h:95
BigNumber BigNumber::operator+ ( const BigNumber bn)
inline

Definition at line 44 of file BigNumber.h.

References operator-=().

45  {
46  BigNumber t(*this);
47  return t += bn;
48  }
BigNumber BigNumber::operator+= ( const BigNumber bn)

Definition at line 82 of file BigNumber.cpp.

References _bn.

83 {
84  BN_add(_bn, _bn, bn._bn);
85  return *this;
86 }
struct bignum_st * _bn
Definition: BigNumber.h:95
BigNumber BigNumber::operator- ( const BigNumber bn)
inline

Definition at line 50 of file BigNumber.h.

References operator*=().

51  {
52  BigNumber t(*this);
53  return t -= bn;
54  }
BigNumber BigNumber::operator-= ( const BigNumber bn)

Definition at line 88 of file BigNumber.cpp.

References _bn.

Referenced by operator+().

89 {
90  BN_sub(_bn, _bn, bn._bn);
91  return *this;
92 }
struct bignum_st * _bn
Definition: BigNumber.h:95
BigNumber BigNumber::operator/ ( const BigNumber bn)
inline

Definition at line 62 of file BigNumber.h.

References operator%=().

63  {
64  BigNumber t(*this);
65  return t /= bn;
66  }
BigNumber BigNumber::operator/= ( const BigNumber bn)

Definition at line 105 of file BigNumber.cpp.

References _bn.

Referenced by operator*().

106 {
107  BN_CTX* bnctx;
108 
109  bnctx = BN_CTX_new();
110  BN_div(_bn, NULL, _bn, bn._bn, bnctx);
111  BN_CTX_free(bnctx);
112 
113  return *this;
114 }
struct bignum_st * _bn
Definition: BigNumber.h:95
BigNumber BigNumber::operator= ( const BigNumber bn)

Definition at line 76 of file BigNumber.cpp.

References _bn.

77 {
78  BN_copy(_bn, bn._bn);
79  return *this;
80 }
struct bignum_st * _bn
Definition: BigNumber.h:95
void BigNumber::SetBinary ( const uint8 bytes,
int  len 
)

Definition at line 59 of file BigNumber.cpp.

References _bn.

Referenced by AuthSocket::_HandleLogonProof(), AuthSocket::_HandleReconnectProof(), AuthSocket::_SetVSFields(), and CWardenDataStorage::LoadWardenDataResult().

60 {
61  uint8 t[1000];
62  for (int i = 0; i < len; i++) t[i] = bytes[len - 1 - i];
63  BN_bin2bn(t, len, _bn);
64 }
ACE_UINT8 uint8
Definition: Define.h:73
struct bignum_st * _bn
Definition: BigNumber.h:95
void BigNumber::SetDword ( uint32  val)

Definition at line 47 of file BigNumber.cpp.

References _bn.

Referenced by WorldSocket::HandleAuthSession().

48 {
49  BN_set_word(_bn, val);
50 }
struct bignum_st * _bn
Definition: BigNumber.h:95
void BigNumber::SetHexStr ( const char *  str)

Definition at line 66 of file BigNumber.cpp.

References _bn.

Referenced by AuthSocket::_SetVSFields(), WorldSocket::HandleAuthSession(), and CWardenDataStorage::LoadWardenDataResult().

67 {
68  BN_hex2bn(&_bn, str);
69 }
struct bignum_st * _bn
Definition: BigNumber.h:95
void BigNumber::SetQword ( uint64  val)

Definition at line 52 of file BigNumber.cpp.

References _bn.

53 {
54  BN_add_word(_bn, (uint32)(val >> 32));
55  BN_lshift(_bn, _bn, 32);
56  BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF));
57 }
struct bignum_st * _bn
Definition: BigNumber.h:95
ACE_UINT32 uint32
Definition: Define.h:71
void BigNumber::SetRand ( int  numbits)

Definition at line 71 of file BigNumber.cpp.

References _bn.

Referenced by AuthSocket::_HandleLogonChallenge().

72 {
73  BN_rand(_bn, numbits, 0, 1);
74 }
struct bignum_st * _bn
Definition: BigNumber.h:95

Member Data Documentation

uint8* BigNumber::_array
private

Definition at line 96 of file BigNumber.h.

Referenced by AsByteArray(), BigNumber(), and ~BigNumber().


The documentation for this class was generated from the following files: