From 10e82093b6e09dad202b06bcc6f49e09af869fb2 Mon Sep 17 00:00:00 2001
From: BuckarooBanzay <BuckarooBanzay@users.noreply.github.com>
Date: Sat, 28 Jan 2023 19:47:01 +0100
Subject: [PATCH] add `otp.generate_secret()`

---
 functions.lua      | 9 +++++++++
 functions.spec.lua | 6 ++++++
 2 files changed, 15 insertions(+)

diff --git a/functions.lua b/functions.lua
index db3f077..f90df3a 100644
--- a/functions.lua
+++ b/functions.lua
@@ -163,4 +163,13 @@ function otp.create_qr_png(data)
     assert(#png_data == width*height)
 
     return minetest.encode_png(width, height, png_data, 2)
+end
+
+function otp.generate_secret()
+    local buf = minetest.sha1("" .. math.random(10000), true)
+    local s = ""
+    for i=1,20 do
+        s = s .. string.char(string.byte(buf, i))
+    end
+    return s
 end
\ No newline at end of file
diff --git a/functions.spec.lua b/functions.spec.lua
index e5d629e..64893ce 100644
--- a/functions.spec.lua
+++ b/functions.spec.lua
@@ -57,4 +57,10 @@ mtt.register("otp.create_qr_png", function(callback)
     f:write(png)
     f:close()
     callback()
+end)
+
+mtt.register("otp.generate_secret", function(callback)
+    local s = otp.generate_secret()
+    assert(#s == 20)
+    callback()
 end)
\ No newline at end of file