/// <summary> /// Adds a signature to a package if it is not already signed. /// </summary> /// <param name="signatureStream">Stream of the signature SignedCms object to be added to the package.</param> /// <param name="token">Cancellation Token.</param> /// <returns></returns> public async Task AddSignatureAsync(Stream signatureStream, CancellationToken token) { token.ThrowIfCancellationRequested(); ThrowIfZipReadStreamIsNull(); if (_zipWriteStream == null) { throw new SignatureException(Strings.SignedPackageUnableToAccessSignature); } if (await IsSignedAsync(token)) { throw new SignatureException(NuGetLogCode.NU3001, Strings.SignedPackageAlreadySigned); } using (var reader = new BinaryReader(ZipReadStream, new UTF8Encoding(), leaveOpen: true)) using (var writer = new BinaryWriter( _zipWriteStream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true), leaveOpen: true)) { SignedPackageArchiveUtility.SignZip((MemoryStream)signatureStream, reader, writer); } }
/// <summary> /// Adds a signature to a package if it is not already signed. /// </summary> /// <param name="signatureStream">Stream of the signature SignedCms object to be added to the package.</param> /// <param name="token">Cancellation Token.</param> /// <returns></returns> public async Task AddSignatureAsync(Stream signatureStream, CancellationToken token) { token.ThrowIfCancellationRequested(); if (ZipReadStream == null) { throw new SignatureException(Strings.SignedPackageUnableToAccessSignature); } if (ZipWriteStream == null) { throw new SignatureException(Strings.SignedPackageUnableToAccessSignature); } if (await IsSignedAsync(token)) { throw new SignatureException(NuGetLogCode.NU3001, Strings.SignedPackagePackageAlreadySigned); } using (var reader = new BinaryReader(ZipReadStream, SigningSpecifications.Encoding, leaveOpen: true)) using (var writer = new BinaryWriter(ZipWriteStream, SigningSpecifications.Encoding, leaveOpen: true)) { SignedPackageArchiveUtility.SignZip((MemoryStream)signatureStream, reader, writer); } }