Search code examples
c#jsondotnetzipwallet

.pkpass create fail because of manifest pass.json string format?


This is a very strange question.

I using C# to create a pass.json and save it to memoryStream, it work normally. After that I create the manifest.json SHA1 data which including that pass.json, the string of manifest.json like this and it is totally correct.

{"icon.png":     "9423bd00e2b01c59a3265c38b5062fac7da0752d",
 "[email protected]": "4d1db55bdaca70b685c013529a1c0dcbd7046524",
 "logo.png":     "ee5b053e63dbfe3b78378c15d163331d68a0ede8",
 "[email protected]": "2f9e3a55bded1163620719a4d6c1ad496ed40c17",
 "pass.json":    "fd68bf77757d3057263a9aca0e5110ddd933934a"}

After generate pkpass as my phone, it can't open. I change the pass.json SHA1 code as "fd68bf77757d3057263a9aca0e5110ddd933934a" without using a value to save it, it work.

The coding like following:

// This version run success
var strPass = JavascriptSerialize(details);
var sw = new StreamWriter(assetsFolder + @"pass.json");
sw.Write(strPass);
sw.Close();
manifest.passjson = GetSha1Hash(assetsFolder + manifest.GetAssetBoardingPass(libPkPass_object_boardingPass.JsonObjects.AssetTypes.passjson));

//manifest.passjson = "2f9e3a55bded1163620719a4d6c1ad496ed40c17"
// end

// This version run fail
var strPass = JavascriptSerialize(details);
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(strPass);
writer.Write(s);
writer.Flush();
stream.Position = 0;
var a = GetSha1HashMemory(passStream);
    private static string GetSha1HashMemory(Stream passStream)
    {

        //var bs = new BufferedStream(passStream);
        using (SHA1Managed sha = new SHA1Managed())
        {
            byte[] checksum = sha.ComputeHash(passStream);
            string sendCheckSum = BitConverter.ToString(checksum)
                .Replace("-", string.Empty);
            return sendCheckSum.ToString().ToLower();
        }
    }
manifest.passjson = a;

//manifest.passjson = "2f9e3a55bded1163620719a4d6c1ad496ed40c17" (same data )
//end

What is going on?????? I can find out any question that string is wrong. The pkpass provide in here (sendspace).

Can any body told me where is wrong?

Big Thank!


Solution

  • Two mistakes :
    ComputeHash(Stream) and using Stream

    ComputeHash(Stream) : ComputeHash stream only using System.IO.Stream, but not MemoryStream, change to ComputeHash(bytes[]) can handle it

    using Stream: I try to pass the stream to other function, it is not a good example, the stream need to create a new one and it may replace some bytes at your computer stream. In this case, I just need to call this function will out open new one, it will fix

                    StringBuilder formatted;
                    using (var sha1 = new SHA1Managed())
                    {
                        //var bytePass = ReadFully(passStream);
                        var bytePass = passStream.ToArray();
                        var hash = sha1.ComputeHash(bytePass);
                        formatted = new StringBuilder(2 * hash.Length);
                        foreach (var b in hash)
                        {
                            formatted.AppendFormat("{0:X2}", b);
                        }
                    }
                    manifest.passjson = formatted.ToString().ToLower();