From 544fbd19f43404ba26a92e7b94103127c6065db1 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Tue, 16 Apr 2024 18:15:40 +0900 Subject: [PATCH] some fixes --- .../Catswords.DataType.Client.csproj | 51 ++++++++++++++----- .../Helper/FileHasher.cs | 37 +++++++------- .../Helper/InfoHashExtractor.cs | 51 +++++++++++++++++++ Catswords.DataType.Client/packages.config | 1 - 4 files changed, 106 insertions(+), 34 deletions(-) create mode 100644 Catswords.DataType.Client/Helper/InfoHashExtractor.cs diff --git a/Catswords.DataType.Client/Catswords.DataType.Client.csproj b/Catswords.DataType.Client/Catswords.DataType.Client.csproj index 138b732..0b13a66 100644 --- a/Catswords.DataType.Client/Catswords.DataType.Client.csproj +++ b/Catswords.DataType.Client/Catswords.DataType.Client.csproj @@ -12,6 +12,21 @@ 512 true true + 게시\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true AnyCPU @@ -42,9 +57,6 @@ socialonthefile.snk - - ..\packages\BencodeNET.4.0.0\lib\netstandard2.0\BencodeNET.dll - ..\packages\Crc32.NET.1.2.0\lib\net20\Crc32.NET.dll @@ -103,6 +115,7 @@ + @@ -192,16 +205,6 @@ - - - {607eeb2c-6b7c-409e-959e-3b458a109426} - AndroidXml - - - {b1244c5a-cdeb-4d1d-8807-bf40251abcab} - SsdeepNET - - @@ -214,5 +217,27 @@ + + + False + Microsoft .NET Framework 4.8%28x86 및 x64%29 + true + + + False + .NET Framework 3.5 SP1 + false + + + + + {e55ce5e9-aeec-4625-82ff-9c73dad2ba84} + AndroidXml + + + {1fc34ef4-5137-4069-a18a-d35afec5181f} + SsdeepNET + + \ No newline at end of file diff --git a/Catswords.DataType.Client/Helper/FileHasher.cs b/Catswords.DataType.Client/Helper/FileHasher.cs index a01f29c..d85d5f1 100644 --- a/Catswords.DataType.Client/Helper/FileHasher.cs +++ b/Catswords.DataType.Client/Helper/FileHasher.cs @@ -1,6 +1,4 @@ -using BencodeNET.Parsing; -using BencodeNET.Torrents; -using Force.Crc32; +using Force.Crc32; using SsdeepNET; using System; using System.IO; @@ -20,26 +18,26 @@ namespace Catswords.DataType.Client.Helper public string GetExtension() { + string extension = string.Empty; + try { if (Path.GetExtension(FilePath).Length > 0) { - return Path.GetExtension(FilePath).Substring(1).ToUpper(); - } - else - { - return ""; + extension = Path.GetExtension(FilePath).Substring(1).ToLower(); } } - catch (Exception) + catch { - return ""; + // nothing } + + return extension; } public string GetMD5() { - string checksum = ""; + string checksum = string.Empty; using (MD5 hasher = MD5.Create()) { @@ -55,7 +53,7 @@ namespace Catswords.DataType.Client.Helper public string GetSHA1() { - string checksum = ""; + string checksum = string.Empty; using (SHA1 hasher = SHA1.Create()) { @@ -71,7 +69,7 @@ namespace Catswords.DataType.Client.Helper public string GetCRC32() { - string checksum = ""; + string checksum = string.Empty; using (FileStream stream = File.OpenRead(FilePath)) { @@ -85,7 +83,7 @@ namespace Catswords.DataType.Client.Helper public string GetSHA256() { - string checksum = ""; + string checksum = string.Empty; using (SHA256 hasher = SHA256.Create()) { @@ -134,14 +132,13 @@ namespace Catswords.DataType.Client.Helper public string GetInfoHash() { - string checksum = ""; + string checksum = string.Empty; string extension = GetExtension().ToLower(); if (extension == "torrent") { - BencodeParser parser = new BencodeParser(); - Torrent torrent = parser.Parse(FilePath); - checksum = BitConverter.ToString(torrent.GetInfoHashBytes()).Replace("-", "").ToLowerInvariant(); + var extractor = new InfoHashExtractor(FilePath); + checksum = extractor.GetString(); } return checksum; @@ -149,7 +146,7 @@ namespace Catswords.DataType.Client.Helper public string GetSSDEEP() { - string checksum = ""; + string checksum = string.Empty; using (FileStream stream = File.OpenRead(FilePath)) { @@ -165,7 +162,7 @@ namespace Catswords.DataType.Client.Helper public string GetHexView(byte[] Data) { - string output = ""; + string output = string.Empty; StringBuilder strb = new StringBuilder(); StringBuilder text = new StringBuilder(); diff --git a/Catswords.DataType.Client/Helper/InfoHashExtractor.cs b/Catswords.DataType.Client/Helper/InfoHashExtractor.cs new file mode 100644 index 0000000..a810590 --- /dev/null +++ b/Catswords.DataType.Client/Helper/InfoHashExtractor.cs @@ -0,0 +1,51 @@ +using System; +using System.IO; + +namespace Catswords.DataType.Client.Helper +{ + public class InfoHashExtractor + { + private string FilePath; + + public InfoHashExtractor(string filePath) + { + FilePath = filePath; + } + + public string GetString() + { + string infoHashString = ""; + + try + { + // 토렌트 파일을 바이트 배열로 읽어옴 + byte[] torrentData = File.ReadAllBytes(FilePath); + + // 토렌트 파일에서 InfoHash 추출 + byte[] infoHash = ExtractInfoHash(torrentData); + + // InfoHash를 문자열로 변환하여 출력 + infoHashString = BitConverter.ToString(infoHash).Replace("-", "").ToLower(); + } + catch (Exception ex) + { + Console.WriteLine("An error occurred: " + ex.Message); + } + + return infoHashString; + } + + private byte[] ExtractInfoHash(byte[] torrentData) + { + // 토렌트 파일의 구조에 따라 InfoHash 위치 파악 + int start = Array.IndexOf(torrentData, (byte)'4', 0); + int end = Array.IndexOf(torrentData, (byte)'e', start); + + // InfoHash 추출 + byte[] infoHash = new byte[20]; + Array.Copy(torrentData, start + 1, infoHash, 0, 20); + + return infoHash; + } + } +} diff --git a/Catswords.DataType.Client/packages.config b/Catswords.DataType.Client/packages.config index b2d7bb9..f4a53b4 100644 --- a/Catswords.DataType.Client/packages.config +++ b/Catswords.DataType.Client/packages.config @@ -1,6 +1,5 @@  -