예제 #1
0
        public async Task CreateAsync(IEnumerable <TemporaryExposureKeyModel> keys)
        {
            var current = keys;

            while (current.Any())
            {
                var exportKeyModels = current.Take(MaxKeysPerFile).ToImmutableArray();
                var exportKeys      = exportKeyModels.Select(_ => _.ToKey());
                current = current.Skip(MaxKeysPerFile);

                var exportModel = await TekExportRepository.CreateAsync();

                exportModel.BatchSize      = exportKeyModels.Length;
                exportModel.StartTimestamp = exportKeyModels.Min(_ => _.Timestamp);
                exportModel.EndTimestamp   = exportKeyModels.Max(_ => _.Timestamp);
                exportModel.Region         = Region;

                var bin = new TemporaryExposureKeyExport();
                bin.Keys.AddRange(exportKeys);
                bin.BatchNum       = exportModel.BatchNum;
                bin.BatchSize      = exportModel.BatchSize;
                bin.Region         = exportModel.Region;
                bin.StartTimestamp = exportModel.StartTimestamp;
                bin.EndTimestamp   = exportModel.EndTimestamp;
                bin.SignatureInfos.Add(SigInfo);

                var sig = new TEKSignatureList();

                using var binStream = new MemoryStream();
                bin.WriteTo(binStream);
                await binStream.FlushAsync();

                binStream.Seek(0, SeekOrigin.Begin);
                var signature = CreateSignature(binStream, bin.BatchNum, bin.BatchSize);
                sig.Signatures.Add(signature);
                binStream.Seek(0, SeekOrigin.Begin);

                using (var s = new MemoryStream())
                    using (var z = new System.IO.Compression.ZipArchive(s, System.IO.Compression.ZipArchiveMode.Create, true))
                    {
                        var binEntry = z.CreateEntry(ExportBinFileName);
                        using (var binFile = binEntry.Open())
                        {
                            await binStream.CopyToAsync(binFile);

                            await binFile.FlushAsync();
                        }

                        var sigEntry = z.CreateEntry(ExportSigFileName);
                        using (var sigFile = sigEntry.Open())
                        {
                            sig.WriteTo(sigFile);
                            await sigFile.FlushAsync();
                        }
                        s.Seek(0, SeekOrigin.Begin);
                        await WriteToBlobAsync(s, exportModel, bin, sig);
                    }
                await TekExportRepository.UpdateAsync(exportModel);
            }
        }
예제 #2
0
        public static async Task <MemoryStream> CreateBinAsync(TemporaryExposureKeyExport export)
        {
            var stream = new MemoryStream();

            // Write header
            await stream.WriteAsync(TemporaryExposureKeyExport.Header);

            // Write export proto
            export.WriteTo(stream);

            // Rewind to the front for the consumer
            stream.Position = 0;

            return(stream);
        }
예제 #3
0
        public void EqualsMethodWithSetValue()
        {
            // preparation
            var instance = new TemporaryExposureKeyExport();

            instance.BatchNum     = 99;
            instance.BatchSize    = 99;
            instance.EndTimestamp = ulong.MaxValue;
            instance.Keys.Add(new TemporaryExposureKey());
            instance.Region = "Region";
            instance.SignatureInfos.Add(new SignatureInfo());
            instance.StartTimestamp = ulong.MaxValue;
            // action
            var actual1 = instance.Clone();
            var actual2 = new TemporaryExposureKeyExport(instance);
            var actual3 = new TemporaryExposureKeyExport();
            var actual4 = new TemporaryExposureKeyExport();

            using var memory   = new MemoryStream();
            using var codedOut = new CodedOutputStream(memory, true);
            instance.WriteTo(codedOut);
            codedOut.Flush();
            memory.Position   = 0;
            using var codedIn = new CodedInputStream(memory, true);
            actual3.MergeFrom(codedIn);
            actual4.MergeFrom(actual3);
            // assert
            Assert.AreEqual(instance, actual1);
            Assert.AreEqual(instance, actual2);
            Assert.AreEqual(instance, actual3);
            Assert.AreEqual(instance, actual4);
            Assert.AreEqual(instance.GetHashCode(), actual1.GetHashCode());
            Assert.AreEqual(instance.GetHashCode(), actual2.GetHashCode());
            Assert.AreEqual(instance.GetHashCode(), actual3.GetHashCode());
            Assert.AreEqual(instance.GetHashCode(), actual4.GetHashCode());
            Assert.AreEqual(instance.CalculateSize(), actual1.CalculateSize());
            Assert.AreEqual(instance.CalculateSize(), actual2.CalculateSize());
            Assert.AreEqual(instance.CalculateSize(), actual3.CalculateSize());
            Assert.AreEqual(instance.CalculateSize(), actual4.CalculateSize());
            Assert.AreEqual(instance.ToString(), actual1.ToString());
            Assert.AreEqual(instance.ToString(), actual2.ToString());
            Assert.AreEqual(instance.ToString(), actual3.ToString());
            Assert.AreEqual(instance.ToString(), actual4.ToString());
        }
