c# – SHA-1散列字节数组与文件流

我正在编写一个API,允许用户上传文件(图像,视频等).我使用SHA-1哈希来确保多次上传相同的文件.以前我们只允许较小的文件,所以我将它们读入字节数组并进行散列,但现在我们允许更大的文件,所以我使用的是文件流.问题是SHA-1算法返回不同的哈希值.我需要弄清楚如何获得相同的哈希,无论方法如何,即使我必须将字节数组转换为文件流或其他东西.但是,我尝试将字节数组写入临时文件并读入它,并返回与字节数组相同的哈希值.这是一个示例控制台应用程序,显示我正在做什么:

static void Main(string[] args)
{
    string file = "C:\\CUWCDFileStorage\\temp\\test.png";
    var bytes = File.ReadAllBytes(file);
    using (var stream = File.Open(file, FileMode.Open))
    {
        Console.WriteLine(Sha1HashFile(bytes));   // Returns B7F6D90C30233F91FCEFE05FB49679F8B26C9D80
        Console.WriteLine(Sha1HashFile(stream));  // Returns DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
        Console.WriteLine(Sha1HashFile2(bytes));  // Returns B7F6D90C30233F91FCEFE05FB49679F8B26C9D80
    }

    Console.Read();
}

public static string Sha1HashFile(byte[] file)
{
    using (SHA1Managed sha1 = new SHA1Managed())
    {
        return BitConverter.ToString(sha1.ComputeHash(file)).Replace("-", "");
    }
}

public static string Sha1HashFile(Stream stream)
{
    using (SHA1Managed sha1 = new SHA1Managed())
    {
        return BitConverter.ToString(sha1.ComputeHash(stream)).Replace("-", "");
    }
}

public static string Sha1HashFile2(byte[] bytes)
{
    string file = "C:\\CUWCDFileStorage\\temp\\test2.png";
    File.WriteAllBytes(file, bytes);
    return Sha1HashFile(File.OpenRead(file));
}

我甚至试图将字节数组放入带有新MemoryStream(字节)的MemoryStream中,但这也不起作用.似乎一旦我将文件放在一个字节数组中就无法放回.

编辑:

我从我的示例中删除了一些代码,因为我认为MD5正在运行.这是我用来测试的原始代码:

static void Main(string[] args)
{
    string file = "C:\\CUWCDFileStorage\\temp\\test.png";
    var bytes = File.ReadAllBytes(file);
    using (var stream = File.Open(file, FileMode.Open))
    {
        Console.WriteLine(Md5HashFile(bytes));
        Console.WriteLine(Md5HashFile(stream));
        Console.WriteLine(Sha1HashFile(bytes));
        Console.WriteLine(Sha1HashFile(stream));
        Console.WriteLine(Sha1HashFile2(bytes));
    }

    Console.Read();
}

public static string Md5HashFile(byte[] file)
{
    using (MD5 md5 = MD5.Create())
    {
        return BitConverter.ToString(md5.ComputeHash(file)).Replace("-", "");
    }
}

public static string Sha1HashFile(byte[] file)
{
    using (SHA1Managed sha1 = new SHA1Managed())
    {
        return BitConverter.ToString(sha1.ComputeHash(file)).Replace("-", "");
    }
}

public static string Md5HashFile(Stream stream)
{
    using (MD5 md5 = MD5.Create())
    {
        return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "");
    }
}

public static string Sha1HashFile(Stream stream)
{
    using (SHA1Managed sha1 = new SHA1Managed())
    {
        return BitConverter.ToString(sha1.ComputeHash(stream)).Replace("-", "");
    }
}

public static string Sha1HashFile2(byte[] bytes)
{
    string file = "C:\\CUWCDFileStorage\\temp\\test2.png";
    File.WriteAllBytes(file, bytes);
    return Sha1HashFile(File.OpenRead(file));
}

请参阅下面的答案以解释问题.

解决方法:

问题是当第一路散列时,流被读取到最后.这导致第二个哈希错误.因此,我需要重新打开第二个散列的流,或者在散列第二个方法之前将流回放到开头.这是解决方案:

static void Main(string[] args)
{
    string file = "C:\\CUWCDFileStorage\\temp\\test.png";
    var bytes = File.ReadAllBytes(file);
    using (var stream = File.Open(file, FileMode.Open))
    {
        Console.WriteLine(Md5HashFile(bytes));
        Console.WriteLine(Md5HashFile(stream));
    }

    using (var stream = File.Open(file, FileMode.Open))
    {
        Console.WriteLine(Sha1HashFile(bytes));
        Console.WriteLine(Sha1HashFile(stream));
        Console.WriteLine(Sha1HashFile2(bytes));
    }

    Console.Read();
}

public static string Md5HashFile(byte[] file)
{
    using (MD5 md5 = MD5.Create())
    {
        return BitConverter.ToString(md5.ComputeHash(file)).Replace("-", "");
    }
}

public static string Sha1HashFile(byte[] file)
{
    using (SHA1Managed sha1 = new SHA1Managed())
    {
        return BitConverter.ToString(sha1.ComputeHash(file)).Replace("-", "");
    }
}

public static string Md5HashFile(Stream stream)
{
    using (MD5 md5 = MD5.Create())
    {
        return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "");
    }
}

public static string Sha1HashFile(Stream stream)
{
    using (SHA1Managed sha1 = new SHA1Managed())
    {
        return BitConverter.ToString(sha1.ComputeHash(stream)).Replace("-", "");
    }
}

public static string Sha1HashFile2(byte[] bytes)
{
    string file = "C:\\CUWCDFileStorage\\temp\\test2.png";
    File.WriteAllBytes(file, bytes);
    return Sha1HashFile(File.OpenRead(file));
}
上一篇:使用bash或python或其他一些linux命令行工具创建一个dovecot SHA1摘要


下一篇:java – Hudson使用什么密码加密?