本文整理汇总了C++中GlobalRNG函数的典型用法代码示例。如果您正苦于以下问题:C++ GlobalRNG函数的具体用法?C++ GlobalRNG怎么用?C++ GlobalRNG使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了GlobalRNG函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ProfileSignatureValidate
bool ProfileSignatureValidate(PK_Signer &priv, PK_Verifier &pub, const byte *input,
const size_t inputLength, string description, bool thorough = false)
{
bool pass = true, fail;
fail = !pub.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2) || !priv.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2);
assert(pass && !fail);
SecByteBlock signature(priv.MaxSignatureLength());
std::chrono::steady_clock::time_point signStartTime = std::chrono::steady_clock::now();
size_t signatureLength = priv.SignMessage(GlobalRNG(), input, inputLength, signature);
std::chrono::steady_clock::time_point signEndTime = std::chrono::steady_clock::now();
size_t signNanoSeconds = std::chrono::duration_cast<std::chrono::nanoseconds>(signEndTime - signStartTime).count();
cout << generateCSVString(description, "sign", signNanoSeconds) << endl;
std::chrono::steady_clock::time_point verifyStartTime = std::chrono::steady_clock::now();
fail = !pub.VerifyMessage(input, inputLength, signature, signatureLength);
std::chrono::steady_clock::time_point verifyEndTime = std::chrono::steady_clock::now();
size_t verifyNanoSeconds = std::chrono::duration_cast<std::chrono::nanoseconds>(verifyEndTime - verifyStartTime).count();
cout << generateCSVString(description, "verify", verifyNanoSeconds) << endl;
assert(pass && !fail);
return pass;
}
示例2: SimpleKeyAgreementValidate
bool SimpleKeyAgreementValidate(SimpleKeyAgreementDomain &d)
{
if (d.GetCryptoParameters().Validate(GlobalRNG(), 3))
cout << "passed simple key agreement domain parameters validation" << endl;
else
{
cout << "FAILED simple key agreement domain parameters invalid" << endl;
return false;
}
SecByteBlock priv1(d.PrivateKeyLength()), priv2(d.PrivateKeyLength());
SecByteBlock pub1(d.PublicKeyLength()), pub2(d.PublicKeyLength());
SecByteBlock val1(d.AgreedValueLength()), val2(d.AgreedValueLength());
d.GenerateKeyPair(GlobalRNG(), priv1, pub1);
d.GenerateKeyPair(GlobalRNG(), priv2, pub2);
memset(val1.begin(), 0x10, val1.size());
memset(val2.begin(), 0x11, val2.size());
if (!(d.Agree(val1, priv1, pub2) && d.Agree(val2, priv2, pub1)))
{
cout << "FAILED simple key agreement failed" << endl;
return false;
}
if (!VerifyBufsEqual(val1.begin(), val2.begin(), d.AgreedValueLength()))
{
cout << "FAILED simple agreed values not equal" << endl;
return false;
}
cout << "passed simple key agreement" << endl;
return true;
}
示例3: BenchMarkVerification
void BenchMarkVerification(const char *name, const PK_Signer &priv, PK_Verifier &pub, double timeTotal, bool pc=false)
{
unsigned int len = 16;
AlignedSecByteBlock message(len), signature(pub.SignatureLength());
GlobalRNG().GenerateBlock(message, len);
priv.SignMessage(GlobalRNG(), message, len, signature);
const clock_t start = clock();
unsigned int i;
double timeTaken;
for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
{
// The return value is ignored because we are interested in throughput
bool unused = pub.VerifyMessage(message, len, signature, signature.size());
CRYPTOPP_UNUSED(unused);
}
OutputResultOperations(name, "Verification", pc, i, timeTaken);
if (!pc && pub.GetMaterial().SupportsPrecomputation())
{
pub.AccessMaterial().Precompute(16);
BenchMarkVerification(name, priv, pub, timeTotal, true);
}
}
示例4: TestModeIV
bool TestModeIV(SymmetricCipher &e, SymmetricCipher &d)
{
SecByteBlock lastIV, iv(e.IVSize());
StreamTransformationFilter filter(e, new StreamTransformationFilter(d));
byte plaintext[20480];
for (unsigned int i=1; i<sizeof(plaintext); i*=2)
{
e.GetNextIV(GlobalRNG(), iv);
if (iv == lastIV)
return false;
else
lastIV = iv;
e.Resynchronize(iv);
d.Resynchronize(iv);
unsigned int length = STDMAX(GlobalRNG().GenerateWord32(0, i), (word32)e.MinLastBlockSize());
GlobalRNG().GenerateBlock(plaintext, length);
if (!TestFilter(filter, plaintext, length, plaintext, length))
return false;
}
return true;
}
示例5: CryptoSystemValidate
bool CryptoSystemValidate(PK_Decryptor &priv, PK_Encryptor &pub, bool thorough = false)
{
bool pass = true, fail;
fail = !pub.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2) || !priv.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2);
pass = pass && !fail;
cout << (fail ? "FAILED " : "passed ");
cout << "cryptosystem key validation\n";
static const byte message[] = "test message";
const int messageLen = COUNTOF(message);
SecByteBlock ciphertext(priv.CiphertextLength(messageLen));
SecByteBlock plaintext(priv.MaxPlaintextLength(ciphertext.size()));
pub.Encrypt(GlobalRNG(), message, messageLen, ciphertext);
fail = priv.Decrypt(GlobalRNG(), ciphertext, priv.CiphertextLength(messageLen), plaintext) != DecodingResult(messageLen);
fail = fail || !VerifyBufsEqual(message, plaintext, messageLen);
pass = pass && !fail;
cout << (fail ? "FAILED " : "passed ");
cout << "encryption and decryption\n";
return pass;
}
示例6: RandomizedTransfer
void RandomizedTransfer(BufferedTransformation &source, BufferedTransformation &target, bool finish, const std::string &channel=DEFAULT_CHANNEL)
{
while (source.MaxRetrievable() > (finish ? 0 : 4096))
{
byte buf[4096+64];
size_t start = GlobalRNG().GenerateWord32(0, 63);
size_t len = GlobalRNG().GenerateWord32(1, UnsignedMin(4096U, 3*source.MaxRetrievable()/2));
len = source.Get(buf+start, len);
target.ChannelPut(channel, buf+start, len);
}
}
示例7: SignatureValidate
bool SignatureValidate(PK_Signer &priv, PK_Verifier &pub, bool thorough = false)
{
bool pass = true, fail;
fail = !pub.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2) || !priv.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2);
pass = pass && !fail;
cout << (fail ? "FAILED " : "passed ");
cout << "signature key validation\n";
static const byte message[] = "test message";
const unsigned int messageLen = COUNTOF(message);
SecByteBlock signature(priv.MaxSignatureLength());
size_t signatureLength = priv.SignMessage(GlobalRNG(), message, messageLen, signature);
fail = !pub.VerifyMessage(message, messageLen, signature, signatureLength);
pass = pass && !fail;
cout << (fail ? "FAILED " : "passed ");
cout << "signature and verification\n";
++signature[0];
fail = pub.VerifyMessage(message, messageLen, signature, signatureLength);
pass = pass && !fail;
cout << (fail ? "FAILED " : "passed ");
cout << "checking invalid signature" << endl;
if (priv.MaxRecoverableLength() > 0)
{
signatureLength = priv.SignMessageWithRecovery(GlobalRNG(), message, messageLen, NULL, 0, signature);
SecByteBlock recovered(priv.MaxRecoverableLengthFromSignatureLength(signatureLength));
DecodingResult result = pub.RecoverMessage(recovered, NULL, 0, signature, signatureLength);
fail = !(result.isValidCoding && result.messageLength == messageLen && VerifyBufsEqual(recovered, message, messageLen));
pass = pass && !fail;
cout << (fail ? "FAILED " : "passed ");
cout << "signature and verification with recovery" << endl;
++signature[0];
result = pub.RecoverMessage(recovered, NULL, 0, signature, signatureLength);
fail = result.isValidCoding;
pass = pass && !fail;
cout << (fail ? "FAILED " : "passed ");
cout << "recovery with invalid signature" << endl;
}
return pass;
}
示例8: TestKeyPairValidAndConsistent
void TestKeyPairValidAndConsistent(CryptoMaterial &pub, const CryptoMaterial &priv)
{
if (!pub.Validate(GlobalRNG(), 3))
SignalTestFailure();
if (!priv.Validate(GlobalRNG(), 3))
SignalTestFailure();
/* EqualityComparisonFilter comparison;
pub.Save(ChannelSwitch(comparison, "0"));
pub.AssignFrom(priv);
pub.Save(ChannelSwitch(comparison, "1"));
comparison.ChannelMessageSeriesEnd("0");
comparison.ChannelMessageSeriesEnd("1");
*/
}
示例9: TestKeyPairValidAndConsistent
void TestKeyPairValidAndConsistent(CryptoMaterial &pub, const CryptoMaterial &priv)
{
// "!!" converts between bool <-> integral.
if (!pub.Validate(GlobalRNG(), 2U+!!s_thorough))
SignalTestFailure();
if (!priv.Validate(GlobalRNG(), 2U+!!s_thorough))
SignalTestFailure();
ByteQueue bq1, bq2;
pub.Save(bq1);
pub.AssignFrom(priv);
pub.Save(bq2);
if (bq1 != bq2)
SignalTestFailure();
}
示例10: ValidateECP
bool ValidateECP()
{
std::cout << "\nECP validation suite running...\n\n";
ECIES<ECP>::Decryptor cpriv(GlobalRNG(), ASN1::secp192r1());
ECIES<ECP>::Encryptor cpub(cpriv);
ByteQueue bq;
cpriv.GetKey().DEREncode(bq);
cpub.AccessKey().AccessGroupParameters().SetEncodeAsOID(true);
cpub.GetKey().DEREncode(bq);
ECDSA<ECP, SHA>::Signer spriv(bq);
ECDSA<ECP, SHA>::Verifier spub(bq);
ECDH<ECP>::Domain ecdhc(ASN1::secp192r1());
ECMQV<ECP>::Domain ecmqvc(ASN1::secp192r1());
spriv.AccessKey().Precompute();
ByteQueue queue;
spriv.AccessKey().SavePrecomputation(queue);
spriv.AccessKey().LoadPrecomputation(queue);
bool pass = SignatureValidate(spriv, spub);
cpub.AccessKey().Precompute();
cpriv.AccessKey().Precompute();
pass = CryptoSystemValidate(cpriv, cpub) && pass;
pass = SimpleKeyAgreementValidate(ecdhc) && pass;
pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;
std::cout << "Turning on point compression..." << std::endl;
cpriv.AccessKey().AccessGroupParameters().SetPointCompression(true);
cpub.AccessKey().AccessGroupParameters().SetPointCompression(true);
ecdhc.AccessGroupParameters().SetPointCompression(true);
ecmqvc.AccessGroupParameters().SetPointCompression(true);
pass = CryptoSystemValidate(cpriv, cpub) && pass;
pass = SimpleKeyAgreementValidate(ecdhc) && pass;
pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;
std::cout << "Testing SEC 2, NIST, and Brainpool recommended curves..." << std::endl;
OID oid;
while (!(oid = DL_GroupParameters_EC<ECP>::GetNextRecommendedParametersOID(oid)).m_values.empty())
{
DL_GroupParameters_EC<ECP> params(oid);
bool fail = !params.Validate(GlobalRNG(), 2);
std::cout << (fail ? "FAILED" : "passed") << " " << std::dec << params.GetCurve().GetField().MaxElementBitLength() << " bits" << std::endl;
pass = pass && !fail;
}
return pass;
}
示例11: ValidateEC2N
bool ValidateEC2N()
{
cout << "\nEC2N validation suite running...\n\n";
ECIES<EC2N>::Decryptor cpriv(GlobalRNG(), ASN1::sect193r1());
ECIES<EC2N>::Encryptor cpub(cpriv);
ByteQueue bq;
cpriv.DEREncode(bq);
cpub.AccessKey().AccessGroupParameters().SetEncodeAsOID(true);
cpub.DEREncode(bq);
ECDSA<EC2N, SHA>::Signer spriv(bq);
ECDSA<EC2N, SHA>::Verifier spub(bq);
ECDH<EC2N>::Domain ecdhc(ASN1::sect193r1());
ECMQV<EC2N>::Domain ecmqvc(ASN1::sect193r1());
spriv.AccessKey().Precompute();
ByteQueue queue;
spriv.AccessKey().SavePrecomputation(queue);
spriv.AccessKey().LoadPrecomputation(queue);
bool pass = SignatureValidate(spriv, spub);
pass = CryptoSystemValidate(cpriv, cpub) && pass;
pass = SimpleKeyAgreementValidate(ecdhc) && pass;
pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;
cout << "Turning on point compression..." << endl;
cpriv.AccessKey().AccessGroupParameters().SetPointCompression(true);
cpub.AccessKey().AccessGroupParameters().SetPointCompression(true);
ecdhc.AccessGroupParameters().SetPointCompression(true);
ecmqvc.AccessGroupParameters().SetPointCompression(true);
pass = CryptoSystemValidate(cpriv, cpub) && pass;
pass = SimpleKeyAgreementValidate(ecdhc) && pass;
pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;
#if 0 // TODO: turn this back on when I make EC2N faster for pentanomial basis
cout << "Testing SEC 2 recommended curves..." << endl;
OID oid;
while (!(oid = DL_GroupParameters_EC<EC2N>::GetNextRecommendedParametersOID(oid)).m_values.empty())
{
DL_GroupParameters_EC<EC2N> params(oid);
bool fail = !params.Validate(GlobalRNG(), 2);
cout << (fail ? "FAILED" : "passed") << " " << params.GetCurve().GetField().MaxElementBitLength() << " bits" << endl;
pass = pass && !fail;
}
#endif
return pass;
}
示例12: RSASignFile
void RSASignFile(const char *privFilename, const char *messageFilename, const char *signatureFilename)
{
FileSource privFile(privFilename, true);
RSASSA_PKCS1v15_SHA_Signer priv(privFile);
// RSASSA_PKCS1v15_SHA_Signer ignores the rng. Use a real RNG for other signature schemes!
FileSource f(messageFilename, true, new SignerFilter(GlobalRNG(), priv, new FileSink(signatureFilename)));
}
示例13: BenchMarkDecryption
void BenchMarkDecryption(const char *name, PK_Decryptor &priv, PK_Encryptor &pub, double timeTotal)
{
unsigned int len = 16;
SecByteBlock ciphertext(pub.CiphertextLength(len));
SecByteBlock plaintext(pub.MaxPlaintextLength(ciphertext.size()));
GlobalRNG().GenerateBlock(plaintext, len);
pub.Encrypt(GlobalRNG(), plaintext, len, ciphertext);
const clock_t start = clock();
unsigned int i;
double timeTaken;
for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
priv.Decrypt(GlobalRNG(), ciphertext, ciphertext.size(), plaintext);
OutputResultOperations(name, "Decryption", false, i, timeTaken);
}
示例14: RSADecryptString
string RSADecryptString(const char *privFilename, const char *ciphertext)
{
FileSource privFile(privFilename, true, new HexDecoder);
RSAES_OAEP_SHA_Decryptor priv(privFile);
string result;
StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));
return result;
}
示例15: BenchMarkAgreement
void BenchMarkAgreement(const char *name, SimpleKeyAgreementDomain &d, double timeTotal, bool pc=false)
{
SecByteBlock priv1(d.PrivateKeyLength()), priv2(d.PrivateKeyLength());
SecByteBlock pub1(d.PublicKeyLength()), pub2(d.PublicKeyLength());
d.GenerateKeyPair(GlobalRNG(), priv1, pub1);
d.GenerateKeyPair(GlobalRNG(), priv2, pub2);
SecByteBlock val(d.AgreedValueLength());
const clock_t start = clock();
unsigned int i;
double timeTaken;
for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i+=2)
{
d.Agree(val, priv1, pub2);
d.Agree(val, priv2, pub1);
}
OutputResultOperations(name, "Key Agreement", pc, i, timeTaken);
}