예제 #1
0
    public static void Main(String[] args)
    {
        TokenSigner tokenSigner = new TokenSigner();
        if (args == null || args.Length == 0)
        {
            tokenSigner.ListCertificates();

            Console.WriteLine("Help.");
            Console.WriteLine("First argument is the folder path.");
            Console.WriteLine("Second argument is the token pin.");


        }
        else
        {
            if (args.Length == 2)
                tokenSigner.TokenPin = args[1];

            if (File.Exists(args[0] + @"\SourceDocumentJson.json") == false)
            {
                Console.WriteLine("The file " + args[0] + @"\SourceDocumentJson.json is not exist");
            }
            else
            {
                String cades = "";
                String SourceDocumentJson = File.ReadAllText(args[0] + @"\SourceDocumentJson.json");
                JObject request = JsonConvert.DeserializeObject<JObject>(SourceDocumentJson, new JsonSerializerSettings()
                {
                    FloatFormatHandling = FloatFormatHandling.String,
                    FloatParseHandling = FloatParseHandling.Decimal,
                    DateFormatHandling = DateFormatHandling.IsoDateFormat,
                    DateParseHandling = DateParseHandling.None
                });

                //Start serialize
                String canonicalString = tokenSigner.Serialize(request);
                File.WriteAllBytes(args[0] + @"\CanonicalString.txt", System.Text.Encoding.UTF8.GetBytes(canonicalString));
                // retrieve cades
                if (request["documentTypeVersion"].Value<string>() == "0.9")
                {
                    cades = "ANY";
                }
                else
                {
                    cades = tokenSigner.SignWithCMS(canonicalString);
                }
                File.WriteAllBytes(args[0] + @"\Cades.txt", System.Text.Encoding.UTF8.GetBytes(cades));
                JObject signaturesObject = new JObject(
                                       new JProperty("signatureType", "I"),
                                       new JProperty("value", cades));
                JArray signaturesArray = new JArray();
                signaturesArray.Add(signaturesObject);
                request.Add("signatures", signaturesArray);
                String fullSignedDocument = "{\"documents\":[" + request.ToString() + "]}";
                File.WriteAllBytes(args[0] + @"\FullSignedDocument.json", System.Text.Encoding.UTF8.GetBytes(fullSignedDocument));
            }
        }
    }