Here is my conversation with OpenAI where I tell the artificial intelligence to create code for me. What I want is a database with a table that holds two hashes and the file size so I can check if a file is unique or not.
It’s simple, but quite effective and took me less than 30 minutes to get the final code. Will it work? Well, I’m going to test that soon enough. My input is in bold fonts.
I need to compare if a file is identical to another file. For this I want to calculate over the hash. And then calculate a second hash over the same file with a different algorithm. Both hashes are then stored in a C# class together with the file size in bytes. Write this code for me in C#.
using System;
using System.Security.Cryptography;
public class FileHash
{
public string Hash1 { get; set; }
public string Hash2 { get; set; }
public long FileSize { get; set; }
public FileHash(string filePath)
{
// Calculate hash of file using SHA256
using (SHA256 SHA256 = SHA256.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = SHA256.ComputeHash(fs);
}
// Store hash in Hex format
this.Hash1 = BitConverter.ToString(hashBytes).Replace("-", "");
}
// Calculate hash of file using SHA512
using (SHA512 SHA512 = SHA512.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = SHA512.ComputeHash(fs);
}
// Store hash in Hex format
this.Hash2 = BitConverter.ToString(hashBytes).Replace("-", "");
}
// Get file size
this.FileSize = new FileInfo(filePath).Length;
}
}
Use two different hashing algorithms. Add salt.
using System;
using System.Security.Cryptography;
using System.IO;
public class FileHash
{
public string Hash1 { get; set; }
public string Hash2 { get; set; }
public long FileSize { get; set; }
public FileHash(string filePath, string salt)
{
// Calculate hash of file using SHA256
using (SHA256 SHA256 = SHA256.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = SHA256.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this.Hash1 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Calculate hash of file using SHA512
using (SHA512 SHA512 = SHA512.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = SHA512.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this.Hash2 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Get file size
this.FileSize = new FileInfo(filePath).Length;
}
}
Do not use SHA256.
using System;
using System.Security.Cryptography;
using System.IO;
public class FileHash
{
public string Hash1 { get; set; }
public string Hash2 { get; set; }
public long FileSize { get; set; }
public FileHash(string filePath, string salt)
{
// Calculate hash of file using MD5
using (MD5 MD5 = MD5.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = MD5.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this.Hash1 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Calculate hash of file using SHA1
using (SHA1 SHA1 = SHA1.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = SHA1.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this.Hash2 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Get file size
this.FileSize = new FileInfo(filePath).Length;
}
}
Make fields of the properties. Add a constant “Knowledge And Technology Joyfully Engaged is salting this data.” Us this as salt.
using System;
using System.Security.Cryptography;
using System.IO;
public class FileHash
{
private string _hash1;
private string _hash2;
private long _fileSize;
public string Hash1 { get { return _hash1; } }
public string Hash2 { get { return _hash2; } }
public long FileSize { get { return _fileSize; } }
public const string Salt = "Knowledge And Technology Joyfully Engaged is salting this data.";
public FileHash(string filePath)
{
// Calculate hash of file using MD5
using (MD5 MD5 = MD5.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = MD5.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(Salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this._hash1 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Calculate hash of file using SHA1
using (SHA1 SHA1 = SHA1.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = SHA1.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(Salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this._hash2 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Get file size
this._fileSize = new FileInfo(filePath).Length;
}
}
Make the salt private. Remove the backing fields.
using System;
using System.Security.Cryptography;
using System.IO;
public class FileHash
{
public string Hash1 { get; private set; }
public string Hash2 { get; private set; }
public long FileSize { get; private set; }
public const string Salt = "Knowledge And Technology Joyfully Engaged is salting this data.";
public FileHash(string filePath)
{
// Calculate hash of file using MD5
using (MD5 MD5 = MD5.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = MD5.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(Salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this.Hash1 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Calculate hash of file using SHA1
using (SHA1 SHA1 = SHA1.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = SHA1.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(Salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this.Hash2 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Get file size
this.FileSize = new FileInfo(filePath).Length;
}
}
What will each hash look like?
The hashes will be a string of 32 hexadecimal characters, with each character being a combination of 0-9 and A-F.
Add an unique Guid as identifier. Make this class ready to be used in Entity Framework 6.
public class FileHash
{
public Guid Id { get; set; }
public string Hash1 { get; private set; }
public string Hash2 { get; private set; }
public long FileSize { get; private set; }
public const string Salt = "Knowledge And Technology Joyfully Engaged is salting this data.";
public FileHash(string filePath)
{
this.Id = Guid.NewGuid();
// Calculate hash of file using MD5
using (MD5 MD5 = MD5.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = MD5.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(Salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this.Hash1 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Calculate hash of file using SHA1
using (SHA1 SHA1 = SHA1.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = SHA1.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(Salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this.Hash2 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Get file size
this.FileSize = new FileInfo(filePath).Length;
}
}
Replace the constructor by a class factory.
public class FileHash
{
public Guid Id { get; set; }
public string Hash1 { get; private set; }
public string Hash2 { get; private set; }
public long FileSize { get; private set; }
public const string Salt = "Knowledge And Technology Joyfully Engaged is salting this data.";
private FileHash(string filePath)
{
this.Id = Guid.NewGuid();
// Calculate hash of file using MD5
using (MD5 MD5 = MD5.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = MD5.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(Salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this.Hash1 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Calculate hash of file using SHA1
using (SHA1 SHA1 = SHA1.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = SHA1.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(Salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this.Hash2 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Get file size
this.FileSize = new FileInfo(filePath).Length;
}
public static FileHash Create(string filePath)
{
return new FileHash(filePath);
}
}
Create a DBContext.
public class FileHashContext : DbContext
{
public DbSet<FileHash> FileHashes { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=localhost;Database=FileHashDatabase;User ID=sa;Password=password;");
}
}
Use MariaDB. Server is “katje.org”, database is “FileData”, user is “FileManager”, password is “123456”.
public class FileHashContext : DbContext
{
public DbSet<FileHash> FileHashes { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMariaDB("Server=katje.org;Database=FileData;User ID=FileManager;Password=123456;");
}
}
Make the Id field unique. Add an unique index to the combined values of Hash1, Hash2 and FileSize.
public class FileHash
{
[Key]
public Guid Id { get; set; }
public string Hash1 { get; private set; }
public string Hash2 { get; private set; }
public long FileSize { get; private set; }
public const string Salt = "Knowledge And Technology Joyfully Engaged is salting this data.";
private FileHash(string filePath)
{
this.Id = Guid.NewGuid();
// Calculate hash of file using MD5
using (MD5 MD5 = MD5.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = MD5.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(Salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this.Hash1 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Calculate hash of file using SHA1
using (SHA1 SHA1 = SHA1.Create())
{
byte[] hashBytes;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
hashBytes = SHA1.ComputeHash(fs);
}
// Add salt to hash
byte[] saltBytes = Encoding.UTF8.GetBytes(Salt);
byte[] saltedHashBytes = new byte[hashBytes.Length + saltBytes.Length];
Array.Copy(hashBytes, saltedHashBytes, hashBytes.Length);
Array.Copy(saltBytes, 0, saltedHashBytes, hashBytes.Length, saltBytes.Length);
// Store hash in Hex format
this.Hash2 = BitConverter.ToString(saltedHashBytes).Replace("-", "");
}
// Get file size
this.FileSize = new FileInfo(filePath).Length;
}
public static FileHash Create(string filePath)
{
return new FileHash(filePath);
}
}
public class FileHashContext : DbContext
{
public DbSet<FileHash> FileHashes { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<FileHash>().HasIndex(x => new { x.Hash1, x.Hash2, x.FileSize }).IsUnique();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMariaDB("Server=katje.org;Database=FileData;User ID=FileManager;Password=123456;");
}
}
What does the table look like? Give me the CREATE SQL code.
CREATE TABLE FileHash (
Id BINARY(16) NOT NULL PRIMARY KEY,
Hash1 VARCHAR(128) NOT NULL,
Hash2 VARCHAR(128) NOT NULL,
FileSize BIGINT NOT NULL,
UNIQUE INDEX (Hash1, Hash2, FileSize)
);
Summarize the code.
FileHash is a class that stores the hashes and file size of a file. It uses the MD5 and SHA1 algorithms to generate the hashes, and also adds salt to the hashes for increased security. The FileHashContext class is a database context that uses MariaDB to store the FileHash objects. The database contains a table with columns for the Id, Hash1, Hash2, and FileSize, and an index that ensures that each combination of hashes and file sizes is unique.

It amazes me with how little instructions I managed to get all this code. And it looks okay to me. Thing is, I’m going to make some manual changes to this code to make it more my style, but having OpenAI create most of this code for me is definitely going to save me a lot of time in the future!
Will OpenAI and other AI replace developers? That is unlikely, as developers still have to evaluate the resulting code and someone has to actually tell the AI to what it needs to make. And while telling the AI to create it all, I also had to check the code over and over again and have the AI replace code I did not like.