git: 25f255d45f2d - stable/15 - nuage.lua: add encode_base64 helper

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Sun, 28 Jun 2026 18:40:50 UTC
The branch stable/15 has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=25f255d45f2dabf38fe50659ef9c4c5f5fe36dce

commit 25f255d45f2dabf38fe50659ef9c4c5f5fe36dce
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2026-05-07 21:53:04 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2026-06-28 18:36:23 +0000

    nuage.lua: add encode_base64 helper
    
    (cherry picked from commit 71e8122b3f6efdaac23ac219312dfe270731b495)
---
 libexec/nuageinit/nuage.lua | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/libexec/nuageinit/nuage.lua b/libexec/nuageinit/nuage.lua
index 6cef5d2dd904..3f614dba2b22 100644
--- a/libexec/nuageinit/nuage.lua
+++ b/libexec/nuageinit/nuage.lua
@@ -55,6 +55,35 @@ local function decode_base64(input)
 	return table.concat(result)
 end
 
+local function encode_base64(input)
+	if input == nil or #input == 0 then
+		return ""
+	end
+	local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
+	local result = {}
+	local pos = 1
+	local padding = ""
+	while pos <= #input do
+		local a = string.byte(input, pos)
+		local bb = pos + 1 <= #input and string.byte(input, pos + 1) or 0
+		local c = pos + 2 <= #input and string.byte(input, pos + 2) or 0
+		table.insert(result, string.sub(b, math.floor(a / 4) + 1, math.floor(a / 4) + 1))
+		table.insert(result, string.sub(b, math.floor(a % 4 * 16 + bb / 16) + 1, math.floor(a % 4 * 16 + bb / 16) + 1))
+		if pos + 1 <= #input then
+			table.insert(result, string.sub(b, math.floor(bb % 16 * 4 + c / 64) + 1, math.floor(bb % 16 * 4 + c / 64) + 1))
+		else
+			table.insert(result, "=")
+		end
+		if pos + 2 <= #input then
+			table.insert(result, string.sub(b, math.floor(c % 64) + 1, math.floor(c % 64) + 1))
+		else
+			table.insert(result, "=")
+		end
+		pos = pos + 3
+	end
+	return table.concat(result)
+end
+
 local function shell_escape(s)
 	return "'" .. string.gsub(s, "'", "'\\''") .. "'"
 end
@@ -964,6 +993,7 @@ local n = {
 	addsudo = addsudo,
 	adddoas = adddoas,
 	addfile = addfile,
+	encode_base64 = encode_base64,
 	add_fstab_entry = add_fstab_entry,
 	remove_fstab_entry = remove_fstab_entry,
 	write_resolv_conf = write_resolv_conf,