Skip to content

Adding Auth functionality #2

@flodmotorgrodan

Description

@flodmotorgrodan

Tested to add an authorization section inspired by python code from https://github.com/esp8266/Arduino/blob/master/tools/espota.py
It worked and responded OK!

//Password authentication similar to github python code
var nonce = res_text.Split(' ')[1];
var cnonce_text = $"{file_uri}{content_size}{file_md5}{remoteAddr}";
var cnonce = cnonce_text.ToMD5Hash();
var passmd5 = password.ToMD5Hash();
var result_text = $"{passmd5}:{nonce}:{cnonce}";
var result = result_text.ToMD5Hash();
Console.WriteLine("Authenticating...");
message = $"{FLASH_Options.AUTH:D} {cnonce} {result}\n";
message_bytes = message.Encode();

//Send authentication request
sock2.Send(message_bytes, message_bytes.Length, ep);
t = sock2.ReceiveAsync();

Created three extensions String.ToMD5Hash(), Stream.ToMD5Hash and String.Encode() to simplify

public static byte[] Encode(this string @this)
{
    return Encoding.ASCII.GetBytes(@this);
}

/// <summary>
///     A Stream extension method that converts the @this to a md 5 hash.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <returns>@this as a string.</returns>
public static string ToMD5Hash(this Stream @this)
{
    using (MD5 md5 = MD5.Create())
    {
	byte[] hashBytes = md5.ComputeHash(@this);
	var sb = new StringBuilder();
	foreach (byte bytes in hashBytes)
	{
	    sb.Append(bytes.ToString("x2"));
	}

	return sb.ToString();
    }
}

/// <summary>
///     A Stream extension method that converts the @this to a md 5 hash.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <returns>@this as a string.</returns>
public static string ToMD5Hash(this string @this, bool useACII=false)
{
    using (MD5 md5 = MD5.Create())
    {
	byte[] hashBytes = md5.ComputeHash(useACII ? Encoding.ASCII.GetBytes(@this) : Encoding.UTF8.GetBytes(@this));
	var sb = new StringBuilder();
	foreach (byte bytes in hashBytes)
	{
	    sb.Append(bytes.ToString("x2"));
	}

	return sb.ToString();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions