Advertisement
Visual_Studio

3DS NAND Checker Source

Aug 24th, 2016
1,411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. //default includes
  2. using System;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. //additional includes
  7. using System.IO;
  8. using System.Security.Cryptography;
  9.  
  10. namespace _3DS_NAND_Checker
  11. {
  12.     class Program
  13.     {
  14.         private const string ImgDir = "imgs\\";
  15.         private const string LogDir = "logs\\";
  16.  
  17.         //nand sizes listed here --> https://github.com/Plailect/Guide/wiki/NAND-Size
  18.         private static Int64[] imgSizes = {
  19.             988807168, 1979711488, 1000341504, 1300234240,  //2ds
  20.             988807168, 1000341504, //old 3ds
  21.             1979711488, 1300234240, 1954545664  //new 3ds
  22.         };
  23.  
  24.         static void Main(string[] args)
  25.         {
  26.             //set console title
  27.             Console.Title = "3DS NAND Checker";
  28.  
  29.             //create imgs directory if it doesn't exist
  30.             if (!Directory.Exists(ImgDir))
  31.             {
  32.                 Directory.CreateDirectory(ImgDir);
  33.             }
  34.  
  35.             //create log directory if it doesn't exist
  36.             if (!Directory.Exists(LogDir))
  37.             {
  38.                 Directory.CreateDirectory(LogDir);
  39.             }
  40.  
  41.             //create log file name
  42.             DateTime dt = DateTime.Now;
  43.             string logFile = Path.Combine(LogDir, dt.ToString("dd-MM-yyyy_hh-mm-ss") + ".log");
  44.  
  45.             //get list of imgs
  46.             string[] imgFiles = Directory.GetFiles(ImgDir, "*.img");
  47.  
  48.             WriteColor(ConsoleColor.Yellow, String.Format("Checking {0} img files...", imgFiles.Length));
  49.  
  50.             //check if any img files exist
  51.             if (imgFiles.Length > 1)
  52.             {
  53.                 //create log file stream
  54.                 List<string> imgHashes = new List<string>();
  55.                 using (StreamWriter sw = new StreamWriter(logFile))
  56.                 {
  57.                     //loop through the files and calculate the hashes for them
  58.                     foreach (string singleImg in imgFiles)
  59.                     {
  60.                         //check the file size against the list
  61.                         Int64 fileSize = new FileInfo(singleImg).Length;
  62.                         if (imgSizes.Contains(fileSize))
  63.                         {
  64.                             string hashStr = HashLargeFile(singleImg);
  65.                             string logStr = String.Format("\"{0}\" --> {1} bytes --> {2}", singleImg, fileSize, hashStr);
  66.                             sw.WriteLine(logStr);
  67.                             Console.WriteLine(logStr);
  68.                             imgHashes.Add(hashStr);
  69.                         }
  70.                         else
  71.                         {
  72.                             string logStr = String.Format("Invalid NAND size for \"{0}\"", singleImg);
  73.                             sw.WriteLine(logStr);
  74.                             WriteColor(ConsoleColor.Red, logStr);
  75.                         }
  76.                     }
  77.                 }
  78.  
  79.                 //make sure that all the images passed the size checks
  80.                 if (imgHashes.Count() == imgFiles.Length)
  81.                 {
  82.                     //check if all of them are the same
  83.                     string firstItem = imgHashes[0];
  84.                     bool allEqual = imgHashes.Skip(1).All(s => string.Equals(firstItem, s, StringComparison.InvariantCultureIgnoreCase));
  85.  
  86.                     //write the result to the console
  87.                     if (allEqual)  //all of the dumps match
  88.                     {
  89.                         WriteColor(ConsoleColor.Green, "All the NAND's are the same :D");
  90.                     }
  91.                     else  //not all of the dumps match
  92.                     {
  93.                         WriteColor(ConsoleColor.Red, "Some of the NAND's aren't the same :'(");
  94.                     }
  95.                 }
  96.                 else
  97.                 {
  98.                     WriteColor(ConsoleColor.Red, "Too many NAND's failed file size checks");
  99.                 }
  100.             }
  101.             else
  102.             {
  103.                 WriteColor(ConsoleColor.Red, "You need two or more NAND dumps in the imgs directory to use this tool");
  104.             }
  105.  
  106.             //wait on enter to be pressed
  107.             Console.WriteLine("Press ENTER to exit...");
  108.             Console.ReadKey();
  109.         }
  110.  
  111.         /// <summary>
  112.         /// Hash a file using SHA-1
  113.         /// </summary>
  114.         /// <param name="fileName">The name of the file to hash</param>
  115.         /// <returns>The SHA-1 checksum of the file</returns>
  116.         private static string HashLargeFile(string fileName)
  117.         {
  118.             string hash = String.Empty;
  119.             using (SHA1CryptoServiceProvider shacsp = new SHA1CryptoServiceProvider())
  120.             using (FileStream fs = File.Open(fileName, FileMode.Open))
  121.             {
  122.                 foreach (byte b in shacsp.ComputeHash(fs))
  123.                 {
  124.                     hash += b.ToString("x2").ToLower();
  125.                 }
  126.             }
  127.             return hash;
  128.         }
  129.  
  130.         /// <summary>
  131.         /// Used to pretty up the application a bit because gray all the time is boring
  132.         /// </summary>
  133.         /// <param name="cc">The color to print with</param>
  134.         /// <param name="text">The text to print</param>
  135.         private static void WriteColor(ConsoleColor cc, string text)
  136.         {
  137.             Console.ForegroundColor = cc;
  138.             Console.WriteLine(text);
  139.             Console.ResetColor();
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement