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

Function testPublicKey

fdbrpc/JsonWebKeySet.cpp:690–717  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

688}
689
690void testPublicKey(PrivateKey (*factory)()) {
691 // stringify-deserialize public key.
692 // sign some data using private key to see whether deserialized public key can verify it.
693 auto& rng = *deterministicRandom();
694 auto pubKeyName = Standalone<StringRef>("somePublicKey"_sr);
695 auto privKey = factory();
696 auto pubKey = privKey.toPublic();
697 auto jwks = JsonWebKeySet{};
698 jwks.keys.emplace(pubKeyName, pubKey);
699 auto arena = Arena();
700 auto jwksStr = jwks.toStringRef(arena).get();
701 fmt::print("Test JWKS: {}\n", jwksStr.toString());
702 auto jwksClone = JsonWebKeySet::parse(jwksStr, {});
703 ASSERT(jwksClone.present());
704 auto pubKeyClone = jwksClone.get().keys[pubKeyName].getPublic();
705 auto randByteStr = [&rng, &arena](int len) {
706 auto buf = new (arena) uint8_t[len];
707 for (auto i = 0; i < len; i++)
708 buf[i] = rng.randomUInt32() % 255u;
709 return StringRef(buf, len);
710 };
711 auto randData = randByteStr(rng.randomUInt32() % 128 + 16);
712 auto signature = privKey.sign(arena, randData, *::EVP_sha256());
713 ASSERT(pubKeyClone.verify(randData, signature, *::EVP_sha256()));
714 const_cast<uint8_t&>(*randData.begin())++;
715 ASSERT(!pubKeyClone.verify(randData, signature, *::EVP_sha256()));
716 fmt::print("TESTED OK FOR OPENSSL V{} API\n", (OPENSSL_VERSION_NUMBER >> 28));
717}
718
719void testPrivateKey(PrivateKey (*factory)()) {
720 // stringify-deserialize private key.

Callers 1

JsonWebKeySet.cppFile · 0.85

Calls 15

deterministicRandomFunction · 0.85
toPublicMethod · 0.80
getPublicMethod · 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