Exemple #1
0
 public bool IsValid(string rootFolder)
 {
     if (Exists(rootFolder))
     {
         int size = (int)new FileInfo(Path.Combine(rootFolder, fileName)).Length;
         if (size == fileSize)
         {
             byte[] buffer = File.ReadAllBytes(Path.Combine(rootFolder, fileName));
             byte[] hash   = XCalcSig.CalculateDigest(buffer, (int)fileHashOffset, (int)fileHashLength);
             buffer = null;
             bool result = hash.CompareTo(fileHash);
             hash = null;
             return(result);
         }
     }
     return(false);
 }
Exemple #2
0
            public bool Sign(byte[] XboxHDKey, bool skipContentHashing, out string errorMessage)
            {
                if (Equals(null, XboxHDKey) || XboxHDKey.Length != 0x10)
                {
                    errorMessage = "Bad key";
                    return(false);
                }

                byte[] buffer, hash;

                //calculate content hashes
                if (!skipContentHashing)
                {
                    object result;
                    if (!GetSection(Types.SectionType.TableOfContents, out result))
                    {
                        errorMessage = "Failed to parse section data";
                        return(false);
                    }

                    Types.Section         section   = Sections[1];
                    Types.TableOfContents fileTable = (Types.TableOfContents)result; //parsed toc data
                    foreach (Types.TableEntry file in fileTable.entries)
                    {
                        if (!file.Exists(Path.GetDirectoryName(FilePath)))
                        {
                            errorMessage = string.Format("Content file not found: {0}", file.fileName);
                            return(false);
                        }

                        uint hashStartOffset = file.fileHashOffset;
                        uint hashLength      = file.fileHashLength;
                        uint size            = (uint)new FileInfo(Path.Combine(Path.GetDirectoryName(FilePath), file.fileName)).Length;
                        if (size != file.fileSize)
                        {
                            //errorMessage = string.Format("The size of the file \"{0}\" is invalid\n\nExpected size: {1} ({2} bytes)\nActual size: {3} ({4} bytes)", file.fileName, file.fileSize.RoundBytes(), file.fileSize, size.RoundBytes(), size);
                            //return false;

                            //instead of returning an error here I decided to correct the file size, in case of modders :)
                            buffer      = BitConverter.GetBytes(size);
                            IO.Position = (((section.sectionOffset + fileTable.entryTableOffset) + file.entryOffset) + 0x8);
                            IO.Write(buffer, 0, 4); //file size

                            //set hash to cover entire file
                            IO.Write(new byte[4], 0, 4); //hash start offset
                            IO.Write(buffer, 0, 4);      //hash length

                            hashStartOffset = 0;
                            hashLength      = size;
                        }

                        buffer = File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(FilePath), file.fileName));
                        hash   = XCalcSig.CalculateDigest(buffer, (int)hashStartOffset, (int)hashLength);

                        IO.Position = ((section.sectionOffset + 0xC) + (file.fileHashIndex * 0x14));
                        IO.Write(hash, 0, 0x14);
                    }
                    IO.Flush();
                }

                //calculate section hashes
                foreach (Types.Section section in Sections)
                {
                    if (!section.IsAllocated)
                    {
                        continue;
                    }
                    buffer      = new byte[section.sectionLength];
                    IO.Position = section.sectionOffset;
                    IO.Read(buffer, 0, (int)section.sectionLength);
                    hash        = XCalcSig.CalculateDigest(buffer);
                    IO.Position = section.hashOffset;
                    IO.Write(hash, 0, 0x14);
                }
                IO.Flush();

                //calculate header "signature"
                buffer      = new byte[(Header.headerSize - 0x14)];
                IO.Position = 0x14;
                IO.Read(buffer, 0, (int)(Header.headerSize - 0x14));
                hash        = XCalcSig.CalculateNonRoamable(BitConverter.GetBytes(Header.titleId), XboxHDKey, buffer);
                IO.Position = 0;
                IO.Write(hash, 0, 0x14);
                IO.Flush();

                buffer = null;
                hash   = null;

                errorMessage = null;
                return(true);
            }