示例#1
0
        /// <summary>
        /// Appends the file with the properties that you configured on this object.
        /// It will be encrypted if you have set the Key propertie.
        /// </summary>
        /// <returns>True when all went ok, false when something failed for some reason.</returns>
        public bool Append()
        {
            if (this.binArchive != null)
            {
                var binFile = new BinFile()
                {
                    version = this.Version,
                    content = File.ReadAllBytes(this.Filename)   // TODO: Will this always fit in memory? For now we just use very simple and small files.
                };

                // Unfortunate we can't everything into the object initialiser:
                binFile.typeValues.AddRange(this.TypeValues);
                binFile.size = (uint)binFile.content.Length;

                // Calculate the checksum in a proper way for our file data. We now that we should place 20 bytes into our test file!
                binFile.check = 3141592653;
                binFile.crc   = calculateCrc32(binFile.content);
                binFile.sha1  = calculateSha1(binFile.content);    // Should always be 20 bytes...

                binFile.time = Helpers.convertToUnixTimeMilliseconds(File.GetCreationTimeUtc(this.Filename));

                // Now add the binFile!
                if (this.Key == null)
                {
                    this.binArchive.binFiles.Add(binFile);
                }
                else
                {
                    // Encrypt it when a key was given!
                    var binFileCrypter = new BinFileCrypter(this.Key);
                    var cryptedBinFile = binFileCrypter.convert(binFile, this.RestrictToValue);
                    this.binArchive.cryptedBinFiles.Add(cryptedBinFile);
                }

                // Also increase our append call, so that we know that we need to finish it really at the end.
                this.appendCall++;
            }
            else
            {
                throw new InvalidOperationException("Trying to call Append() without calling Prepare() first!");
            }

            return(true);
        }
示例#2
0
        /// <summary>
        /// Prints some debug info about a bin file.
        /// </summary>
        public static void PrintBinFile(BinFile binFile)
        {
            Console.WriteLine("Valid BinFile found (version={0}, size={1}, time={2})",
                              binFile.version,
                              binFile.size,
                              Helpers.convertUnixTimeMillisecondsToDateTime(binFile.time).ToString());

            Console.WriteLine("File can be applied to type values: ");
            binFile.typeValues.ForEach(v => Console.Write("{0} ", v));
            Console.Write("\n");

            var savedCrc      = binFile.crc;
            var calculatedCrc = ArchiveAppender.calculateCrc32(binFile.content);

            var savedSha      = Convert.ToBase64String(binFile.sha1);
            var calculatedSha = Convert.ToBase64String(ArchiveAppender.calculateSha1(binFile.content));

            Console.WriteLine("Hashes: CRC-32={0}, SHA-1={1}, Check={2}",
                              savedCrc,
                              savedSha,
                              binFile.check);

            Console.WriteLine("Expected hashes: CRC-32={0}, SHA-1={1}, Check=3141592653",
                              calculatedCrc,
                              calculatedSha);

            if (savedCrc != calculatedCrc)
            {
                Console.WriteLine("CRC ERROR!!!");
            }

            if (savedSha != calculatedSha)
            {
                Console.WriteLine("SHA1 ERROR!!!");
            }
        }