From 426258b06bc300db9923f6b81d5af94bf77bfe94 Mon Sep 17 00:00:00 2001
From: jkerdreux-imt <jerome.kerdreux@imt-atlantique.fr>
Date: Mon, 27 Jan 2025 15:50:45 +0100
Subject: [PATCH] Remove xaal.tools dependency

---
 apps/tools/xaal/tools/keygen.py | 34 ++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/apps/tools/xaal/tools/keygen.py b/apps/tools/xaal/tools/keygen.py
index cd0f6ea1..7d46bc9f 100644
--- a/apps/tools/xaal/tools/keygen.py
+++ b/apps/tools/xaal/tools/keygen.py
@@ -1,17 +1,41 @@
-""" Tool to build a key pass for xAAL config file"""
+"""
+Tool to build a key pass for xAAL config file
+
+You can use it to get a key for xaal configuration file.
+This script support piping.
+
+$ echo xaal |xaal-keygen 
+
+"""
 
-from xaal.lib import tools
 import binascii
+import pysodium
+
+
+def pass2key(passphrase: str) -> bytes:
+    """Generate key from passphrase using libsodium"""
+    # This function is a cut / paste from xaal.lib.tools pass2key function.
+    # Check out this file for more information. This stuff avoid to import
+    # xaal.tools and messing w/ the xaal configuration file at the first
+    # install.
+    buf = passphrase.encode("utf-8")
+    KEY_BYTES = pysodium.crypto_pwhash_scryptsalsa208sha256_SALTBYTES  # 32
+    salt = ('\00' * KEY_BYTES).encode('utf-8')
+    opslimit = pysodium.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE
+    memlimit = pysodium.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE
+    key = pysodium.crypto_pwhash_scryptsalsa208sha256(KEY_BYTES, buf, salt, opslimit, memlimit)
+    return key
+
 
 def main():
     try:
         temp = input("Please enter your passphrase: ")
-        key  = tools.pass2key(temp)
+        key = pass2key(temp)
         print("Cut & Paste this key in your xAAL config-file")
-        print("key=%s"% binascii.hexlify(key).decode('utf-8'))
+        print("key=%s" % binascii.hexlify(key).decode('utf-8'))
     except KeyboardInterrupt:
         print("Bye Bye..")
-        
+
 
 if __name__ == '__main__':
     main()
-- 
GitLab