MCPcopy Create free account
hub / github.com/apple/foundationdb / testPrivateKey

Function testPrivateKey

fdbrpc/JsonWebKeySet.cpp:719–746  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

717}
718
719void testPrivateKey(PrivateKey (*factory)()) {
720 // stringify-deserialize private key.
721 // sign some data using deserialized private key to see whether public key can verify it.
722 auto& rng = *deterministicRandom();
723 auto privKeyName = Standalone<StringRef>("somePrivateKey"_sr);
724 auto privKey = factory();
725 auto pubKey = privKey.toPublic();
726 auto jwks = JsonWebKeySet{};
727 jwks.keys.emplace(privKeyName, privKey);
728 auto arena = Arena();
729 auto jwksStr = jwks.toStringRef(arena).get();
730 fmt::print("Test JWKS: {}\n", jwksStr.toString());
731 auto jwksClone = JsonWebKeySet::parse(jwksStr, {});
732 ASSERT(jwksClone.present());
733 auto privKeyClone = jwksClone.get().keys[privKeyName].getPrivate();
734 auto randByteStr = [&rng, &arena](int len) {
735 auto buf = new (arena) uint8_t[len];
736 for (auto i = 0; i < len; i++)
737 buf[i] = rng.randomUInt32() % 255u;
738 return StringRef(buf, len);
739 };
740 auto randData = randByteStr(rng.randomUInt32() % 128 + 16);
741 auto signature = privKeyClone.sign(arena, randData, *::EVP_sha256());
742 ASSERT(pubKey.verify(randData, signature, *::EVP_sha256()));
743 const_cast<uint8_t&>(*randData.begin())++;
744 ASSERT(!pubKey.verify(randData, signature, *::EVP_sha256()));
745 fmt::print("TESTED OK FOR OPENSSL V{} API\n", (OPENSSL_VERSION_NUMBER >> 28));
746}
747
748} // anonymous namespace
749

Callers 1

JsonWebKeySet.cppFile · 0.85

Calls 15

deterministicRandomFunction · 0.85
toPublicMethod · 0.80
getPrivateMethod · 0.80
randomUInt32Method · 0.80
signMethod · 0.80
getMethod · 0.65
ArenaClass · 0.50
printFunction · 0.50
parseFunction · 0.50
StringRefClass · 0.50
toStringRefMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected