forked from rohitcoderCdefense/vulnCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CWE-502.cs
38 lines (37 loc) · 1.11 KB
/
CWE-502.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Security.Cryptography;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string path = "C:\\Users\\user\\Desktop\\test.txt";
string hash = "C:\\Users\\user\\Desktop\\hash.txt";
string hashValue = File.ReadAllText(hash);
string fileHash = GetHash(path);
if (hashValue == fileHash)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream(path, FileMode.Open);
bf.Deserialize(fs);
}
else
{
Console.WriteLine("File is modified");
}
}
public static string GetHash(string path)
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(path))
{
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLowerInvariant();
}
}
}
}
}