예제 #4
0
        public async Task CreateAsync(ulong startTimestamp,
                                      ulong endTimestamp,
                                      string region,
                                      int batchNum,
                                      long batchTimestamp,
                                      IEnumerable <TemporaryExposureKeyModel> keys)
        {
            Logger.LogInformation($"start {nameof(CreateAsync)}");
            var current = keys;

            while (current.Any())
            {
                var exportKeyModels = current.Take(MaxKeysPerFile).ToArray();
                var exportKeys      = exportKeyModels.Select(_ => _.ToKey()).ToArray();
                current = current.Skip(MaxKeysPerFile);

                var signatureInfo = SignatureService.Create();
                await SignService.SetSignatureAsync(signatureInfo);

                var exportModel = new TemporaryExposureKeyExportModel();
                exportModel.id           = batchNum.ToString();
                exportModel.PartitionKey = region;
                // TODO: not support apple
                //exportModel.BatchNum = batchNum;
                exportModel.BatchNum = 1;
                exportModel.Region   = region;
                // TODO: not support apple
                //exportModel.BatchSize = exportKeyModels.Length;
                exportModel.BatchSize                  = 1;
                exportModel.StartTimestamp             = startTimestamp;
                exportModel.EndTimestamp               = endTimestamp;
                exportModel.TimestampSecondsSinceEpoch = batchTimestamp;
                exportModel = await TekExportRepository.CreateAsync(exportModel);

                var bin = new TemporaryExposureKeyExport();
                bin.Keys.AddRange(exportKeys);
                bin.BatchNum       = exportModel.BatchNum;
                bin.BatchSize      = exportModel.BatchSize;
                bin.Region         = exportModel.Region;
                bin.StartTimestamp = exportModel.StartTimestamp;
                bin.EndTimestamp   = exportModel.EndTimestamp;
                bin.SignatureInfos.Add(signatureInfo);

                var sig = new TEKSignatureList();

                using var binStream = new MemoryStream();
                await binStream.WriteAsync(FixedHeader, 0, FixedHeaderWidth);

                using var binStreamCoded = new CodedOutputStream(binStream, true);
                bin.WriteTo(binStreamCoded);
                binStreamCoded.Flush();
                await binStream.FlushAsync();

                var signature = await CreateSignatureAsync(binStream, bin.BatchNum, bin.BatchSize);

                signature.SignatureInfo = signatureInfo;
                sig.Signatures.Add(signature);

                using (var s = new MemoryStream())
                {
                    using (var z = new System.IO.Compression.ZipArchive(s, System.IO.Compression.ZipArchiveMode.Create, true))
                    {
                        var binEntry = z.CreateEntry(ExportBinFileName);
                        using (var binFile = binEntry.Open())
                        {
                            binStream.Seek(0, SeekOrigin.Begin);
                            await binStream.CopyToAsync(binFile);

                            await binFile.FlushAsync();
                        }

                        var sigEntry = z.CreateEntry(ExportSigFileName);
                        using (var sigFile = sigEntry.Open())
                            using (var output = new CodedOutputStream(sigFile))
                            {
                                sig.WriteTo(output);
                                output.Flush();
                                await sigFile.FlushAsync();
                            }
                    }
                    s.Seek(0, SeekOrigin.Begin);
                    await BlobService.WriteToBlobAsync(s, exportModel, bin, sig);
                }
                await TekExportRepository.UpdateAsync(exportModel);
            }
        }
        public async Task CreateAsync(ulong startTimestamp, ulong endTimestamp, string region, IEnumerable <TemporaryExposureKeyModel> keys)
        {
            Logger.LogInformation($"start {nameof(CreateAsync)}");
            var current = keys;

            while (current.Any())
            {
                var exportKeyModels = current.Take(MaxKeysPerFile).ToArray();
                var exportKeys      = exportKeyModels.Select(_ => _.ToKey()).ToArray();
                current = current.Skip(MaxKeysPerFile);

                var signatureInfo = SignatureService.Create();
                await SignService.SetSignatureAsync(signatureInfo);

                var exportModel = await TekExportRepository.CreateAsync();

                exportModel.BatchSize      = exportKeyModels.Length;
                exportModel.StartTimestamp = startTimestamp;
                exportModel.EndTimestamp   = endTimestamp;
                exportModel.Region         = region;
                exportModel.SignatureInfos = new SignatureInfo[] { signatureInfo };

                var bin = new TemporaryExposureKeyExport();
                bin.Keys.AddRange(exportKeys);
                bin.BatchNum       = exportModel.BatchNum;
                bin.BatchSize      = exportModel.BatchSize;
                bin.Region         = exportModel.Region;
                bin.StartTimestamp = exportModel.StartTimestamp;
                bin.EndTimestamp   = exportModel.EndTimestamp;
                bin.SignatureInfos.Add(signatureInfo);

                var sig = new TEKSignatureList();

                using var binStream = new MemoryStream();
                bin.WriteTo(binStream);
                await binStream.FlushAsync();

                binStream.Seek(0, SeekOrigin.Begin);
                var signature = await CreateSignatureAsync(binStream, bin.BatchNum, bin.BatchSize);

                signature.SignatureInfo = signatureInfo;
                sig.Signatures.Add(signature);

                using (var s = new MemoryStream())
                {
                    using (var z = new System.IO.Compression.ZipArchive(s, System.IO.Compression.ZipArchiveMode.Create, true))
                    {
                        var binEntry = z.CreateEntry(ExportBinFileName);
                        using (var binFile = binEntry.Open())
                        {
                            binStream.Seek(0, SeekOrigin.Begin);
                            await binStream.CopyToAsync(binFile);

                            await binFile.FlushAsync();
                        }

                        var sigEntry = z.CreateEntry(ExportSigFileName);
                        using (var sigFile = sigEntry.Open())
                        {
                            sig.WriteTo(sigFile);
                            await sigFile.FlushAsync();
                        }
                    }
                    s.Seek(0, SeekOrigin.Begin);
                    await BlobService.WriteToBlobAsync(s, exportModel, bin, sig);
                }
                await TekExportRepository.UpdateAsync(exportModel);
            }
        }