Files

120 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace AxCopilot.Services.Agent;
public class HashTool : IAgentTool
{
public string Name => "hash_tool";
public string Description => "Compute hash digests for text or files. Supports MD5, SHA1, SHA256, SHA512. Use 'text' mode for inline text or 'file' mode for file path.";
public ToolParameterSchema Parameters
{
get
{
ToolParameterSchema toolParameterSchema = new ToolParameterSchema();
Dictionary<string, ToolProperty> dictionary = new Dictionary<string, ToolProperty>();
ToolProperty obj = new ToolProperty
{
Type = "string",
Description = "Input mode"
};
int num = 2;
List<string> list = new List<string>(num);
CollectionsMarshal.SetCount(list, num);
Span<string> span = CollectionsMarshal.AsSpan(list);
span[0] = "text";
span[1] = "file";
obj.Enum = list;
dictionary["mode"] = obj;
dictionary["input"] = new ToolProperty
{
Type = "string",
Description = "Text to hash or file path"
};
ToolProperty obj2 = new ToolProperty
{
Type = "string",
Description = "Hash algorithm (default: sha256)"
};
num = 4;
List<string> list2 = new List<string>(num);
CollectionsMarshal.SetCount(list2, num);
Span<string> span2 = CollectionsMarshal.AsSpan(list2);
span2[0] = "md5";
span2[1] = "sha1";
span2[2] = "sha256";
span2[3] = "sha512";
obj2.Enum = list2;
dictionary["algorithm"] = obj2;
toolParameterSchema.Properties = dictionary;
num = 2;
List<string> list3 = new List<string>(num);
CollectionsMarshal.SetCount(list3, num);
Span<string> span3 = CollectionsMarshal.AsSpan(list3);
span3[0] = "mode";
span3[1] = "input";
toolParameterSchema.Required = list3;
return toolParameterSchema;
}
}
public Task<ToolResult> ExecuteAsync(JsonElement args, AgentContext context, CancellationToken ct = default(CancellationToken))
{
string text = args.GetProperty("mode").GetString() ?? "text";
string text2 = args.GetProperty("input").GetString() ?? "";
JsonElement value;
string text3 = (args.TryGetProperty("algorithm", out value) ? (value.GetString() ?? "sha256") : "sha256");
try
{
byte[] array;
string value2;
if (text == "file")
{
string text4 = (Path.IsPathRooted(text2) ? text2 : Path.Combine(context.WorkFolder, text2));
if (!File.Exists(text4))
{
return Task.FromResult(ToolResult.Fail("File not found: " + text4));
}
array = File.ReadAllBytes(text4);
value2 = Path.GetFileName(text4);
}
else
{
array = Encoding.UTF8.GetBytes(text2);
value2 = $"text ({array.Length} bytes)";
}
if (1 == 0)
{
}
HashAlgorithm hashAlgorithm = text3 switch
{
"md5" => MD5.Create(),
"sha1" => SHA1.Create(),
"sha256" => SHA256.Create(),
"sha512" => SHA512.Create(),
_ => SHA256.Create(),
};
if (1 == 0)
{
}
using HashAlgorithm hashAlgorithm2 = hashAlgorithm;
byte[] array2 = hashAlgorithm2.ComputeHash(array);
string value3 = BitConverter.ToString(array2).Replace("-", "").ToLowerInvariant();
return Task.FromResult(ToolResult.Ok($"{text3.ToUpperInvariant()}({value2}):\n{value3}"));
}
catch (Exception ex)
{
return Task.FromResult(ToolResult.Fail("해시 오류: " + ex.Message));
}
}
}