private static byte[] GeneratePreallocatedEntropy(PayloadLayoutScheme schemeEnum, int items, int min, int max) { var entropyStream = new MemoryStream(); bool[] itemCompletionRegister = new bool[items]; int completedItems = 0; while (completedItems < items) { // Stream selection int stream; do { stream = StratCom.EntropySupplier.Next(0, items); } while (itemCompletionRegister[stream]); entropyStream.WriteUInt32((uint)stream); if (schemeEnum == PayloadLayoutScheme.Frameshift) { // Item header padding length selection int headerPaddingLength = StratCom.EntropySupplier.Next(min, max + 1); entropyStream.WriteUInt32((uint)headerPaddingLength); // Item trailer padding length selection int trailerPaddingLength = StratCom.EntropySupplier.Next(min, max + 1); entropyStream.WriteUInt32((uint)trailerPaddingLength); #if INCLUDE_FABRIC } else if (schemeEnum == PayloadLayoutScheme.Fabric) { // Item header padding length selection int stripeLength = StratCom.EntropySupplier.Next(min, max + 1); entropyStream.WriteUInt32((uint)stripeLength); #endif } itemCompletionRegister[stream] = true; completedItems++; } return(entropyStream.ToArray()); }
/// <summary> /// Instantiates and returns a payload I/O module implementing the mode of operation that the /// instance this method was called from describes. /// </summary> /// <param name="schemeEnum">Payload layout scheme to choose the correspknding multiplexer.</param> /// <param name="writing">Whether the multiplexer will be multiplexing or demultiplexing.</param> /// <param name="multiplexedStream">Stream to multiplex/demultiplex to/from.</param> /// <param name="payloadItems"></param> /// <param name="itemPreKeys"></param> /// <param name="config">Configuration of the layout module/multiplexer.</param> /// <returns> /// An module object deriving from PayloadMultiplexer. /// </returns> public static PayloadMux CreatePayloadMultiplexer(PayloadLayoutScheme schemeEnum, bool writing, Stream multiplexedStream, List <PayloadItem> payloadItems, IReadOnlyDictionary <Guid, byte[]> itemPreKeys, PayloadConfiguration config) { switch (schemeEnum) { case PayloadLayoutScheme.Simple: return(new SimplePayloadMux(writing, multiplexedStream, payloadItems, itemPreKeys, config)); case PayloadLayoutScheme.Frameshift: return(new FrameshiftPayloadMux(writing, multiplexedStream, payloadItems, itemPreKeys, config)); #if INCLUDE_FABRIC case PayloadLayoutScheme.Fabric: return(new FabricPayloadMux(writing, multiplexedStream, payloadItems, itemPreKeys, config)); #endif default: throw new ArgumentException("Scheme unsupported.", "schemeEnum"); } }
/// <summary> /// Initialises the configuration for the specified module type with default settings. /// If fine-tuning is desired, use the specialised constructors. /// </summary> /// <param name="schemeEnum">Desired payload layout scheme.</param> /// <seealso cref="CreateSimple" /> /// <seealso cref="CreateFrameshiftVariable" /> public static PayloadConfiguration CreateDefault(PayloadLayoutScheme schemeEnum) { const CsPseudorandomNumberGenerator defaultCsprng = CsPseudorandomNumberGenerator.Rabbit; // Fast initialisation! switch (schemeEnum) { case PayloadLayoutScheme.Simple: return(CreateSimple(defaultCsprng)); case PayloadLayoutScheme.Frameshift: // Padding length is variable by default. return(CreateFrameshiftVariable(defaultCsprng, FrameshiftPayloadMux.MinimumPaddingLength, FrameshiftPayloadMux.MaximumPaddingLength)); #if INCLUDE_FABRIC case PayloadLayoutScheme.Fabric: // Stripe length is variable by default. return(CreateFabricVariable(defaultCsprng, FabricPayloadMux.MinimumStripeLength, FabricPayloadMux.MaximumStripeLength)); #endif } throw new NotSupportedException(); }
private static void SymmetricPackageTest(string testName, List <FileInfo> data, PayloadLayoutScheme scheme, bool outputToFile = false, bool precomputed = false) { // Process of writing destroys preKey variable passed in for security // We must copy it to a local variable before reading the package back var preKey = KeyProviders.Alice.SymmetricKeys.ElementAt( StratCom.EntropySupplier.Next(KeyProviders.Alice.SymmetricKeys.Count())); var totalLen = data.Sum(file => file.Length); int expLen = (int)(totalLen * 1.1); TimeSpan enc, dec; using (var ms = new MemoryStream(expLen)) { var sw = Stopwatch.StartNew(); var packageWriter = new PackageWriter(preKey, lowEntropy: false, layoutScheme: scheme); // low entropy = false foreach (var file in data) { packageWriter.AddFile(file.FullName); } if (precomputed) { if (scheme == PayloadLayoutScheme.Simple) { packageWriter.SetPayloadConfiguration(PayloadLayoutConfigurationFactory.CreateSimplePreallocated(data.Count)); } else if (scheme == PayloadLayoutScheme.Frameshift) { packageWriter.SetPayloadConfiguration( PayloadLayoutConfigurationFactory.CreateFrameshiftPrecomputedVariable(data.Count)); } else { throw new InvalidOperationException(); } } packageWriter.Write(ms, false); sw.Stop(); enc = sw.Elapsed; sw.Reset(); ms.Seek(0, SeekOrigin.Begin); if (outputToFile) { using (var fs = new FileStream( IOTestBase.PackageDestinationDirectory.FullName + Path.DirectorySeparatorChar + testName + IOTestBase.PackageExtension, FileMode.Create)) { ms.CopyTo(fs); } ms.Seek(0, SeekOrigin.Begin); } sw.Start(); // Now read it back var readingPackage = PackageReader.FromStream(ms, KeyProviders.Alice); readingPackage.ReadToDirectory(IOTestBase.PackageDestinationDirectory.FullName, true); sw.Stop(); dec = sw.Elapsed; } var megabytes = (double)totalLen / 1024 / 1024; Assert.Pass("{0} ms / {1:N2} MB/s -> {2} ms / {3:N2} MB/s.", enc.Milliseconds, (1000.0 / (double)enc.Milliseconds) * megabytes, dec.Milliseconds, (1000.0 / (double)dec.Milliseconds) * megabytes); }