Problem Deploying to Artifactory with Checksum (C#)

橙三吉。 提交于 2020-02-06 08:42:24

问题


Okay, Well, I'm trying to figure out how to do this.
I've ready the API and and yet this never works.

using (HttpClient client = new HttpClient())
{
    if (!path.EndsWith("/")) path = $"{path}/";
    string url = config.CreateRequest(client, null, $"{path}{file.Name}");
    string sha1 = JFrogLoader.GetSha1Hash(file);
    string sha256 = JFrogLoader.GetSha256Hash(file);
    string md5 = JFrogLoader.GetMD5Hash(file);
    using (Stream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        StreamContent content = new StreamContent(stream);
        client.DefaultRequestHeaders.Add("X-Checksum-Deploy", "true");
        client.DefaultRequestHeaders.Add("X-Checksum-Sha1", sha1);
        client.DefaultRequestHeaders.Add("X-Checksum-Sha256", sha256);
        client.DefaultRequestHeaders.Add("X-Checksum", md5);
        content.Headers.Add("Content-Type", "application/octet-stream");
        HttpResponseMessage message = await client.PutAsync(url, content);
        string response = await message.Content.ReadAsStringAsync();
        return message.StatusCode == System.Net.HttpStatusCode.Created;
    }
}

The thing is, if I don't use any of the "X-Checksum-" header items, the deploy works, but when you navigate to the page in Artifactory, it has the "Fix Checksum" button. So I figured I should probably provide them. My methods that generate the checksums use the "*CryptoServiceProvider" classes, and trim the final '=' from the computed hash string. But every time I add in the checksum headers, I get a multi-part exception for "Unable to read data from the transport connection : An existing connection was forcibly closed by the remote host".

I've tried using the content.Headers and client.DefaultRequestHeaders. I've tried only providing the SHa1.
I've tried naming the md5 X-Checksum-Md5 (which is not in the api, but figured it was worth a shot). Nothing works, and i get a connection closed by host.

Any ideas how I should resolve this? Thanks in advance.


回答1:


Well, took a while but using a little trial and error, i found the problem was in the has algorithm I was using.

The Correct Solution to load this via C# with checksums is as follows:

using (HttpClient client = new HttpClient())
{
    if (!path.EndsWith("/")) path = $"{path}/";
    string url = config.CreateRequest(client, null, $"{path}{file.Name}");
    string sha1 = JFrogLoader.GetSha1Hash(file);
    string sha256 = JFrogLoader.GetSha256Hash(file);
    string md5 = JFrogLoader.GetMD5Hash(file);
    using (Stream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        StreamContent content = new StreamContent(stream);
        content.Headers.Add("Content-Type", "application/octet-stream");
        client.DefaultRequestHeaders.Add("X-Checksum-Deploy", "true");
        client.DefaultRequestHeaders.Add("X-Checksum-Sha1", sha1);
        client.DefaultRequestHeaders.Add("X-Checksum-Sha256", sha256);
        client.DefaultRequestHeaders.Add("X-Checksum-Md5", md5);
        HttpResponseMessage message = await client.PutAsync(url, content);

        string response = await message.Content.ReadAsStringAsync();
        return message.StatusCode == System.Net.HttpStatusCode.Created;
    }
}

public static string GetSha1Hash(FileInfo file)
{
    using (var sha1 = new SHA1CryptoServiceProvider())
    {
        return JFrogLoader.GetHash(file, sha1);
    }
}

public static string GetSha256Hash(FileInfo file)
{
    using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
    {
        return JFrogLoader.GetHash(file, sha256);
    }
}

public static string GetMD5Hash(FileInfo file)
{
    using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
    {
        return JFrogLoader.GetHash(file, md5);
    }
}

private static string GetHash(FileInfo file, HashAlgorithm hasher)
{
    using (Stream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        byte[] hash = hasher.ComputeHash(stream);
        return BitConverter.ToString(hash).Replace("-", "").ToLower();
    }
}

The key was originally i was using a Convert.ToBase64String instead of the BitConverter to a straight hex string. Tragically, Artifcatory was not clear in it's error message, and triggered an exception in the web request.

Thanks



来源:https://stackoverflow.com/questions/58424210/problem-deploying-to-artifactory-with-checksum-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!