GClasses
GClasses::GBigInt Class Reference

Detailed Description

Represents an integer of arbitrary size, and provides basic arithmetic functionality. Also contains functionality for implementing RSA symmetric-key cryptography.

#include <GBigInt.h>

Public Member Functions

 GBigInt ()
 
 GBigInt (GDomNode *pNode)
 
virtual ~GBigInt ()
 
void add (GBigInt *pBigNumber)
 Add another big number to this one. More...
 
void And (GBigInt *pBigNumber)
 bitwise and More...
 
void calculatePrivateKey (GBigInt *pPublicKey, GBigInt *pProd)
 Input: pProd is the product of (p - 1) * (q - 1) where p and q are prime pPublicKey is a number that has no common factors with pProd Output: this will become a private key to go with the public key. More...
 
int compareTo (GBigInt *pBigNumber)
 Returns -1 if this is less than pBigNumber Returns 0 if this is equal to pBigNumber Returns 1 if this is greater than pBigNumber. More...
 
void copy (GBigInt *pBigNumber)
 Copies the value of pBigNumber into this object. More...
 
void decrement ()
 Subtracts one from the number. More...
 
void divide (GBigInt *pInNominator, GBigInt *pInDenominator, GBigInt *pOutRemainder)
 Set this value to the ratio of two big numbers and return the remainder. More...
 
void euclid (GBigInt *pA1, GBigInt *pB1, GBigInt *pOutX=NULL, GBigInt *pOutY=NULL)
 Input: integers a, b Output: this will be set to the greatest common divisor of a,b. (If pOutX and pOutY are not NULL, they will be values such that "this" = ax + by.) More...
 
void fromBuffer (const unsigned int *pBuffer, int nBufferSize)
 Deserializes the number. little-Endian (first bit in buffer will be LSB) More...
 
void fromByteBuffer (const unsigned char *pBuffer, int nBufferChars)
 Deserializes the number. More...
 
bool fromHex (const char *szHexValue)
 Extract a value from a big endian hexadecimal string. More...
 
bool getBit (unsigned int n)
 Returns the value of the nth bit where 0 represents the least significant bit (little endian) More...
 
unsigned int getBitCount ()
 Returns the number of bits in the number. More...
 
bool getSign ()
 Returns true if the number is positive and false if it is negative. More...
 
unsigned int getUInt (unsigned int nPos)
 Returns the nth unsigned integer used to represent this number. More...
 
unsigned int getUIntCount ()
 Returns the number of unsigned integers required to represent this number. More...
 
void increment ()
 Adds one to the number. More...
 
bool isPrime ()
 Output: true = pretty darn sure (like 99.999%) it's prime false = definately (100%) not prime. More...
 
bool isZero ()
 Returns true if the number is zero. More...
 
bool millerRabin (GBigInt *pA)
 Input: "this" must be >= 3, and 2 <= a < "this" Output: "true" if this is either prime or a strong pseudoprime to base a, "false" otherwise. More...
 
void multiply (GBigInt *pBigNumber, unsigned int nUInt)
 Set this value to the product of another big number and an unsigned integer. More...
 
void multiply (GBigInt *pFirst, GBigInt *pSecond)
 Set this value to the product of two big numbers. More...
 
void negate ()
 Multiplies the number by -1. More...
 
void Or (GBigInt *pBigNumber)
 bitwise or More...
 
void powerMod (GBigInt *pA, GBigInt *pK, GBigInt *pN)
 Input: a, k>=0, n>=2 Output: this will be set to ((a raised to the power of k) modulus n) More...
 
void selectPublicKey (const unsigned int *pRandomData, int nRandomDataUInts, GBigInt *pProd)
 Input: pProd is the product of (p - 1) * (q - 1) where p and q are prime pRandomData is some random data that will be used to pick the key. Output: It will return a key that has no common factors with pProd. It starts with the random data you provide and increments it until it fits this criteria. More...
 
GDomNodeserialize (GDom *pDoc) const
 Marshal this object into a DOM that can be converted to a variety of serial formats. More...
 
void setBit (unsigned int nPos, bool bVal)
 Sets the value of the nth bit where 0 represents the least significant bit (little endian) More...
 
void setRandom (unsigned int nBits)
 DO NOT use for crypto–This is NOT a cryptographic random number generator. More...
 
void setSign (bool bSign)
 Makes the number positive if bSign is true and negative if bSign is false. More...
 
void setToZero ()
 Sets the number to zero. More...
 
void setUInt (unsigned int nPos, unsigned int nVal)
 Sets the value of the nth unsigned integer used to represent this number. More...
 
void shiftLeft (unsigned int nBits)
 Shift left (multiply by 2) More...
 
void shiftRight (unsigned int nBits)
 Shift right (divide by 2 and round down) More...
 
void subtract (GBigInt *pBigNumber)
 Subtract another big number from this one. More...
 
bool toBuffer (unsigned int *pBuffer, int nBufferSize)
 Serializes the number. little-Endian (first bit in buffer will be LSB) More...
 
unsigned int * toBufferGiveOwnership ()
 This gives you ownership of the buffer. (You must delete it.) It also sets the value to zero. More...
 
bool toHex (char *szBuff, int nBufferSize)
 Produces a big endian hexadecimal representation of this number. More...
 
void Xor (GBigInt *pBigNumber)
 bitwise xor More...
 

Protected Types

Protected Member Functions

void resize (unsigned int nBits)
 
void shiftLeftBits (unsigned int nBits)
 
void shiftLeftUInts (unsigned int nBits)
 
void shiftRightBits (unsigned int nBits)
 
void shiftRightUInts (unsigned int nBits)
 

Protected Attributes

bool m_bSign
 
unsigned int m_nUInts
 
unsigned int * m_pBits
 

Member Enumeration Documentation

anonymous enum
protected
Enumerator
BITS_PER_INT 

Constructor & Destructor Documentation

GClasses::GBigInt::GBigInt ( )
GClasses::GBigInt::GBigInt ( GDomNode pNode)
virtual GClasses::GBigInt::~GBigInt ( )
virtual

Member Function Documentation

void GClasses::GBigInt::add ( GBigInt pBigNumber)

Add another big number to this one.

void GClasses::GBigInt::And ( GBigInt pBigNumber)

bitwise and

void GClasses::GBigInt::calculatePrivateKey ( GBigInt pPublicKey,
GBigInt pProd 
)

Input: pProd is the product of (p - 1) * (q - 1) where p and q are prime pPublicKey is a number that has no common factors with pProd Output: this will become a private key to go with the public key.

int GClasses::GBigInt::compareTo ( GBigInt pBigNumber)

Returns -1 if this is less than pBigNumber Returns 0 if this is equal to pBigNumber Returns 1 if this is greater than pBigNumber.

void GClasses::GBigInt::copy ( GBigInt pBigNumber)

Copies the value of pBigNumber into this object.

void GClasses::GBigInt::decrement ( )

Subtracts one from the number.

void GClasses::GBigInt::divide ( GBigInt pInNominator,
GBigInt pInDenominator,
GBigInt pOutRemainder 
)

Set this value to the ratio of two big numbers and return the remainder.

void GClasses::GBigInt::euclid ( GBigInt pA1,
GBigInt pB1,
GBigInt pOutX = NULL,
GBigInt pOutY = NULL 
)

Input: integers a, b Output: this will be set to the greatest common divisor of a,b. (If pOutX and pOutY are not NULL, they will be values such that "this" = ax + by.)

void GClasses::GBigInt::fromBuffer ( const unsigned int *  pBuffer,
int  nBufferSize 
)

Deserializes the number. little-Endian (first bit in buffer will be LSB)

void GClasses::GBigInt::fromByteBuffer ( const unsigned char *  pBuffer,
int  nBufferChars 
)

Deserializes the number.

bool GClasses::GBigInt::fromHex ( const char *  szHexValue)

Extract a value from a big endian hexadecimal string.

bool GClasses::GBigInt::getBit ( unsigned int  n)
inline

Returns the value of the nth bit where 0 represents the least significant bit (little endian)

unsigned int GClasses::GBigInt::getBitCount ( )

Returns the number of bits in the number.

bool GClasses::GBigInt::getSign ( )
inline

Returns true if the number is positive and false if it is negative.

unsigned int GClasses::GBigInt::getUInt ( unsigned int  nPos)
inline

Returns the nth unsigned integer used to represent this number.

unsigned int GClasses::GBigInt::getUIntCount ( )
inline

Returns the number of unsigned integers required to represent this number.

void GClasses::GBigInt::increment ( )

Adds one to the number.

bool GClasses::GBigInt::isPrime ( )

Output: true = pretty darn sure (like 99.999%) it's prime false = definately (100%) not prime.

bool GClasses::GBigInt::isZero ( )

Returns true if the number is zero.

bool GClasses::GBigInt::millerRabin ( GBigInt pA)

Input: "this" must be >= 3, and 2 <= a < "this" Output: "true" if this is either prime or a strong pseudoprime to base a, "false" otherwise.

void GClasses::GBigInt::multiply ( GBigInt pBigNumber,
unsigned int  nUInt 
)

Set this value to the product of another big number and an unsigned integer.

void GClasses::GBigInt::multiply ( GBigInt pFirst,
GBigInt pSecond 
)

Set this value to the product of two big numbers.

void GClasses::GBigInt::negate ( )

Multiplies the number by -1.

void GClasses::GBigInt::Or ( GBigInt pBigNumber)

bitwise or

void GClasses::GBigInt::powerMod ( GBigInt pA,
GBigInt pK,
GBigInt pN 
)

Input: a, k>=0, n>=2 Output: this will be set to ((a raised to the power of k) modulus n)

void GClasses::GBigInt::resize ( unsigned int  nBits)
protected
void GClasses::GBigInt::selectPublicKey ( const unsigned int *  pRandomData,
int  nRandomDataUInts,
GBigInt pProd 
)

Input: pProd is the product of (p - 1) * (q - 1) where p and q are prime pRandomData is some random data that will be used to pick the key. Output: It will return a key that has no common factors with pProd. It starts with the random data you provide and increments it until it fits this criteria.

GDomNode* GClasses::GBigInt::serialize ( GDom pDoc) const

Marshal this object into a DOM that can be converted to a variety of serial formats.

void GClasses::GBigInt::setBit ( unsigned int  nPos,
bool  bVal 
)

Sets the value of the nth bit where 0 represents the least significant bit (little endian)

void GClasses::GBigInt::setRandom ( unsigned int  nBits)

DO NOT use for crypto–This is NOT a cryptographic random number generator.

void GClasses::GBigInt::setSign ( bool  bSign)
inline

Makes the number positive if bSign is true and negative if bSign is false.

void GClasses::GBigInt::setToZero ( )

Sets the number to zero.

void GClasses::GBigInt::setUInt ( unsigned int  nPos,
unsigned int  nVal 
)

Sets the value of the nth unsigned integer used to represent this number.

void GClasses::GBigInt::shiftLeft ( unsigned int  nBits)

Shift left (multiply by 2)

void GClasses::GBigInt::shiftLeftBits ( unsigned int  nBits)
protected
void GClasses::GBigInt::shiftLeftUInts ( unsigned int  nBits)
protected
void GClasses::GBigInt::shiftRight ( unsigned int  nBits)

Shift right (divide by 2 and round down)

void GClasses::GBigInt::shiftRightBits ( unsigned int  nBits)
protected
void GClasses::GBigInt::shiftRightUInts ( unsigned int  nBits)
protected
void GClasses::GBigInt::subtract ( GBigInt pBigNumber)

Subtract another big number from this one.

bool GClasses::GBigInt::toBuffer ( unsigned int *  pBuffer,
int  nBufferSize 
)

Serializes the number. little-Endian (first bit in buffer will be LSB)

unsigned int* GClasses::GBigInt::toBufferGiveOwnership ( )

This gives you ownership of the buffer. (You must delete it.) It also sets the value to zero.

bool GClasses::GBigInt::toHex ( char *  szBuff,
int  nBufferSize 
)

Produces a big endian hexadecimal representation of this number.

void GClasses::GBigInt::Xor ( GBigInt pBigNumber)

bitwise xor

Member Data Documentation

bool GClasses::GBigInt::m_bSign
protected
unsigned int GClasses::GBigInt::m_nUInts
protected
unsigned int* GClasses::GBigInt::m_pBits
protected