static void Main(string[] args) { var data = new Data(); data.wireDecode(new Blob(TlvData)); // Use a hard-wired secret for testing. In a real application the signer // ensures that the verifier knows the shared key and its keyName. var key = new Blob(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 }); if (KeyChain.verifyDataWithHmacWithSha256(data, key)) Console.Out.WriteLine("Hard-coded data signature verification: VERIFIED"); else Console.Out.WriteLine("Hard-coded data signature verification: FAILED"); var freshData = new Data(new Name("/ndn/abc")); var signature = new HmacWithSha256Signature(); signature.getKeyLocator().setType(KeyLocatorType.KEYNAME); signature.getKeyLocator().setKeyName(new Name("key1")); freshData.setSignature(signature); freshData.setContent(new Blob("SUCCESS!")); Console.Out.WriteLine("Signing fresh data packet " + freshData.getName().toUri()); KeyChain.signWithHmacWithSha256(freshData, key); if (KeyChain.verifyDataWithHmacWithSha256(freshData, key)) Console.Out.WriteLine("Freshly-signed data signature verification: VERIFIED"); else Console.Out.WriteLine("Freshly-signed data signature verification: FAILED"); }
/** * Loop to encode a data packet nIterations times. * @param nIterations The number of iterations. * @param useComplex If true, use a large name, large content and all fields. * If false, use a small name, small content * and only required fields. * @param useCrypto If true, sign the data packet. If false, use a blank * signature. * @param keyType KeyType.RSA or EC, used if useCrypto is true. * @param encoding Set encoding[0] to the wire encoding. * @return The number of seconds for all iterations. */ private static double benchmarkEncodeDataSeconds(int nIterations, bool useComplex, bool useCrypto, KeyType keyType, Blob[] encoding) { Name name; Blob content; if (useComplex) { // Use a large name and content. name = new Name ("/ndn/ucla.edu/apps/lwndn-test/numbers.txt/%FD%05%05%E8%0C%CE%1D/%00"); StringBuilder contentStream = new StringBuilder(); int count = 1; contentStream.append(count++); while (contentStream.toString().Length < 1115) contentStream.append(" ").append(count++); content = new Blob(contentStream.toString()); } else { // Use a small name and content. name = new Name("/test"); content = new Blob("abc"); } Name.Component finalBlockId = new Name.Component(new Blob(new byte[] { (byte)0 })); // Initialize the KeyChain storage in case useCrypto is true. MemoryIdentityStorage identityStorage = new MemoryIdentityStorage(); MemoryPrivateKeyStorage privateKeyStorage = new MemoryPrivateKeyStorage(); KeyChain keyChain = new KeyChain (new IdentityManager(identityStorage, privateKeyStorage), new SelfVerifyPolicyManager(identityStorage)); Name keyName = new Name("/testname/DSK-123"); Name certificateName = keyName.getSubName(0, keyName.size() - 1).append ("KEY").append(keyName.get(-1)).append("ID-CERT").append("0"); privateKeyStorage.setKeyPairForKeyName (keyName, KeyType.RSA, new ByteBuffer(DEFAULT_RSA_PUBLIC_KEY_DER), new ByteBuffer(DEFAULT_RSA_PRIVATE_KEY_DER)); Blob signatureBits = new Blob(new byte[256]); Blob emptyBlob = new Blob(new byte[0]); double start = getNowSeconds(); for (int i = 0; i < nIterations; ++i) { Data data = new Data(name); data.setContent(content); if (useComplex) { data.getMetaInfo().setFreshnessPeriod(30000); data.getMetaInfo().setFinalBlockId(finalBlockId); } if (useCrypto) // This sets the signature fields. keyChain.sign(data, certificateName); else { // Imitate IdentityManager.signByCertificate to set up the signature // fields, but don't sign. KeyLocator keyLocator = new KeyLocator(); keyLocator.setType(KeyLocatorType.KEYNAME); keyLocator.setKeyName(certificateName); Sha256WithRsaSignature sha256Signature = (Sha256WithRsaSignature)data.getSignature(); sha256Signature.setKeyLocator(keyLocator); sha256Signature.setSignature(signatureBits); } encoding[0] = data.wireEncode(); } double finish = getNowSeconds(); return finish - start; }
/** * Call benchmarkEncodeDataSeconds and benchmarkDecodeDataSeconds with * appropriate nInterations. Print the results to System.out. * @param useComplex See benchmarkEncodeDataSeconds. * @param useCrypto See benchmarkEncodeDataSeconds and * benchmarkDecodeDataSeconds. * @param keyType KeyType.RSA or EC, used if useCrypto is true. */ private static void benchmarkEncodeDecodeData(bool useComplex, bool useCrypto, KeyType keyType) { String format = "TLV"; Blob[] encoding = new Blob[1]; { int nIterations = useCrypto ? 30 : 200000; double duration = benchmarkEncodeDataSeconds (nIterations, useComplex, useCrypto, keyType, encoding); Console.Out.WriteLine("Encode " + (useComplex ? "complex " : "simple ") + format + " data: Crypto? " + (useCrypto ? (keyType == KeyType.ECDSA ? "EC " : "RSA") : "no ") + ", Duration sec, Hz: " + duration + ", " + (nIterations / duration)); } { // Use an extra long duration for decoding until we understand why it gets // a different rate at a shorter duration. int nIterations = useCrypto ? 1000 : 200000; double duration = benchmarkDecodeDataSeconds (nIterations, useCrypto, keyType, encoding[0]); Console.Out.WriteLine("Decode " + (useComplex ? "complex " : "simple ") + format + " data: Crypto? " + (useCrypto ? (keyType == KeyType.ECDSA ? "EC " : "RSA") : "no ") + ", Duration sec, Hz: " + duration + ", " + (nIterations / duration)); } }
/** * Loop to decode a data packet nIterations times. * @param nIterations The number of iterations. * @param useCrypto If true, verify the signature. If false, don't verify. * @param keyType KeyType.RSA or EC, used if useCrypto is true. * @param encoding The wire encoding to decode. * @return The number of seconds for all iterations. * @throws EncodingException */ private static double benchmarkDecodeDataSeconds(int nIterations, bool useCrypto, KeyType keyType, Blob encoding) { // Initialize the KeyChain storage in case useCrypto is true. MemoryIdentityStorage identityStorage = new MemoryIdentityStorage(); KeyChain keyChain = new KeyChain (new IdentityManager(identityStorage, new MemoryPrivateKeyStorage()), new SelfVerifyPolicyManager(identityStorage)); Name keyName = new Name("/testname/DSK-123"); Name certificateName = keyName.getSubName(0, keyName.size() - 1).append ("KEY").append(keyName.get(-1)).append("ID-CERT").append("0"); identityStorage.addKey(keyName, KeyType.RSA, new Blob(DEFAULT_RSA_PUBLIC_KEY_DER)); VerifyCallbacks callbacks = new VerifyCallbacks(); double start = getNowSeconds(); for (int i = 0; i < nIterations; ++i) { Data data = new Data(); data.wireDecode(encoding.buf()); if (useCrypto) keyChain.verifyData(data, callbacks, callbacks); } double finish = getNowSeconds(); return finish - start; }