Browse Source

tags/2021-05-28
jules 18 years ago
parent
commit
e9080b6a17
6 changed files with 65 additions and 35 deletions
  1. +5
    -0
      build/macosx/platform_specific_code/juce_mac_Windowing.cpp
  2. +39
    -19
      src/juce_core/cryptography/juce_Primes.cpp
  3. +2
    -2
      src/juce_core/cryptography/juce_Primes.h
  4. +10
    -9
      src/juce_core/cryptography/juce_RSAKey.cpp
  5. +5
    -5
      src/juce_core/cryptography/juce_RSAKey.h
  6. +4
    -0
      src/juce_core/misc/juce_PlatformUtilities.h

+ 5
- 0
build/macosx/platform_specific_code/juce_mac_Windowing.cpp View File

@@ -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*)
{
if (result == kIOReturnSuccess)


+ 39
- 19
src/juce_core/cryptography/juce_Primes.cpp View File

@@ -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.clearBit (numBits); // to enlarge the array
result.setBit (0);
int index = 1;
int n = 2;
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);
}
index = result.findNextClearBit (index + 1);
n = result.findNextClearBit (n + 1);
}
while (index < numBits);
while (n <= (numBits >> 1));
}
static void bigSieve (const BitArray& base,
int numBits,
const int numBits,
BitArray& result,
const BitArray& smallSieve,
const int smallSieveSize)
const int smallSieveSize) throw()
{
jassert (! base[0]); // must be even!
@@ -105,9 +100,9 @@ static void bigSieve (const BitArray& base,
static bool findCandidate (const BitArray& base,
const BitArray& sieve,
int numBits,
const int numBits,
BitArray& result,
int certainty)
const int certainty) throw()
{
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;
const int smallSieveSize = 15000;
@@ -156,7 +152,7 @@ const BitArray Primes::createProbablePrime (int bitLength, int certainty)
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 two (2);
@@ -168,10 +164,28 @@ static bool passesMillerRabin (const BitArray& n, int iterations)
const int s = d.findNextSetBit (0);
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)
{
BitArray r;
r.createRandomNumber (nMinusOne);
smallPrime = smallPrimes.findNextClearBit (smallPrime + 1);
BitArray r (smallPrime);
//r.createRandomNumber (nMinusOne);
r.exponentModulo (d, n);
if (! (r == one || r == nMinusOne))
@@ -192,7 +206,8 @@ static bool passesMillerRabin (const BitArray& n, int iterations)
return true;
}
bool Primes::isProbablyPrime (const BitArray& number, int certainty)
bool Primes::isProbablyPrime (const BitArray& number,
const int certainty) throw()
{
if (! number[0])
return false;
@@ -209,6 +224,11 @@ bool Primes::isProbablyPrime (const BitArray& number, int certainty)
}
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);
}
}


+ 2
- 2
src/juce_core/cryptography/juce_Primes.h View File

@@ -53,7 +53,7 @@ public:
for primality. A safe value might be anything over about 20-30.
*/
static const BitArray createProbablePrime (int bitLength,
int certainty);
int certainty) throw();
/** Tests a number to see if it's prime.
@@ -64,7 +64,7 @@ public:
safe value might be anything over about 20-30.
*/
static bool isProbablyPrime (const BitArray& number,
int certainty);
int certainty) throw();
};


+ 10
- 9
src/juce_core/cryptography/juce_RSAKey.cpp View File

@@ -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(',')))
{
@@ -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);
}
bool RSAKey::applyToValue (BitArray& value) const
bool RSAKey::applyToValue (BitArray& value) const throw()
{
if (part1.isEmpty() || part2.isEmpty()
|| value.compare (0) <= 0)
@@ -96,7 +96,7 @@ bool RSAKey::applyToValue (BitArray& value) const
}
static const BitArray findBestCommonDivisor (const BitArray& p,
const BitArray& q)
const BitArray& q) throw()
{
const BitArray one (1);
@@ -126,24 +126,24 @@ static const BitArray findBestCommonDivisor (const BitArray& p,
void RSAKey::createKeyPair (RSAKey& publicKey,
RSAKey& privateKey,
const int numBits)
const int numBits) throw()
{
jassert (numBits > 16); // not much point using less than this..
const BitArray one (1);
BitArray p (Primes::createProbablePrime (numBits / 2, 30));
BitArray q (Primes::createProbablePrime (numBits - numBits / 2, 30));
BitArray n (p);
n.multiplyBy (q); // n = pq
const BitArray one (1);
p.subtract (one);
q.subtract (one);
BitArray m (p);
m.multiplyBy (q); // m = (p - 1)(q - 1)
BitArray e (findBestCommonDivisor (p, q));
const BitArray e (findBestCommonDivisor (p, q));
BitArray d (e);
d.inverseModulo (m);
@@ -155,4 +155,5 @@ void RSAKey::createKeyPair (RSAKey& publicKey,
privateKey.part2 = n;
}
END_JUCE_NAMESPACE

+ 5
- 5
src/juce_core/cryptography/juce_RSAKey.h View File

@@ -50,16 +50,16 @@ public:
Initialise a pair of objects for use with the createKeyPair() method.
*/
RSAKey();
RSAKey() throw();
/** Loads a key from an encoded string representation.
This reloads a key from a string created by the toString() method.
*/
RSAKey (const String& stringRepresentation);
RSAKey (const String& stringRepresentation) throw();
/** Destructor. */
~RSAKey();
~RSAKey() throw();
//==============================================================================
/** 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.
*/
bool applyToValue (BitArray& value) const;
bool applyToValue (BitArray& value) const throw();
//==============================================================================
/** Creates a public/private key-pair.
@@ -89,7 +89,7 @@ public:
*/
static void createKeyPair (RSAKey& publicKey,
RSAKey& privateKey,
const int numBits);
const int numBits) throw();
//==============================================================================


+ 4
- 0
src/juce_core/misc/juce_PlatformUtilities.h View File

@@ -213,6 +213,10 @@ public:
*/
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.
*/
int getRemoteId() const throw() { return remoteId; }


Loading…
Cancel
Save