@@ -3140,6 +3140,11 @@ void AppleRemoteDevice::stop() throw() | |||||
} | } | ||||
} | } | ||||
bool AppleRemoteDevice::isActive() const throw() | |||||
{ | |||||
return queue != 0; | |||||
} | |||||
static void appleRemoteQueueCallback (void* const target, const IOReturn result, void*, void*) | static void appleRemoteQueueCallback (void* const target, const IOReturn result, void*, void*) | ||||
{ | { | ||||
if (result == kIOReturnSuccess) | if (result == kIOReturnSuccess) | ||||
@@ -38,34 +38,29 @@ BEGIN_JUCE_NAMESPACE | |||||
//============================================================================== | //============================================================================== | ||||
static void createSmallSieve (int numBits, BitArray& result) | |||||
static void createSmallSieve (const int numBits, BitArray& result) throw() | |||||
{ | { | ||||
result.setBit (numBits); | result.setBit (numBits); | ||||
result.clearBit (numBits); // to enlarge the array | result.clearBit (numBits); // to enlarge the array | ||||
result.setBit (0); | result.setBit (0); | ||||
int index = 1; | |||||
int n = 2; | |||||
do | do | ||||
{ | { | ||||
const int step = (index << 1) + 1; | |||||
for (int i = index + step; i < numBits; i += step) | |||||
{ | |||||
jassert (i != 6); | |||||
for (int i = n + n; i < numBits; i += n) | |||||
result.setBit (i); | result.setBit (i); | ||||
} | |||||
index = result.findNextClearBit (index + 1); | |||||
n = result.findNextClearBit (n + 1); | |||||
} | } | ||||
while (index < numBits); | |||||
while (n <= (numBits >> 1)); | |||||
} | } | ||||
static void bigSieve (const BitArray& base, | static void bigSieve (const BitArray& base, | ||||
int numBits, | |||||
const int numBits, | |||||
BitArray& result, | BitArray& result, | ||||
const BitArray& smallSieve, | const BitArray& smallSieve, | ||||
const int smallSieveSize) | |||||
const int smallSieveSize) throw() | |||||
{ | { | ||||
jassert (! base[0]); // must be even! | jassert (! base[0]); // must be even! | ||||
@@ -105,9 +100,9 @@ static void bigSieve (const BitArray& base, | |||||
static bool findCandidate (const BitArray& base, | static bool findCandidate (const BitArray& base, | ||||
const BitArray& sieve, | const BitArray& sieve, | ||||
int numBits, | |||||
const int numBits, | |||||
BitArray& result, | BitArray& result, | ||||
int certainty) | |||||
const int certainty) throw() | |||||
{ | { | ||||
for (int i = 0; i < numBits; ++i) | for (int i = 0; i < numBits; ++i) | ||||
{ | { | ||||
@@ -125,7 +120,8 @@ static bool findCandidate (const BitArray& base, | |||||
} | } | ||||
//============================================================================== | //============================================================================== | ||||
const BitArray Primes::createProbablePrime (int bitLength, int certainty) | |||||
const BitArray Primes::createProbablePrime (const int bitLength, | |||||
const int certainty) throw() | |||||
{ | { | ||||
BitArray smallSieve; | BitArray smallSieve; | ||||
const int smallSieveSize = 15000; | const int smallSieveSize = 15000; | ||||
@@ -156,7 +152,7 @@ const BitArray Primes::createProbablePrime (int bitLength, int certainty) | |||||
return BitArray(); | return BitArray(); | ||||
} | } | ||||
static bool passesMillerRabin (const BitArray& n, int iterations) | |||||
static bool passesMillerRabin (const BitArray& n, int iterations) throw() | |||||
{ | { | ||||
const BitArray one (1); | const BitArray one (1); | ||||
const BitArray two (2); | const BitArray two (2); | ||||
@@ -168,10 +164,28 @@ static bool passesMillerRabin (const BitArray& n, int iterations) | |||||
const int s = d.findNextSetBit (0); | const int s = d.findNextSetBit (0); | ||||
d.shiftBits (-s); | d.shiftBits (-s); | ||||
BitArray smallPrimes; | |||||
int numBitsInSmallPrimes = 0; | |||||
for (;;) | |||||
{ | |||||
numBitsInSmallPrimes += 256; | |||||
createSmallSieve (numBitsInSmallPrimes, smallPrimes); | |||||
const int numPrimesFound = numBitsInSmallPrimes - smallPrimes.countNumberOfSetBits(); | |||||
if (numPrimesFound > iterations + 1) | |||||
break; | |||||
} | |||||
int smallPrime = 2; | |||||
while (--iterations >= 0) | while (--iterations >= 0) | ||||
{ | { | ||||
BitArray r; | |||||
r.createRandomNumber (nMinusOne); | |||||
smallPrime = smallPrimes.findNextClearBit (smallPrime + 1); | |||||
BitArray r (smallPrime); | |||||
//r.createRandomNumber (nMinusOne); | |||||
r.exponentModulo (d, n); | r.exponentModulo (d, n); | ||||
if (! (r == one || r == nMinusOne)) | if (! (r == one || r == nMinusOne)) | ||||
@@ -192,7 +206,8 @@ static bool passesMillerRabin (const BitArray& n, int iterations) | |||||
return true; | return true; | ||||
} | } | ||||
bool Primes::isProbablyPrime (const BitArray& number, int certainty) | |||||
bool Primes::isProbablyPrime (const BitArray& number, | |||||
const int certainty) throw() | |||||
{ | { | ||||
if (! number[0]) | if (! number[0]) | ||||
return false; | return false; | ||||
@@ -209,6 +224,11 @@ bool Primes::isProbablyPrime (const BitArray& number, int certainty) | |||||
} | } | ||||
else | else | ||||
{ | { | ||||
const BitArray screen (2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23); | |||||
if (number.findGreatestCommonDivisor (screen) != BitArray (1)) | |||||
return false; | |||||
return passesMillerRabin (number, certainty); | return passesMillerRabin (number, certainty); | ||||
} | } | ||||
} | } | ||||
@@ -53,7 +53,7 @@ public: | |||||
for primality. A safe value might be anything over about 20-30. | for primality. A safe value might be anything over about 20-30. | ||||
*/ | */ | ||||
static const BitArray createProbablePrime (int bitLength, | static const BitArray createProbablePrime (int bitLength, | ||||
int certainty); | |||||
int certainty) throw(); | |||||
/** Tests a number to see if it's prime. | /** Tests a number to see if it's prime. | ||||
@@ -64,7 +64,7 @@ public: | |||||
safe value might be anything over about 20-30. | safe value might be anything over about 20-30. | ||||
*/ | */ | ||||
static bool isProbablyPrime (const BitArray& number, | static bool isProbablyPrime (const BitArray& number, | ||||
int certainty); | |||||
int certainty) throw(); | |||||
}; | }; | ||||
@@ -39,11 +39,11 @@ BEGIN_JUCE_NAMESPACE | |||||
//============================================================================== | //============================================================================== | ||||
RSAKey::RSAKey() | |||||
RSAKey::RSAKey() throw() | |||||
{ | { | ||||
} | } | ||||
RSAKey::RSAKey (const String& s) | |||||
RSAKey::RSAKey (const String& s) throw() | |||||
{ | { | ||||
if (s.containsChar (T(','))) | if (s.containsChar (T(','))) | ||||
{ | { | ||||
@@ -57,7 +57,7 @@ RSAKey::RSAKey (const String& s) | |||||
} | } | ||||
} | } | ||||
RSAKey::~RSAKey() | |||||
RSAKey::~RSAKey() throw() | |||||
{ | { | ||||
} | } | ||||
@@ -66,7 +66,7 @@ const String RSAKey::toString() const throw() | |||||
return part1.toString (16) + T(",") + part2.toString (16); | return part1.toString (16) + T(",") + part2.toString (16); | ||||
} | } | ||||
bool RSAKey::applyToValue (BitArray& value) const | |||||
bool RSAKey::applyToValue (BitArray& value) const throw() | |||||
{ | { | ||||
if (part1.isEmpty() || part2.isEmpty() | if (part1.isEmpty() || part2.isEmpty() | ||||
|| value.compare (0) <= 0) | || value.compare (0) <= 0) | ||||
@@ -96,7 +96,7 @@ bool RSAKey::applyToValue (BitArray& value) const | |||||
} | } | ||||
static const BitArray findBestCommonDivisor (const BitArray& p, | static const BitArray findBestCommonDivisor (const BitArray& p, | ||||
const BitArray& q) | |||||
const BitArray& q) throw() | |||||
{ | { | ||||
const BitArray one (1); | const BitArray one (1); | ||||
@@ -126,24 +126,24 @@ static const BitArray findBestCommonDivisor (const BitArray& p, | |||||
void RSAKey::createKeyPair (RSAKey& publicKey, | void RSAKey::createKeyPair (RSAKey& publicKey, | ||||
RSAKey& privateKey, | RSAKey& privateKey, | ||||
const int numBits) | |||||
const int numBits) throw() | |||||
{ | { | ||||
jassert (numBits > 16); // not much point using less than this.. | jassert (numBits > 16); // not much point using less than this.. | ||||
const BitArray one (1); | |||||
BitArray p (Primes::createProbablePrime (numBits / 2, 30)); | BitArray p (Primes::createProbablePrime (numBits / 2, 30)); | ||||
BitArray q (Primes::createProbablePrime (numBits - numBits / 2, 30)); | BitArray q (Primes::createProbablePrime (numBits - numBits / 2, 30)); | ||||
BitArray n (p); | BitArray n (p); | ||||
n.multiplyBy (q); // n = pq | n.multiplyBy (q); // n = pq | ||||
const BitArray one (1); | |||||
p.subtract (one); | p.subtract (one); | ||||
q.subtract (one); | q.subtract (one); | ||||
BitArray m (p); | BitArray m (p); | ||||
m.multiplyBy (q); // m = (p - 1)(q - 1) | m.multiplyBy (q); // m = (p - 1)(q - 1) | ||||
BitArray e (findBestCommonDivisor (p, q)); | |||||
const BitArray e (findBestCommonDivisor (p, q)); | |||||
BitArray d (e); | BitArray d (e); | ||||
d.inverseModulo (m); | d.inverseModulo (m); | ||||
@@ -155,4 +155,5 @@ void RSAKey::createKeyPair (RSAKey& publicKey, | |||||
privateKey.part2 = n; | privateKey.part2 = n; | ||||
} | } | ||||
END_JUCE_NAMESPACE | END_JUCE_NAMESPACE |
@@ -50,16 +50,16 @@ public: | |||||
Initialise a pair of objects for use with the createKeyPair() method. | Initialise a pair of objects for use with the createKeyPair() method. | ||||
*/ | */ | ||||
RSAKey(); | |||||
RSAKey() throw(); | |||||
/** Loads a key from an encoded string representation. | /** Loads a key from an encoded string representation. | ||||
This reloads a key from a string created by the toString() method. | This reloads a key from a string created by the toString() method. | ||||
*/ | */ | ||||
RSAKey (const String& stringRepresentation); | |||||
RSAKey (const String& stringRepresentation) throw(); | |||||
/** Destructor. */ | /** Destructor. */ | ||||
~RSAKey(); | |||||
~RSAKey() throw(); | |||||
//============================================================================== | //============================================================================== | ||||
/** Turns the key into a string representation. | /** Turns the key into a string representation. | ||||
@@ -76,7 +76,7 @@ public: | |||||
Returns false if the operation failed, e.g. if this object isn't a valid key. | Returns false if the operation failed, e.g. if this object isn't a valid key. | ||||
*/ | */ | ||||
bool applyToValue (BitArray& value) const; | |||||
bool applyToValue (BitArray& value) const throw(); | |||||
//============================================================================== | //============================================================================== | ||||
/** Creates a public/private key-pair. | /** Creates a public/private key-pair. | ||||
@@ -89,7 +89,7 @@ public: | |||||
*/ | */ | ||||
static void createKeyPair (RSAKey& publicKey, | static void createKeyPair (RSAKey& publicKey, | ||||
RSAKey& privateKey, | RSAKey& privateKey, | ||||
const int numBits); | |||||
const int numBits) throw(); | |||||
//============================================================================== | //============================================================================== | ||||
@@ -213,6 +213,10 @@ public: | |||||
*/ | */ | ||||
void stop() throw(); | void stop() throw(); | ||||
/** Returns true if the device has been started successfully. | |||||
*/ | |||||
bool isActive() const throw(); | |||||
/** Returns the ID number of the remote, if it has sent one. | /** Returns the ID number of the remote, if it has sent one. | ||||
*/ | */ | ||||
int getRemoteId() const throw() { return remoteId; } | int getRemoteId() const throw() { return remoteId; } | ||||