Stream Sign(Certificate certificate, IList <Certificate> chain, DigestAlgorithm digestAlgo, Stream content, bool detach) { var signerInfo = new CmsSignerInfo(); var signers = new CmsSignerInfo[1]; byte[] signedData; IBuffer buffer; signerInfo.HashAlgorithmName = GetHashAlgorithmName(digestAlgo); signerInfo.Certificate = certificate; signers[0] = signerInfo; if (detach) { using (var input = content.AsInputStream()) buffer = CmsDetachedSignature.GenerateSignatureAsync(input, signers, chain).GetResults(); } else { buffer = CryptographicBuffer.CreateFromByteArray(ReadAllBytes(content)); buffer = CmsAttachedSignature.GenerateSignatureAsync(buffer, signers, chain).GetResults(); } CryptographicBuffer.CopyToByteArray(buffer, out signedData); return(new MemoryStream(signedData, false)); }
/// <summary> /// This is the click handler for the 'RunSample' button. It is responsible for executing the sample code. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void RunSample_Click(object sender, RoutedEventArgs e) { Certificate selectedCertificate = null; string verifyselection = VerifyCert.SelectionBoxItem.ToString(); //get the selected certificate if (CertificateList.SelectedIndex >= 0 && CertificateList.SelectedIndex < certList.Count) { selectedCertificate = certList[CertificateList.SelectedIndex]; } if (selectedCertificate == null) { ViewCertText.Text = "Please select a certificate first."; return; } // a certificate was selected, do the desired operation if (verifyselection.Equals("Verify Certificate")) { //Build the chain var chain = await selectedCertificate.BuildChainAsync(null, null); //Validate the chain var result = chain.Validate(); verifytext = "\n Verification Result :" + result.ToString(); } else if (verifyselection.Equals("Sign/Verify using certificate key")) { // get private key CryptographicKey keyPair = await PersistedKeyProvider.OpenKeyPairFromCertificateAsync(selectedCertificate, HashAlgorithmNames.Sha1, CryptographicPadding.RsaPkcs1V15); String cookie = "Some Data to sign"; IBuffer Data = CryptographicBuffer.ConvertStringToBinary(cookie, BinaryStringEncoding.Utf16BE); try { //sign the data by using the key IBuffer Signed = CryptographicEngine.Sign(keyPair, Data); bool bresult = CryptographicEngine.VerifySignature(keyPair, Data, Signed); if (bresult == true) { verifytext = "\n Verification Result : Successfully signed and verified signature"; } else { verifytext = "\n Verification Result : Verify Signature Failed"; } } catch (Exception exp) { verifytext = "\n Verification Failed. Exception Occurred :" + exp.Message; } } else if (verifyselection.Equals("Sign/Verify using CMS based format")) { IInputStream pdfInputstream; InMemoryRandomAccessStream originalData = new InMemoryRandomAccessStream(); //Populate the new memory stream pdfInputstream = originalData.GetInputStreamAt(0); CmsSignerInfo signer = new CmsSignerInfo(); signer.Certificate = selectedCertificate; signer.HashAlgorithmName = HashAlgorithmNames.Sha1; IList <CmsSignerInfo> signers = new List <CmsSignerInfo>(); signers.Add(signer); try { IBuffer signature = await CmsDetachedSignature.GenerateSignatureAsync(pdfInputstream, signers, null); CmsDetachedSignature cmsSignedData = new CmsDetachedSignature(signature); pdfInputstream = originalData.GetInputStreamAt(0); SignatureValidationResult validationResult = await cmsSignedData.VerifySignatureAsync(pdfInputstream); if (SignatureValidationResult.Success == validationResult) { verifytext = "\n Verification Result : Successfully signed and verified Signature"; } else { verifytext = "\n Verification Result : Verify Signature using CMS based format Failed"; } } catch (Exception exp) { verifytext = "\n Verification Failed. Exception Occurred :" + exp.Message; } } else if (verifyselection.Equals("Get certificate and show details")) { DisplayCertificate(selectedCertificate); } ViewCertText.Text += verifytext; verifytext = string.Empty; }