From 79d824cfb0e0cc9ff4fab0e0c546a83c0edaae1c Mon Sep 17 00:00:00 2001 From: vnugent Date: Tue, 7 May 2024 17:01:22 -0400 Subject: initial commit --- src/GpgSigner.cs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/GpgSigner.cs (limited to 'src/GpgSigner.cs') diff --git a/src/GpgSigner.cs b/src/GpgSigner.cs new file mode 100644 index 0000000..76943f9 --- /dev/null +++ b/src/GpgSigner.cs @@ -0,0 +1,52 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using System.Collections.Generic; + +using VNLib.Tools.Build.Executor.Constants; + +namespace VNLib.Tools.Build.Executor +{ + public sealed class GpgSigner(bool enabled, string? defaultKey) + { + public bool IsEnabled { get; } = enabled; + + public async Task SignFileAsync(FileInfo file) + { + if (!IsEnabled) + { + return; + } + + List args = [ "--detach-sign" ]; + + if (!string.IsNullOrWhiteSpace(defaultKey)) + { + //Set the preferred key + args.Add("--default-key"); + args.Add(defaultKey); + } + + //Add input file + args.Add(file.FullName); + + //Delete an original file + string sigFile = $"{file.FullName}.sig"; + if (File.Exists(sigFile)) + { + File.Delete(sigFile); + } + + int result = await Utils.RunProcessAsync("gpg", null, args.ToArray()); + + switch (result) + { + case 2: + case 0: + break; + default: + throw new Exception($"Failed to sign file {file.FullName}"); + } + } + } +} \ No newline at end of file -- cgit