sign-snk.xsh

Sign XSharper script with a SNK file

<xsharper>
    <versionInfo title="Sign-SNK" value="Sign XSH script with a provided SNK key file" version="0.0.0.1" />
    <usage options="ifNoArguments default" />
    <param name="key" value="SNK key" required="true" />
    <param name="data" value="Script file" required="true" />
    <reference name="System.Security" />
    <?h using System;
        using System.Text;
        using System.Security.Cryptography;
        using System.Security.Cryptography.Xml;
    ?>
    <?_ RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.ImportCspBlob(System.IO.File.ReadAllBytes((string)c["key"]));

        string filename=(string)c["data"];
        XmlDocument doc = new XmlDocument();
        doc.PreserveWhitespace = true;
        doc.Load(filename);
        c.Info.WriteLine("Loaded "+filename);
                
        SignedXml signer=  new SignedXml(doc); 
            
        var el=doc.GetElementsByTagName("Signature");
        if (el!=null && el.Count!=0 && el[0]!=null && el[0] is XmlElement)
            el[0].ParentNode.RemoveChild(el[0]);

        
        signer.KeyInfo = new KeyInfo();
        signer.KeyInfo.AddClause(new RSAKeyValue(rsa));
        signer.SigningKey = rsa; 
        signer.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NWithCommentsTransformUrl;

        XmlDsigExcC14NWithCommentsTransform canMethod = (XmlDsigExcC14NWithCommentsTransform)signer.SignedInfo.CanonicalizationMethodObject;
        canMethod.InclusiveNamespacesPrefixList = "Sign";


        Reference orderRef = new Reference("");
        orderRef.AddTransform(new XmlDsigEnvelopedSignatureTransform());
        signer.AddReference(orderRef); 
        
        signer.ComputeSignature();
        doc.DocumentElement.AppendChild(signer.GetXml());
        doc.Save(filename); 
        c.Print("Script '${=Path.GetFullPath($data)}' has been signed & saved successfully");
        return 0;
    

        
    ?>
</xsharper>