/// <summary> /// Add a signature to a package. /// </summary> public async Task SignAsync(SignPackageRequest request, ILogger logger, CancellationToken token) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } token.ThrowIfCancellationRequested(); if (await _package.IsZip64Async(token)) { throw new SignatureException(NuGetLogCode.NU3006, Strings.ErrorZip64NotSupported); } SigningUtility.Verify(request); var zipArchiveHash = await _package.GetArchiveHashAsync(request.SignatureHashAlgorithm, token); var signatureContent = GenerateSignatureContent(request.SignatureHashAlgorithm, zipArchiveHash); var signature = await _signatureProvider.CreateSignatureAsync(request, signatureContent, logger, token); using (var stream = new MemoryStream(signature.GetBytes())) { await _package.AddSignatureAsync(stream, token); } }
/// <summary> /// Generates a Signature for a given package for tests. /// </summary> /// <param name="signatureProvider">Signature proivider to create the signature.</param> /// <param name="package">Package to be used for the signature.</param> /// <param name="request">SignPackageRequest containing the metadata for the signature request.</param> /// <param name="testLogger">ILogger.</param> /// <returns>Signature for the package.</returns> public static async Task <Signature> CreateSignatureForPackageAsync(ISignatureProvider signatureProvider, PackageArchiveReader package, SignPackageRequest request, TestLogger testLogger) { var zipArchiveHash = await package.GetArchiveHashAsync(request.SignatureHashAlgorithm, CancellationToken.None); var base64ZipArchiveHash = Convert.ToBase64String(zipArchiveHash); var signatureContent = new SignatureContent(SigningSpecifications.V1, request.SignatureHashAlgorithm, base64ZipArchiveHash); return(await signatureProvider.CreateSignatureAsync(request, signatureContent, testLogger, CancellationToken.None)); }