Esempio n. 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);
            }
        }
Esempio n. 2
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);
            }
        }