GClasses

GClasses::GBigInt Class Reference

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

#include <GBigInt.h>

List of all members.

Public Member Functions

 GBigInt ()
 GBigInt (GDomNode *pNode)
virtual ~GBigInt ()
GDomNodeserialize (GDom *pDoc)
 Marshal this object into a DOM that can be converted to a variety of serial formats.
bool getSign ()
 Returns true if the number is positive and false if it is negative.
void setSign (bool bSign)
 Makes the number positive if bSign is true and negative if bSign is false.
unsigned int getBitCount ()
 Returns the number of bits in the number.
bool getBit (unsigned int n)
 Returns the value of the nth bit where 0 represents the least significant bit (little endian)
void setBit (unsigned int nPos, bool bVal)
 Sets the value of the nth bit where 0 represents the least significant bit (little endian)
unsigned int getUIntCount ()
 Returns the number of unsigned integers required to represent this number.
unsigned int getUInt (unsigned int nPos)
 Returns the nth unsigned integer used to represent this number.
void setUInt (unsigned int nPos, unsigned int nVal)
 Sets the value of the nth unsigned integer used to represent this number.
void setToZero ()
 Sets the number to zero.
bool isZero ()
 Returns true if the number is zero.
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.
void copy (GBigInt *pBigNumber)
 Copies the value of pBigNumber into this object.
bool toHex (char *szBuff, int nBufferSize)
 Produces a big endian hexadecimal representation of this number.
bool fromHex (const char *szHexValue)
 Extract a value from a big endian hexadecimal string.
unsigned int * toBufferGiveOwnership ()
 This gives you ownership of the buffer. (You must delete it.) It also sets the value to zero.
bool toBuffer (unsigned int *pBuffer, int nBufferSize)
 Serializes the number. little-Endian (first bit in buffer will be LSB)
void fromBuffer (const unsigned int *pBuffer, int nBufferSize)
 Deserializes the number. little-Endian (first bit in buffer will be LSB)
void fromByteBuffer (const unsigned char *pBuffer, int nBufferChars)
 Deserializes the number.
void negate ()
 Multiplies the number by -1.
void increment ()
 Adds one to the number.
void decrement ()
 Subtracts one from the number.
void add (GBigInt *pBigNumber)
 Add another big number to this one.
void subtract (GBigInt *pBigNumber)
 Subtract another big number from this one.
void multiply (GBigInt *pBigNumber, unsigned int nUInt)
 Set this value to the product of another big number and an unsigned integer.
void multiply (GBigInt *pFirst, GBigInt *pSecond)
 Set this value to the product of two big numbers.
void divide (GBigInt *pInNominator, GBigInt *pInDenominator, GBigInt *pOutRemainder)
 Set this value to the ratio of two big numbers and return the remainder.
void shiftLeft (unsigned int nBits)
 Shift left (multiply by 2)
void shiftRight (unsigned int nBits)
 Shift right (divide by 2 and round down)
void Or (GBigInt *pBigNumber)
 bitwise or
void And (GBigInt *pBigNumber)
 bitwise and
void Xor (GBigInt *pBigNumber)
 bitwise xor
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.)
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)
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.
bool isPrime ()
 Output: true = pretty darn sure (like 99.999%) it's prime false = definately (100%) not prime.
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.
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.
void setRandom (unsigned int nBits)
 DO NOT use for crypto--This is NOT a cryptographic random number generator.

Protected Types

enum  { BITS_PER_INT = sizeof(unsigned int) * 8 }

Protected Member Functions

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

Protected Attributes

unsigned int m_nUInts
unsigned int * m_pBits
bool m_bSign

Detailed Description

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


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)

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]