最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I determine the file type of a base64 string, based only on the signature in C# - Stack Overflow

programmeradmin5浏览0评论

I am creating an API call where the user can provide a file in base64 format together with the filename.

I am searching for a way to verify that the provided base64 string matches with the extension of the provided filename. At the moment I'm having the following [reworked based on comment]:

    private static readonly int c_maxSignatureLength = 10;

    public static bool ValidateFileType(string base64Content, string fileName)
    {
        var extension = Path.GetExtension(fileName).ToLower().Replace(".", "");
        List<byte[]> expectedSignatures;
        if (!_base64Signatures.TryGetValue(extension, out expectedSignatures))
        {
            Log.Error($"{fileName} - Not supported file type");
            return false;
        }

        var base64Signature = base64Content.Substring(0, Math.Min(base64Content.Length, c_maxSignatureLength));
        var base64Bytes = Convert.FromBase64String(base64Signature);

        foreach(var signature in expectedSignatures)
        {
            if (base64Bytes.Take(signature.Length).SequenceEqual(signature))
                return true;
        }

        Log.Error($"{fileName} - incorrect base64 signature: ({base64Content.Take(10)})");
        return false;
    }

    private static Dictionary<string, List<byte[]>> _base64Signatures = new Dictionary<string, List<byte[]>>()
    {
        { "gif", [[0x47, 0x49, 0x46, 0x38, 0x37, 0x61], [0x47, 0x49, 0x46, 0x38, 0x39, 0x61]]},
        { "bmp", [[0x42, 0x4D]] },
        { "tif", [[0x49, 0x49, 0x2A, 0x00], [0x4D, 0x4D, 0x00, 0x2A], [0x49, 0x49, 0x2B, 0x00], [0x4D, 0x4D, 0x00, 0x2B]] },
        { "tiff", [[0x49, 0x49, 0x2A, 0x00], [0x4D, 0x4D, 0x00, 0x2A], [0x49, 0x49, 0x2B, 0x00], [0x4D, 0x4D, 0x00, 0x2B]] },
        { "txt", [[0xEF, 0xBB, 0xBF], [0xFF, 0xFE], [0xFE, 0xFF], [0x00, 0x00, 0xFE, 0xFF], [0x0E, 0xFE, 0xFF]]},
        // etc...
    }

I have used as a reference to find the starting signature for different filetypes and /converter/encode/hex to convert these to base64 string.

I have to support a vast array of possible filetypes and not all filetypes are mentioned on wikipedia (which I really don't want to use as a source, but I'm unable to find a decent list anywhere else...)

Does anyone now of a better way to do this? Is there a nuget package out there that contains a list of file extensions and corresponding base64 signatures?

发布评论

评论列表(0)

  1. 暂无评论