Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //default includes
- using System;
- using System.Linq;
- using System.Collections.Generic;
- //additional includes
- using System.IO;
- using System.Security.Cryptography;
- namespace _3DS_NAND_Checker
- {
- class Program
- {
- private const string ImgDir = "imgs\\";
- private const string LogDir = "logs\\";
- //nand sizes listed here --> https://github.com/Plailect/Guide/wiki/NAND-Size
- private static Int64[] imgSizes = {
- 988807168, 1979711488, 1000341504, 1300234240, //2ds
- 988807168, 1000341504, //old 3ds
- 1979711488, 1300234240, 1954545664 //new 3ds
- };
- static void Main(string[] args)
- {
- //set console title
- Console.Title = "3DS NAND Checker";
- //create imgs directory if it doesn't exist
- if (!Directory.Exists(ImgDir))
- {
- Directory.CreateDirectory(ImgDir);
- }
- //create log directory if it doesn't exist
- if (!Directory.Exists(LogDir))
- {
- Directory.CreateDirectory(LogDir);
- }
- //create log file name
- DateTime dt = DateTime.Now;
- string logFile = Path.Combine(LogDir, dt.ToString("dd-MM-yyyy_hh-mm-ss") + ".log");
- //get list of imgs
- string[] imgFiles = Directory.GetFiles(ImgDir, "*.img");
- WriteColor(ConsoleColor.Yellow, String.Format("Checking {0} img files...", imgFiles.Length));
- //check if any img files exist
- if (imgFiles.Length > 1)
- {
- //create log file stream
- List<string> imgHashes = new List<string>();
- using (StreamWriter sw = new StreamWriter(logFile))
- {
- //loop through the files and calculate the hashes for them
- foreach (string singleImg in imgFiles)
- {
- //check the file size against the list
- Int64 fileSize = new FileInfo(singleImg).Length;
- if (imgSizes.Contains(fileSize))
- {
- string hashStr = HashLargeFile(singleImg);
- string logStr = String.Format("\"{0}\" --> {1} bytes --> {2}", singleImg, fileSize, hashStr);
- sw.WriteLine(logStr);
- Console.WriteLine(logStr);
- imgHashes.Add(hashStr);
- }
- else
- {
- string logStr = String.Format("Invalid NAND size for \"{0}\"", singleImg);
- sw.WriteLine(logStr);
- WriteColor(ConsoleColor.Red, logStr);
- }
- }
- }
- //make sure that all the images passed the size checks
- if (imgHashes.Count() == imgFiles.Length)
- {
- //check if all of them are the same
- string firstItem = imgHashes[0];
- bool allEqual = imgHashes.Skip(1).All(s => string.Equals(firstItem, s, StringComparison.InvariantCultureIgnoreCase));
- //write the result to the console
- if (allEqual) //all of the dumps match
- {
- WriteColor(ConsoleColor.Green, "All the NAND's are the same :D");
- }
- else //not all of the dumps match
- {
- WriteColor(ConsoleColor.Red, "Some of the NAND's aren't the same :'(");
- }
- }
- else
- {
- WriteColor(ConsoleColor.Red, "Too many NAND's failed file size checks");
- }
- }
- else
- {
- WriteColor(ConsoleColor.Red, "You need two or more NAND dumps in the imgs directory to use this tool");
- }
- //wait on enter to be pressed
- Console.WriteLine("Press ENTER to exit...");
- Console.ReadKey();
- }
- /// <summary>
- /// Hash a file using SHA-1
- /// </summary>
- /// <param name="fileName">The name of the file to hash</param>
- /// <returns>The SHA-1 checksum of the file</returns>
- private static string HashLargeFile(string fileName)
- {
- string hash = String.Empty;
- using (SHA1CryptoServiceProvider shacsp = new SHA1CryptoServiceProvider())
- using (FileStream fs = File.Open(fileName, FileMode.Open))
- {
- foreach (byte b in shacsp.ComputeHash(fs))
- {
- hash += b.ToString("x2").ToLower();
- }
- }
- return hash;
- }
- /// <summary>
- /// Used to pretty up the application a bit because gray all the time is boring
- /// </summary>
- /// <param name="cc">The color to print with</param>
- /// <param name="text">The text to print</param>
- private static void WriteColor(ConsoleColor cc, string text)
- {
- Console.ForegroundColor = cc;
- Console.WriteLine(text);
- Console.ResetColor();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement