aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-03-09 14:52:04 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2024-03-09 14:52:04 -0500
commitbdf7c1bc36dbcc9f66f5caa344602817f888c49d (patch)
treed0a041e7545712e2a0e33aca362dee93b19973b5 /lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs
parent5d8614d205b7bdca56684a3cc5a08db90e3804b6 (diff)
Squashed commit of the following:
commit 7a263bf54b7967ddeb9f6b662339ec1c74546ce8 Author: vnugent <public@vaughnnugent.com> Date: Sat Mar 9 14:19:31 2024 -0500 refactor: Overhaul secret loading. Remove VaultSharp as a dep commit 766e179d110db4f955fffce55f2b0ad41c139179 Author: vnugent <public@vaughnnugent.com> Date: Wed Mar 6 21:35:35 2024 -0500 refactor: changed how service constructors are invoked, moved routing
Diffstat (limited to 'lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs')
-rw-r--r--lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs45
1 files changed, 28 insertions, 17 deletions
diff --git a/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs b/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs
index 2c231b7..23f2276 100644
--- a/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs
+++ b/lib/VNLib.Plugins.Extensions.Loading/src/Secrets/SecretResult.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Extensions.Loading
@@ -25,7 +25,6 @@
using System;
using VNLib.Utils;
-using VNLib.Utils.Extensions;
using VNLib.Utils.Memory;
namespace VNLib.Plugins.Extensions.Loading
@@ -41,29 +40,41 @@ namespace VNLib.Plugins.Extensions.Loading
///<inheritdoc/>
public ReadOnlySpan<char> Result => _secretChars;
-
- internal SecretResult(ReadOnlySpan<char> value) : this(value.ToArray())
- { }
-
- private SecretResult(char[] secretChars)
- {
- _secretChars = secretChars;
- }
-
+ private SecretResult(char[] secretChars) => _secretChars = secretChars;
///<inheritdoc/>
- protected override void Free()
- {
- MemoryUtil.InitializeBlock(_secretChars);
- }
+ protected override void Free() => MemoryUtil.InitializeBlock(_secretChars);
+ /// <summary>
+ /// Copies the data from the provided string into a new secret result
+ /// then erases the original string
+ /// </summary>
+ /// <param name="result">The secret string to read</param>
+ /// <returns>The <see cref="SecretResult"/> wrapper</returns>
internal static SecretResult ToSecret(string? result)
{
- SecretResult res = new(result.AsSpan());
- MemoryUtil.UnsafeZeroMemory<char>(result);
+ if (result == null)
+ {
+ return new SecretResult([]);
+ }
+
+ //Copy string data into a new char array
+ SecretResult res = new(result.ToCharArray());
+
+ //PrivateStringManager will safely erase the original string if it is able to
+ PrivateStringManager.EraseString(result);
+
return res;
}
+ /// <summary>
+ /// Copies the data from the provided span into a new secret result
+ /// by allocating a new array internally
+ /// </summary>
+ /// <param name="secretChars">The array of characters to copy</param>
+ /// <returns>The wrapped secret</returns>
+ internal static SecretResult ToSecret(ReadOnlySpan<char> secretChars) => new(secretChars.ToArray());
+
internal static SecretResult ToSecret(char[] result) => new(result);
}
}