创建SHA哈希并在源已移动位置时进行比较

时间:2019-02-12 17:36:04

标签: c# hash sha

我创建了一个简单的C#程序,该程序可以对文件进行哈希处理(我专注于图像,而不是PDF,文本等),该文件仅在命令行上显示哈希值。我将创建一个函数来输出文件(不确定什么类型?),以便以后可以引用。

这个想法是,图像将从一个位置传播到另一位置,并且一旦到达第二个目的地,哈希程序将再次对其进行检查,找到与之关联的哈希文件,进行比较,然后生成结果(即是否相同)。我拥有的代码不是很相关,因此为什么我没有发布它-我主要是在寻找技巧/提示或此处可能有用的任何文档!

谢谢。

编辑:代码很重要,所以这里很重要

using System;
using System.IO;
using System.Security.Cryptography;

class Hash
{
    static void Main(string[] args)
    {
        if (args.Length != 2)
        {
            byte[] hashValue = null;
            if (!File.Exists(args[1]))
            {
                try
                {
                    FileStream fileStream = File.OpenRead(args[1]);
                    fileStream.Position = 0;
                    switch (args[0].ToUpper())
                    {
                        case "SHA512":
                            // Compute the SHA512 hash of the fileStream.
                            hashValue = SHA512.Create().ComputeHash(fileStream);
                            return;
                    }
                    if (hashValue != null)
                    {
                        PrintHashData(args[0].ToUpper(), fileStream.Name, hashValue);
                    }
                    fileStream.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: {0}", ex.Message);
                }
            }
        }
    }

    private static void PrintHashData(string algorithm, string fileName, byte[] array)
    {
        Console.Write("File: {0}\r\n{1} Hash: ", fileName, algorithm);
        for (int i = 0; i < array.Length; i++)
        {
            Console.Write(String.Format("{0:X2}", array[i]));
        }
        Console.WriteLine();
    }
}

0 个答案:

没有答案