diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.ValueGenerator.UnitTests/InputParserTests.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.ValueGenerator.UnitTests/InputParserTests.cs index e17437556d..afc2d0232d 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.ValueGenerator.UnitTests/InputParserTests.cs +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.ValueGenerator.UnitTests/InputParserTests.cs @@ -29,6 +29,7 @@ namespace Community.PowerToys.Run.Plugin.ValueGenerator.UnitTests [DataRow("base64 abc", typeof(Base64.Base64Request))] [DataRow("base99 abc", null)] [DataRow("base64s abc", null)] + [DataRow("base64d abc=", typeof(Base64.Base64DecodeRequest))] public void ParserTest(string input, Type? expectedRequestType) { var parser = new InputParser(); @@ -77,7 +78,7 @@ namespace Community.PowerToys.Run.Plugin.ValueGenerator.UnitTests private static bool CommandIsKnown(string command) { - string[] hashes = new string[] { "md5", "sha1", "sha256", "sha384", "sha512", "base64" }; + string[] hashes = new string[] { "md5", "sha1", "sha256", "sha384", "sha512", "base64", "base64d" }; if (hashes.Contains(command.ToLowerInvariant())) { diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.ValueGenerator/Base64/Base64DecodeRequest.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.ValueGenerator/Base64/Base64DecodeRequest.cs new file mode 100644 index 0000000000..ae6623bbc8 --- /dev/null +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.ValueGenerator/Base64/Base64DecodeRequest.cs @@ -0,0 +1,58 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics; +using System.Text; +using Wox.Plugin.Logger; + +namespace Community.PowerToys.Run.Plugin.ValueGenerator.Base64 +{ + public class Base64DecodeRequest : IComputeRequest + { + public byte[] Result { get; set; } + + public string Description => "Base64 Decoding"; + + public bool IsSuccessful { get; set; } + + public string ErrorMessage { get; set; } + + private string DataToDecode { get; set; } + + public Base64DecodeRequest(string dataToDecode) + { + DataToDecode = dataToDecode ?? throw new ArgumentNullException(nameof(dataToDecode)); + } + + public bool Compute() + { + IsSuccessful = true; + try + { + Result = System.Convert.FromBase64String(DataToDecode); + } + catch (Exception e) + { + Log.Exception(e.Message, e, GetType()); + ErrorMessage = e.Message; + IsSuccessful = false; + } + + return IsSuccessful; + } + + public string ResultToString() + { + if (Result != null) + { + return Encoding.UTF8.GetString(Result); + } + else + { + return string.Empty; + } + } + } +} diff --git a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.ValueGenerator/InputParser.cs b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.ValueGenerator/InputParser.cs index 394fbf8752..1581bab297 100644 --- a/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.ValueGenerator/InputParser.cs +++ b/src/modules/launcher/Plugins/Community.PowerToys.Run.Plugin.ValueGenerator/InputParser.cs @@ -121,6 +121,12 @@ namespace Community.PowerToys.Run.Plugin.ValueGenerator string content = query.RawUserQuery.Substring(commandIndex + command.Length).Trim(); request = new Base64Request(Encoding.UTF8.GetBytes(content)); } + else if (command.ToLower(null) == "base64d") + { + int commandIndex = query.RawUserQuery.IndexOf(command, StringComparison.InvariantCultureIgnoreCase); + string content = query.RawUserQuery.Substring(commandIndex + command.Length).Trim(); + request = new Base64DecodeRequest(content); + } else { throw new FormatException($"Invalid Query: {query.RawUserQuery}");