SHA256 or MD5 String

I’m having trouble locating some code to simply create an MD5 Hash from a String … or better yet a SHA256 Hash.

Here, you can have this one I made some time ago (Never actually tested it on a real product, so use it at your own expense).

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

public class CryptoService : EditorWindow
{

	private GUIStyle richTextStyle;
	
	[MenuItem("Window/Crypto Service")]
	public static void ShowWindow()
	{
		GetWindow<CryptoService>();
	}

	void OnSelectionChange()
	{
		Repaint();
	}
	
	void Awake()
	{
		richTextStyle = new GUIStyle();
		richTextStyle.richText = true;
	}
	
	void OnGUI()
	{
		Object current = Selection.activeObject;

		if (current == null)
		{
			EditorGUILayout.HelpBox("No file selected",MessageType.Info);
			return;
		}
		
		string _name = current.name;
		string path = AssetDatabase.GetAssetPath(current);
		
		EditorGUILayout.LabelField("<b>Selected:</b> "+_name + " Type: " + Selection.activeObject.GetType(),richTextStyle);
		EditorGUILayout.LabelField("<b>Type:</b> " + current.GetType(),richTextStyle);
		EditorGUILayout.LabelField("<b>Path:</b> " + path, richTextStyle);	

		if (GUILayout.Button("Generate SHA 256"))
		{
			GenerateSHA256(_name,path);
		}
		if (GUILayout.Button("Generate MD5"))
		{
			GenerateMD5(_name,path);
		}
	}

	private void GenerateSHA256(string _name,string filePath)
	{
		if (!File.Exists(filePath))
		{
			Debug.Log("Path error: File doesn't exist");
			return;
		}
		Debug.Log("Generating SHA256 hash");

		byte[] fileBytes = File.ReadAllBytes(filePath);
		StringBuilder sb = new StringBuilder();

		using (SHA256Managed sha256 = new SHA256Managed())
		{
			byte[] hash = sha256.ComputeHash(fileBytes);
			foreach (Byte b in hash)
				sb.Append(b.ToString("X2"));
		}
		CreateTextFile(_name + "SHA256.txt",filePath,sb.ToString());
	}

	private void GenerateMD5(string _name, string filePath)
	{
		if (!File.Exists(filePath))
		{
			Debug.Log("Path error: File doesn't exist");
			return;
		}
		Debug.Log("Generating MD5 hash");

		byte[] fileBytes = File.ReadAllBytes(filePath);
		StringBuilder sb = new StringBuilder();

		using (MD5 md5 = MD5.Create())
		{
			byte[] hash = md5.ComputeHash(fileBytes);

			foreach (byte b in hash)
				sb.Append(b.ToString("X2"));
		}
		CreateTextFile(_name + "MD5.txt",filePath,sb.ToString());
	}

	private void CreateTextFile(string _name, string _path, string text)
	{
		StreamWriter sw =File.CreateText(Path.GetDirectoryName(_path) + "/" + _name);
		sw.Write(text);
		sw.Close();
		AssetDatabase.Refresh();
	}
}

This is actually an editor window that let’s you select a file to generate an MD5 or SHA256 string out of it, but you can take the parts that you need out of it. For instance, since you want to use a string, you can instead turn that into an array of bytes using

//I used UTF8 encoding here, but you should use the one that works for you
bytes[] stringBytes = Encoding.UTF8.GetBytes("input string");

And passing that instead to the ComputeHash() method.

Hope this helps! :slight_smile: