C# 如何将 MD5 哈希转换为字符串并将其用作文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/760166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:10:29  来源:igfitidea点击:

How to convert an MD5 hash to a string and use it as a file name

c#md5

提问by Malcolm

I am taking the MD5 hash of an image file and I want to use the hash as a filename.

我正在获取图像文件的 MD5 哈希值,并且我想使用该哈希值作为文件名。

How do I convert the hash to a string that is valid filename?

如何将哈希转换为有效文件名的字符串?

EDIT: toString()just gives "System.Byte[]"

编辑:toString()只给出“System.Byte[]”

回答by Jonathan Rupp

System.Convert.ToBase64String

System.Convert.ToBase64String

As a commenter pointed out -- normal base 64 encoding can contain a '/' character, which obivously will be a problem with filenames. However, there are other characters that are usable, such as an underscore - just replace all the '/' with an underscore.

正如评论者指出的那样——正常的 base 64 编码可以包含一个“/”字符,这显然是文件名的问题。但是,还有其他可用的字符,例如下划线 - 只需将所有“/”替换为下划线即可。

string filename = Convert.ToBase64String(md5HashBytes).Replace("/","_");

回答by LukeH

How about this:

这个怎么样:

string filename = BitConverter.ToString(yourMD5ByteArray);

If you prefer a shorter filename without hyphens then you can just use:

如果您更喜欢不带连字符的较​​短文件名,则可以使用:

string filename =
    BitConverter.ToString(yourMD5ByteArray).Replace("-", string.Empty);

回答by meandmycode

Technically using Base64 is bad if this is Windows, filenames are case insensitive (at least in explorers view).. but in base64, 'a' is different to 'A', this means that perhaps unlikely but you end up with even higher rate of collision..

从技术上讲,如果是 Windows,则使用 Base64 是不好的,文件名不区分大小写(至少在资源管理器视图中)。碰撞..

A better alternative is hexadecimal like the bitconverter class, or if you can- use base32 encoding (which after removing the padding from both base64 and base32, and in the case of 128bit, will give you similar length filenames).

更好的选择是十六进制,如 bitconverter 类,或者如果您可以使用 base32 编码(在从 base64 和 base32 中删除填充后,在 128 位的情况下,将为您提供相似长度的文件名)。

回答by jrista

Try this:

尝试这个:

Guid guid = new Guid(md5HashBytes);
string hashString = guid.ToString("N"); 
// format: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

string hashString = guid.ToString("D"); 
// format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

string hashString = guid.ToString("B"); 
// format: {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

string hashString = guid.ToString("P"); 
// format: (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

回答by Erkan

This is probably the safest for file names. You always get a hex string and never worry about / or +, etc.

这可能是最安全的文件名。你总是得到一个十六进制字符串,而不必担心 / 或 + 等。

        byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(inputString));
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        string hashed = sBuilder.ToString();

回答by D.Gjinovci

Try this:

尝试这个:

string Hash = Convert.ToBase64String(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes("sample")));
//input "sample" returns = Xo/5v1W6NQgZnSLphBKb5g==

or

或者

string Hash = BitConverter.ToString(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes("sample")));
//input "sample" returns = 5E-8F-F9-BF-55-BA-35-08-19-9D-22-E9-84-12-9B-E6

回答by Yury Chaikou

Try Base32 hash of MD5. It gives filename-safe case insensitive strings.

尝试使用 MD5 的 Base32 哈希。它提供文件名安全的不区分大小写的字符串。

    string Base32Hash(string input)
    {
        byte[] buf = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
        return String.Join("", buf.Select(b => "abcdefghijklmonpqrstuvwxyz234567"[b & 0x1F]));
    }