[rtems-central commit] util: Add base64_to_hex()

Sebastian Huber sebh at rtems.org
Tue Nov 21 13:35:40 UTC 2023


Module:    rtems-central
Branch:    master
Commit:    a4d045b0a8d6548be58b3b8abb3509429758c89d
Changeset: http://git.rtems.org/rtems-central/commit/?id=a4d045b0a8d6548be58b3b8abb3509429758c89d

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Tue Nov 21 11:13:15 2023 +0100

util: Add base64_to_hex()

---

 rtemsspec/tests/test_util.py | 8 ++++++--
 rtemsspec/util.py            | 8 ++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/rtemsspec/tests/test_util.py b/rtemsspec/tests/test_util.py
index 0873dc5e..35d484d0 100644
--- a/rtemsspec/tests/test_util.py
+++ b/rtemsspec/tests/test_util.py
@@ -27,8 +27,8 @@
 import os
 import logging
 
-from rtemsspec.util import copy_files, create_argument_parser, init_logging, \
-    load_config, run_command
+from rtemsspec.util import copy_files, create_argument_parser, base64_to_hex, \
+    init_logging, load_config, run_command
 from rtemsspec.tests.util import get_and_clear_log
 
 
@@ -41,6 +41,10 @@ def test_copy_files(tmpdir):
     assert os.path.exists(os.path.join(tmpdir, filename))
 
 
+def test_base64_to_hex():
+    assert base64_to_hex("ABCD") == "001083"
+
+
 def test_load_config():
     filename = os.path.join(os.path.dirname(__file__), "config", "a.yml")
     config = load_config(filename)
diff --git a/rtemsspec/util.py b/rtemsspec/util.py
index 41127031..851a7a25 100644
--- a/rtemsspec/util.py
+++ b/rtemsspec/util.py
@@ -25,6 +25,8 @@
 # POSSIBILITY OF SUCH DAMAGE.
 
 import argparse
+import base64
+import binascii
 import logging
 import os
 import shutil
@@ -33,6 +35,12 @@ from typing import Any, List, Optional
 import yaml
 
 
+def base64_to_hex(data: str) -> str:
+    """ Converts the data from base64 to hex. """
+    binary = base64.urlsafe_b64decode(data)
+    return binascii.hexlify(binary).decode('ascii')
+
+
 def copy_files(src_dir: str, dst_dir: str, files: List[str]) -> None:
     """
     Copies a list of files in the source directory to the destination



More information about the vc mailing list